merge: modules/api/caption.py

This commit is contained in:
vladmandic
2026-03-12 14:16:50 +01:00
parent adff14c257
commit eb94fa3f38
+14 -3
View File
@@ -25,6 +25,7 @@ Core processing logic is shared between direct and dispatch handlers via
``do_openclip``, ``do_tagger``, and ``do_vqa`` functions to avoid duplication.
"""
import threading
from typing import Literal, Annotated
from pydantic import BaseModel, Field # pylint: disable=no-name-in-module
from fastapi.exceptions import HTTPException
@@ -245,14 +246,16 @@ class ResCaptionDispatch(BaseModel):
# =============================================================================
def validate_image(image_b64: str):
"""Validate and decode a base64 image string, returning an RGB PIL Image.
"""Validate and decode an image string (base64 or upload ref), returning an RGB PIL Image.
Raises:
HTTPException(404): If image data is missing or too short to be valid.
HTTPException(404): If image data is missing.
"""
if image_b64 is None or len(image_b64) < 64:
if not image_b64:
raise HTTPException(status_code=404, detail="Image not found")
image = helpers.decode_base64_to_image(image_b64)
if image is None:
raise HTTPException(status_code=404, detail="Image not found")
return image.convert('RGB')
@@ -354,11 +357,19 @@ def parse_tagger_scores(tags: str) -> dict:
return scores or None
_tagger_lock = threading.Lock()
def do_tagger(image, req):
"""Core tagger logic shared by direct and dispatch endpoints.
Returns (tags, scores).
"""
with _tagger_lock:
return _do_tagger_locked(image, req)
def _do_tagger_locked(image, req):
from modules.caption import tagger
is_deepbooru = req.model.lower() in ('deepbooru', 'deepdanbooru')
original_opts = {