Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

26.2 KB
Raw Blame
import asyncio
import logging
import re
import threading
from contextlib import ExitStack
from pathlib import Path
from typing import Optional
from urllib.parse import quote

import torch
from fastapi import Body, HTTPException
from fastapi.responses import FileResponse
from fastapi.routing import APIRouter
from fontTools.ttLib import TTFont
from PIL import ImageFont
from pydantic import BaseModel, Field
from transformers import AutoProcessor, LlavaOnevisionForConditionalGeneration, LlavaOnevisionProcessor

from invokeai.app.api.auth_dependencies import CurrentUserOrDefault
from invokeai.app.api.dependencies import ApiDependencies
from invokeai.app.api.routers._access import assert_image_read_access
from invokeai.app.api.routers.image_move_maintenance import assert_image_move_maintenance_inactive
from invokeai.app.services.events.events_base import EventServiceBase
from invokeai.app.services.image_files.image_files_common import ImageFileNotFoundException
from invokeai.app.services.image_records.image_records_common import ImageRecordNotFoundException
from invokeai.app.services.model_records.model_records_base import UnknownModelException
from invokeai.app.services.system_prompt_records.system_prompt_records_common import (
    EXPAND_PROMPT_MAX_TOKENS_DEFAULT,
    EXPAND_PROMPT_MAX_TOKENS_MAX,
    EXPAND_PROMPT_MAX_TOKENS_MIN,
)
from invokeai.app.services.wildcard_records.wildcard_records_common import build_wildcard_manager
from invokeai.app.util.dynamicprompts import expand_dynamic_prompt
from invokeai.app.util.misc import SEED_MAX, get_random_seed
from invokeai.backend.llava_onevision_pipeline import LlavaOnevisionPipeline
from invokeai.backend.model_manager.configs.roles import serves_as, view_as
from invokeai.backend.model_manager.taxonomy import ModelType
from invokeai.backend.text_llm_pipeline import (
    DEFAULT_SYSTEM_PROMPT,
    ProgressCallback,
    TextLLMPipeline,
    load_text_llm_tokenizer_and_processor,
)
from invokeai.backend.util.device_pool import idle_device_borrowed
from invokeai.backend.util.devices import TorchDevice

logger = logging.getLogger(__name__)

utilities_router = APIRouter(prefix="/v1/utilities", tags=["utilities"])
SUPPORTED_FONT_EXTENSIONS = {".ttf", ".otf", ".woff", ".woff2"}
FONT_MEDIA_TYPES = {
    ".ttf": "font/ttf",
    ".otf": "font/otf",
    ".woff": "font/woff",
    ".woff2": "font/woff2",
}
# Directory-backed files can be replaced in place. They must not be pinned by the browser's
# immutable cache, otherwise a rescan can expose stale bytes to a project.
FONT_CACHE_CONTROL = "private, no-cache"

# The underlying model loader is not thread-safe, so we serialize load_model calls.
_model_load_lock = threading.Lock()


class DynamicPromptsResponse(BaseModel):
    prompts: list[str]
    error: Optional[str] = None


class UserFontFace(BaseModel):
    path: str
    url: str
    weight: int
    style: str


class UserFont(BaseModel):
    id: str
    family: str
    label: str
    path: str
    url: str
    faces: list[UserFontFace]


class UserFontsResponse(BaseModel):
    fonts: list[UserFont]


@utilities_router.post(
    "/dynamicprompts",
    operation_id="parse_dynamicprompts",
    responses={
        200: {"model": DynamicPromptsResponse},
    },
)
def parse_dynamicprompts(
    current_user: CurrentUserOrDefault,
    prompt: str = Body(description="The prompt to parse with dynamicprompts"),
    max_prompts: int = Body(ge=1, le=10000, default=1000, description="The max number of prompts to generate"),
    combinatorial: bool = Body(default=True, description="Whether to use the combinatorial generator"),
    seed: int | None = Body(None, description="The seed to use for random generation. Only used if not combinatorial"),
) -> DynamicPromptsResponse:
    """Expands a prompt's dynamic syntax against the current user's wildcards."""
    expanded = expand_dynamic_prompt(
        prompt,
        max_prompts=min(max_prompts, 10000),
        combinatorial=combinatorial,
        seed=seed,
        wildcard_manager=build_wildcard_manager(
            ApiDependencies.invoker.services.wildcard_records.get_many(user_id=current_user.user_id)
        ),
    )
    return DynamicPromptsResponse(prompts=expanded.prompts, error=expanded.error)


def _get_fonts_dir() -> Path:
    return ApiDependencies.invoker.services.configuration.fonts_path


def _path_has_symlink_component(path: Path, boundary: Path) -> bool:
    current = path
    boundary = boundary.absolute()
    try:
        current.absolute().relative_to(boundary)
    except ValueError:
        return True

    while True:
        if current.is_symlink():
            return True
        if current == boundary:
            return False
        current = current.parent


def _get_name_table_value(font: TTFont, name_ids: tuple[int, ...]) -> str | None:
    if "name" not in font:
        return None

    def _sort_key(record: object) -> tuple[int, int]:
        platform_id = getattr(record, "platformID", -1)
        lang_id = getattr(record, "langID", -1)
        if platform_id == 3 and lang_id in (0x0409, 0):
            return (0, 0)
        if platform_id == 3:
            return (1, 0)
        if platform_id == 0:
            return (2, 0)
        return (3, lang_id)

    records = font["name"].names
    for name_id in name_ids:
        for record in sorted((record for record in records if record.nameID == name_id), key=_sort_key):
            try:
                value = record.toUnicode().strip()
            except Exception:
                continue
            if value:
                return value
    return None


def _normalize_variant_text(value: str) -> str:
    value = re.sub(r"(?<=[a-z])(?=[A-Z])", " ", value)
    value = value.replace("_", " ").replace("-", " ")
    value = re.sub(r"\s+", " ", value)
    return value.strip().lower()


def _infer_font_weight(style_name: str, file_stem: str, weight_class: int | None) -> int:
    if isinstance(weight_class, int) and 1 <= weight_class <= 1000:
        return weight_class

    combined = _normalize_variant_text(f"{style_name} {file_stem}")
    # Ordering matters: more specific keywords must be matched before "bold".
    weight_keywords = [
        (("thin", "hairline"), 100),
        (("extra light", "ultra light", "extralight", "ultralight"), 200),
        (("light",), 300),
        (("normal", "regular", "roman", "book"), 400),
        (("medium",), 500),
        (("semi bold", "semibold", "demi bold", "demibold"), 600),
        (("extra bold", "ultra bold", "extrabold", "ultrabold"), 800),
        (("black", "heavy"), 900),
        (("bold",), 700),
    ]
    for keywords, weight in weight_keywords:
        if any(keyword in combined for keyword in keywords):
            return weight

    return 400


def _infer_font_style(style_name: str, file_stem: str, italic_flag: bool) -> str:
    if italic_flag:
        return "italic"

    combined = _normalize_variant_text(f"{style_name} {file_stem}")
    if "italic" in combined or "oblique" in combined:
        return "italic"

    return "normal"


def _get_font_metadata(font_file: Path) -> tuple[str, str, int, str] | None:
    ttfont_error: Exception | None = None
    try:
        with TTFont(font_file.as_posix(), lazy=True) as font:
            family_name = (_get_name_table_value(font, (16, 1)) or "").strip()
            style_name = (_get_name_table_value(font, (17, 2)) or "").strip()
            os2_table = font["OS/2"] if "OS/2" in font else None
            head_table = font["head"] if "head" in font else None
            post_table = font["post"] if "post" in font else None
            weight_class = getattr(os2_table, "usWeightClass", None)
            italic_flag = bool(getattr(os2_table, "fsSelection", 0) & 0x01) or bool(
                getattr(head_table, "macStyle", 0) & 0x02
            )
            if post_table is not None:
                italic_flag = italic_flag or bool(getattr(post_table, "italicAngle", 0))
            if family_name:
                return (
                    family_name,
                    family_name,
                    _infer_font_weight(style_name, font_file.stem, weight_class),
                    _infer_font_style(style_name, font_file.stem, italic_flag),
                )
    except Exception as e:
        ttfont_error = e

    try:
        font = ImageFont.truetype(font_file.as_posix(), size=16)
        family_name, style_name = font.getname()
        family_name = family_name.strip()
        style_name = style_name.strip()
        if family_name:
            return (
                family_name,
                family_name,
                _infer_font_weight(style_name, font_file.stem, None),
                _infer_font_style(style_name, font_file.stem, False),
            )
    except Exception as e:
        logger.warning(
            "Skipping font file %s: unable to read font metadata with fontTools or Pillow (%s, %s)",
            font_file,
            type(ttfont_error).__name__ if ttfont_error else "no-fontTools-error",
            type(e).__name__,
            exc_info=e,
        )
        return None

    logger.warning("Skipping font file %s: missing font family metadata", font_file)
    return None


def _resolve_font_request_path(font_path: str) -> Path:
    fonts_dir = _get_fonts_dir()
    if not fonts_dir.exists() or not fonts_dir.is_dir():
        raise HTTPException(status_code=404, detail="Font file not found")

    requested = (fonts_dir / font_path).absolute()
    if _path_has_symlink_component(requested, fonts_dir):
        raise HTTPException(status_code=400, detail="Invalid font path")

    resolved_fonts_dir = fonts_dir.resolve()
    resolved_requested = requested.resolve()

    try:
        resolved_requested.relative_to(resolved_fonts_dir)
    except ValueError as e:
        raise HTTPException(status_code=400, detail="Invalid font path") from e

    return resolved_requested


@utilities_router.get(
    "/fonts",
    operation_id="list_user_fonts",
    responses={200: {"model": UserFontsResponse}},
)
def list_user_fonts(_current_user: CurrentUserOrDefault) -> UserFontsResponse:
    fonts_dir = _get_fonts_dir()
    if not fonts_dir.exists() or not fonts_dir.is_dir() or fonts_dir.is_symlink():
        if fonts_dir.is_symlink():
            logger.warning("Skipping custom fonts directory %s: symlinks are not supported", fonts_dir)
        return UserFontsResponse(fonts=[])

    family_candidates: dict[str, list[tuple[Path, str, str, int, str]]] = {}
    # key -> [(font_file, relative, family, weight, style)]
    for font_file in sorted(fonts_dir.rglob("*")):
        if _path_has_symlink_component(font_file.absolute(), fonts_dir):
            logger.warning("Skipping font path %s: symlinks are not supported", font_file)
            continue
        if not font_file.is_file() or font_file.suffix.lower() not in SUPPORTED_FONT_EXTENSIONS:
            continue
        relative = font_file.relative_to(fonts_dir).as_posix()
        metadata = _get_font_metadata(font_file)
        if metadata is None:
            continue
        family, _label, weight, style = metadata
        family_key = family.strip().lower()
        family_candidates.setdefault(family_key, []).append((font_file, relative, family, weight, style))

    def _candidate_score(weight: int, style: str, path: Path) -> tuple[int, int, int]:
        """Lower score is better. Prefer regular/normal faces, then shorter names."""
        return (0 if style == "normal" else 1, abs(weight - 400), len(path.stem))

    fonts: list[UserFont] = []
    for family_key, candidates in sorted(family_candidates.items(), key=lambda kv: kv[0]):
        _, selected_relative, selected_family, _, _ = min(candidates, key=lambda c: _candidate_score(c[3], c[4], c[0]))
        faces_by_variant: dict[tuple[int, str], tuple[Path, str, str, int, str]] = {}
        for candidate in candidates:
            variant_key = (candidate[3], candidate[4])
            current = faces_by_variant.get(variant_key)
            if current is None or _candidate_score(candidate[3], candidate[4], candidate[0]) < _candidate_score(
                current[3], current[4], current[0]
            ):
                faces_by_variant[variant_key] = candidate

        faces = [
            UserFontFace(
                path=relative,
                url=f"api/v1/utilities/fonts/{quote(relative)}",
                weight=weight,
                style=style,
            )
            for (weight, style), (_, relative, _, _, _) in sorted(
                faces_by_variant.items(), key=lambda item: (item[0][1] != "normal", abs(item[0][0] - 400), item[0][0])
            )
        ]

        fonts.append(
            UserFont(
                id=f"user:{family_key}",
                family=selected_family,
                label=selected_family,
                path=selected_relative,
                url=f"api/v1/utilities/fonts/{quote(selected_relative)}",
                faces=faces,
            )
        )

    return UserFontsResponse(fonts=fonts)


@utilities_router.get(
    "/fonts/{font_path:path}",
    operation_id="get_user_font_file",
)
def get_user_font_file(font_path: str, _current_user: CurrentUserOrDefault) -> FileResponse:
    requested = _resolve_font_request_path(font_path)
    if not requested.exists() or not requested.is_file():
        raise HTTPException(status_code=404, detail="Font file not found")

    if requested.suffix.lower() not in SUPPORTED_FONT_EXTENSIONS:
        raise HTTPException(status_code=400, detail="Unsupported font format")

    return FileResponse(
        path=requested,
        media_type=FONT_MEDIA_TYPES[requested.suffix.lower()],
        filename=requested.name,
        content_disposition_type="inline",
        headers={"Cache-Control": FONT_CACHE_CONTROL},
    )


# --- Expand Prompt ---


class ExpandPromptRequest(BaseModel):
    prompt: str
    model_key: str
    max_tokens: int = Field(
        default=EXPAND_PROMPT_MAX_TOKENS_DEFAULT,
        ge=EXPAND_PROMPT_MAX_TOKENS_MIN,
        le=EXPAND_PROMPT_MAX_TOKENS_MAX,
        description=(
            "Cap on the tokens the LLM may emit. Clients that expand with a stored system prompt "
            "send that prompt's own `max_tokens` here; omitting it uses the default."
        ),
    )
    system_prompt: str | None = None
    seed: int | None = Field(default=None, ge=0, le=SEED_MAX, description="Seed for reproducible text generation")
    task_id: str | None = Field(
        default=None, description="Client-supplied task ID used to correlate socket progress events to this request"
    )
    image_name: str | None = Field(
        default=None,
        description=(
            "An image to condition the rewrite on, such as a video's first frame. Requires a model whose "
            "`supports_images` is true."
        ),
    )


class ExpandPromptResponse(BaseModel):
    expanded_prompt: str
    seed: int
    error: str | None = None


def _resolve_model_path(model_config_path: str) -> Path:
    """Resolve a model config path to an absolute path."""
    model_path = Path(model_config_path)
    if model_path.is_absolute():
        return model_path.resolve()
    base_models_path = ApiDependencies.invoker.services.configuration.models_path
    return (base_models_path / model_path).resolve()


def _make_progress_callback(events: EventServiceBase, task_id: str | None, user_id: str) -> ProgressCallback | None:
    """Build a progress callback that emits llm_task_progress events during generation.

    Returns None when no task_id was supplied, since there is nothing to correlate the
    events to. Shared by the text-LLM and image-to-prompt endpoints to keep the emitted
    payloads identical.
    """
    if task_id is None:
        return None

    def progress_callback(current: int, total: int) -> None:
        events.emit_llm_task_progress(
            task_id=task_id,
            user_id=user_id,
            phase="generating",
            message="Generating",
            percentage=(current / total) if total > 0 else None,
            current_tokens=current,
            total_tokens=total,
        )

    return progress_callback


def _run_expand_prompt(
    prompt: str,
    model_key: str,
    max_tokens: int,
    system_prompt: str | None,
    seed: int | None,
    task_id: str | None,
    user_id: str,
    image_name: str | None = None,
) -> tuple[str, int]:
    """Run text LLM inference synchronously (called from thread)."""
    model_manager = ApiDependencies.invoker.services.model_manager
    events = ApiDependencies.invoker.services.events
    model_config = model_manager.store.get_model(model_key)

    if not serves_as(model_config, ModelType.TextLLM):
        raise ValueError(f"Model '{model_key}' is not a TextLLM model (got {model_config.type})")
    model_config = view_as(model_config, ModelType.TextLLM)
    # Only the TextLLM config carries the flag (the record type is a union); older records validate to False.
    if image_name is not None and not getattr(model_config, "supports_images", False):
        raise ValueError(f"Model '{model_config.name}' cannot read images; choose a vision-capable text LLM")

    # Read before the model load so a missing image fails fast.
    image = (
        ApiDependencies.invoker.services.images.get_pil_image(image_name).convert("RGB")
        if image_name is not None
        else None
    )

    if task_id is not None:
        events.emit_llm_task_progress(task_id=task_id, user_id=user_id, phase="loading_model", message="Loading model")

    # Loaded before the model so the disk reads do not hold it on the device.
    with _model_load_lock:
        tokenizer, processor = load_text_llm_tokenizer_and_processor(
            lambda submodel: model_manager.load.load_model(model_config, submodel, user_id=user_id),
            with_processor=image is not None,
        )

    # Outside the session queue: take an idle GPU (and its lock) for the load and the run. The borrow is
    # taken once the load lock is ours, so a request queued behind another's load holds no GPU meanwhile;
    # borrowing never blocks, so this ordering cannot deadlock.
    with ExitStack() as gpu:
        with _model_load_lock:
            gpu.enter_context(idle_device_borrowed(purpose="Expand Prompt"))
            loaded_model = model_manager.load.load_model(model_config, user_id=user_id)

        with torch.no_grad(), loaded_model.model_on_device() as (_, model):
            pipeline = TextLLMPipeline(model, tokenizer, processor)
            model_device = next(model.parameters()).device

            progress_callback = _make_progress_callback(events, task_id, user_id)

            effective_seed = seed if seed is not None else get_random_seed()
            output = pipeline.run(
                prompt=prompt,
                system_prompt=system_prompt or DEFAULT_SYSTEM_PROMPT,
                max_new_tokens=max_tokens,
                seed=effective_seed,
                device=model_device,
                dtype=TorchDevice.choose_torch_dtype(),
                progress_callback=progress_callback,
                image=image,
            )

    return output, effective_seed


@utilities_router.post(
    "/expand-prompt",
    operation_id="expand_prompt",
    responses={
        200: {"model": ExpandPromptResponse},
    },
)
async def expand_prompt(current_user: CurrentUserOrDefault, body: ExpandPromptRequest) -> ExpandPromptResponse:
    """Expand a brief prompt into a detailed image generation prompt using a text LLM."""
    if body.image_name is not None:
        # Same policy as image-to-prompt: non-owners cannot probe stored images through this endpoint.
        await asyncio.to_thread(assert_image_move_maintenance_inactive)
        await asyncio.to_thread(assert_image_read_access, body.image_name, current_user)

    events = ApiDependencies.invoker.services.events
    try:
        expanded, seed = await asyncio.to_thread(
            _run_expand_prompt,
            body.prompt,
            body.model_key,
            body.max_tokens,
            body.system_prompt,
            body.seed,
            body.task_id,
            current_user.user_id,
            body.image_name,
        )
        if body.task_id is not None:
            events.emit_llm_task_complete(task_id=body.task_id, user_id=current_user.user_id)
        return ExpandPromptResponse(expanded_prompt=expanded, seed=seed)
    except UnknownModelException:
        if body.task_id is not None:
            events.emit_llm_task_error(task_id=body.task_id, user_id=current_user.user_id, error="Model not found")
        raise HTTPException(status_code=404, detail=f"Model '{body.model_key}' not found")
    except (ImageFileNotFoundException, ImageRecordNotFoundException):
        # The record check matters for admins (and single-user mode), whose access check never reads the record.
        if body.task_id is not None:
            events.emit_llm_task_error(task_id=body.task_id, user_id=current_user.user_id, error="Image not found")
        raise HTTPException(status_code=404, detail=f"Image '{body.image_name}' not found")
    except ValueError as e:
        if body.task_id is not None:
            events.emit_llm_task_error(task_id=body.task_id, user_id=current_user.user_id, error=str(e))
        raise HTTPException(status_code=422, detail=str(e))
    except Exception as e:
        if body.task_id is not None:
            events.emit_llm_task_error(task_id=body.task_id, user_id=current_user.user_id, error=str(e))
        logger.error(f"Error expanding prompt: {e}")
        raise HTTPException(status_code=500, detail=str(e))


# --- Image to Prompt ---


class ImageToPromptRequest(BaseModel):
    image_name: str
    model_key: str
    instruction: str = "Describe this image in detail for use as an AI image generation prompt."
    task_id: str | None = Field(
        default=None, description="Client-supplied task ID used to correlate socket progress events to this request"
    )


class ImageToPromptResponse(BaseModel):
    prompt: str
    error: str | None = None


def _run_image_to_prompt(
    image_name: str,
    model_key: str,
    instruction: str,
    task_id: str | None,
    user_id: str,
) -> str:
    """Run LLaVA OneVision inference synchronously (called from thread)."""
    model_manager = ApiDependencies.invoker.services.model_manager
    events = ApiDependencies.invoker.services.events
    model_config = model_manager.store.get_model(model_key)

    if model_config.type != ModelType.LlavaOnevision:
        raise ValueError(f"Model '{model_key}' is not a LLaVA OneVision model (got {model_config.type})")

    if task_id is not None:
        events.emit_llm_task_progress(task_id=task_id, user_id=user_id, phase="loading_model", message="Loading model")

    # Read before the borrow below, so neither disk read holds a GPU.
    image = ApiDependencies.invoker.services.images.get_pil_image(image_name).convert("RGB")
    processor = AutoProcessor.from_pretrained(_resolve_model_path(model_config.path), local_files_only=True)
    if not isinstance(processor, LlavaOnevisionProcessor):
        raise TypeError(f"Expected LlavaOnevisionProcessor, got {type(processor).__name__}")

    # Outside the session queue: take an idle GPU (and its lock) for the load and the run. The borrow is
    # taken once the load lock is ours, so a request queued behind another's load holds no GPU meanwhile;
    # borrowing never blocks, so this ordering cannot deadlock.
    with ExitStack() as gpu:
        with _model_load_lock:
            gpu.enter_context(idle_device_borrowed(purpose="Image to Prompt"))
            loaded_model = model_manager.load.load_model(model_config, user_id=user_id)

        with torch.no_grad(), loaded_model.model_on_device() as (_, model):
            if not isinstance(model, LlavaOnevisionForConditionalGeneration):
                raise TypeError(f"Expected LlavaOnevisionForConditionalGeneration, got {type(model).__name__}")

            pipeline = LlavaOnevisionPipeline(model, processor)
            model_device = next(model.parameters()).device

            progress_callback = _make_progress_callback(events, task_id, user_id)

            output = pipeline.run(
                prompt=instruction,
                images=[image],
                device=model_device,
                dtype=TorchDevice.choose_torch_dtype(),
                progress_callback=progress_callback,
            )

    return output


@utilities_router.post(
    "/image-to-prompt",
    operation_id="image_to_prompt",
    responses={
        200: {"model": ImageToPromptResponse},
    },
)
async def image_to_prompt(current_user: CurrentUserOrDefault, body: ImageToPromptRequest) -> ImageToPromptResponse:
    """Generate a descriptive prompt from an image using a vision-language model."""
    await asyncio.to_thread(assert_image_move_maintenance_inactive)

    # Reuse the image-read access check so non-owners can't probe stored images
    # via this endpoint (mirrors the policy in routers/images.py).
    await asyncio.to_thread(assert_image_read_access, body.image_name, current_user)

    events = ApiDependencies.invoker.services.events
    try:
        prompt = await asyncio.to_thread(
            _run_image_to_prompt,
            body.image_name,
            body.model_key,
            body.instruction,
            body.task_id,
            current_user.user_id,
        )
        if body.task_id is not None:
            events.emit_llm_task_complete(task_id=body.task_id, user_id=current_user.user_id)
        return ImageToPromptResponse(prompt=prompt)
    except UnknownModelException:
        if body.task_id is not None:
            events.emit_llm_task_error(task_id=body.task_id, user_id=current_user.user_id, error="Model not found")
        raise HTTPException(status_code=404, detail=f"Model '{body.model_key}' not found")
    except (ImageFileNotFoundException, ImageRecordNotFoundException):
        if body.task_id is not None:
            events.emit_llm_task_error(task_id=body.task_id, user_id=current_user.user_id, error="Image not found")
        raise HTTPException(status_code=404, detail=f"Image '{body.image_name}' not found")
    except (ValueError, TypeError) as e:
        if body.task_id is not None:
            events.emit_llm_task_error(task_id=body.task_id, user_id=current_user.user_id, error=str(e))
        raise HTTPException(status_code=422, detail=str(e))
    except Exception as e:
        if body.task_id is not None:
            events.emit_llm_task_error(task_id=body.task_id, user_id=current_user.user_id, error=str(e))
        logger.error(f"Error generating prompt from image: {e}")
        raise HTTPException(status_code=500, detail=str(e))