4331 行
212 KiB
Python
4331 行
212 KiB
Python
"""Style multi-image extraction and transfer validation.
|
||
|
||
The existing advanced-material service remains the compatibility boundary for
|
||
single-image imports. This coordinator adds a child-task graph only when the
|
||
server-confirmed material category is style:
|
||
|
||
* one independent VLM task per source image;
|
||
* one multi-image consensus VLM task after the source barrier;
|
||
* one compatibility validation attempt containing three independent image
|
||
generation cases with deliberately unrelated characters and scenes.
|
||
|
||
Validation may optionally attach one source image as a style-only reference
|
||
(shared by portrait/fullbody/novel_scene). Identity/costume/pose/scene copy
|
||
from the reference is still forbidden; pure text remains the fallback.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import datetime, timezone
|
||
|
||
import json
|
||
import re
|
||
from copy import deepcopy
|
||
from dataclasses import replace
|
||
from typing import Any
|
||
from uuid import uuid4
|
||
|
||
from backend.dto.common import ApiError, ErrorCode
|
||
from backend.dto.material_dto import Material
|
||
from backend.dto.workflow_dto import WorkflowNode
|
||
from backend.services.material_normalization_service import MaterialNormalizationError
|
||
from core.generation_errors import provider_failure_attribution
|
||
from backend.services.material_service import (
|
||
_normalize_upload_content_type,
|
||
_normalize_upload_filename,
|
||
_normalize_upload_metadata,
|
||
_normalize_upload_size,
|
||
_recognition_metadata,
|
||
_storage_image_data_url,
|
||
_vision_provider_error,
|
||
)
|
||
from backend.services.worker_service import WorkerTaskUpdate
|
||
from core.identity import RequestContext
|
||
from engines.image_provider import ImageProviderError
|
||
from engines.vision_provider import VisionProviderError, VisionRecognitionRequest, VisionRecognitionResult
|
||
|
||
|
||
MIN_STYLE_SOURCES = 2
|
||
MAX_STYLE_SOURCES = 8
|
||
STYLE_SOURCE_TERMINAL = {"succeeded", "failed", "excluded", "cancelled"}
|
||
STYLE_SOURCE_EXECUTABLE = {"queued", "running"}
|
||
STYLE_CONSENSUS_EXECUTABLE = {"queued", "running"}
|
||
STYLE_CASE_EXECUTABLE = {"queued", "running"}
|
||
STYLE_ATTEMPT_BLOCKING = {"queued", "running", "validation_partial_failed", "awaiting_review"}
|
||
STYLE_SET_KIND = "advanced_style_set"
|
||
STYLE_VALIDATION_SEEDS = (240719, 240720, 240721)
|
||
STYLE_VALIDATION_TIMEOUT_SECONDS = 900
|
||
STYLE_EVIDENCE_MIN_SUPPORT = 0.60
|
||
STYLE_EVIDENCE_MIN_CONFIDENCE = 0.70
|
||
_ARTIST_SIGNATURE_DIMENSION_ORDER = (
|
||
"face_structure",
|
||
"eye_design",
|
||
"hair_rendering",
|
||
"linework",
|
||
"shading",
|
||
"palette",
|
||
"highlights",
|
||
"texture",
|
||
"anatomy",
|
||
)
|
||
_ARTIST_SIGNATURE_DIMENSION_MARKERS: dict[str, tuple[str, ...]] = {
|
||
"face_structure": ("face", "facial", "jaw", "chin", "cheek", "nose", "mouth", "forehead"),
|
||
"eye_design": ("eye", "iris", "pupil", "lash", "eyelid", "catchlight"),
|
||
"hair_rendering": ("hair", "strand", "bang", "fringe", "lock", "curl"),
|
||
"linework": ("line", "ink", "outline", "contour", "stroke", "taper"),
|
||
"shading": ("shadow", "shadows", "shading", "cel", "occlusion", "gradient", "rendering edge"),
|
||
"palette": ("palette", "color", "colour", "chroma", "saturation", "hue", "sepia", "rose", "teal"),
|
||
"highlights": ("highlight", "sheen", "gloss", "specular", "rim light"),
|
||
"texture": ("texture", "grain", "brush", "surface", "paper", "noise"),
|
||
"anatomy": ("head-to-body", "head to body", "proportion", "anatomy", "limb", "silhouette"),
|
||
}
|
||
_STYLE_KERNEL_RULES: tuple[tuple[tuple[str, ...], str], ...] = (
|
||
(
|
||
(r"wide|rounded", r"short|compact|tiny\s+chin|small\s+chin"),
|
||
"short broad rounded facial geometry with a compact tiny chin, preserving the source width-to-length variation without elongating it",
|
||
),
|
||
(
|
||
(r"oversized|very\s+large|large", r"eye|iris", r"layered|gradient|catchlight"),
|
||
"large round-oval eyes at the source-proportional scale with layered gradient irises, dark upper rims, and multiple crisp circular or crescent white catchlights",
|
||
),
|
||
(
|
||
(r"hair", r"broad|flowing|segment|gloss|highlight", r"white|bright|curved|sweep"),
|
||
"hair grouped into broad flowing segments with 2-4 opaque elongated pearlescent white sheen bands crossing each major curved lock, plus layered flyaways at the tips",
|
||
),
|
||
(
|
||
(r"colored", r"line|contour|outline|ink"),
|
||
"thin tapered linework tinted in local lavender, plum, rose, or blue fill colors instead of heavy black ink",
|
||
),
|
||
(
|
||
(r"high[- ]key|pastel|luminous", r"palette|spectrum|color|white|pink|blue|lavender"),
|
||
"high-key pastel color relationships built from luminous white, pink, powder blue, mint, and lavender accents",
|
||
),
|
||
(
|
||
(r"pink|peach|rose", r"blush|cheek|skin"),
|
||
"diffuse peach-pink cheek blush with small soft white highlight accents",
|
||
),
|
||
(
|
||
(r"cool|lavender|blue|violet|purple", r"shadow|shading|cel|gradient"),
|
||
"soft airbrush transitions combined with translucent cool lavender-blue cel-shadow bands and clear local edges",
|
||
),
|
||
)
|
||
_STYLE_BACKGROUND_EFFECT_PATTERNS: tuple[tuple[str, str], ...] = (
|
||
(r"\bsoft\s+floating\s+particles\b", ""),
|
||
(r"\bfloating\s+particles\b", ""),
|
||
(r"\bsoft\s+background\s+bokeh\b", ""),
|
||
(r"\bbackground\s+bokeh\b", ""),
|
||
)
|
||
_STYLE_SCALE_KERNELS: dict[str, str] = {
|
||
"portrait": (
|
||
"at portrait scale prioritize the face width-to-length ratio, eye-to-face ratio, iris layers, clustered eye catchlights, "
|
||
"cheek blush accents, and surface-bound horizontal hair gloss; do not redesign or elongate the facial contour"
|
||
),
|
||
"fullbody": (
|
||
"at full-body scale keep a subject-appropriate stylized anatomy band of about 5-7 heads tall, never chibi 2-4 heads, "
|
||
"while preserving the same face, linework, hair highlight, and cool shadow grammar"
|
||
),
|
||
"novel_scene": (
|
||
"in a wide scene keep the character and environment in the same 2D pastel illustration grammar; simplify architecture and "
|
||
"surface detail, avoid photorealistic backgrounds, and let the requested scene content control setting and weather"
|
||
),
|
||
}
|
||
STYLE_DELTA_SEVERITY = {
|
||
"low": 0.20,
|
||
"minor": 0.25,
|
||
"moderate": 0.50,
|
||
"medium": 0.50,
|
||
"high": 0.75,
|
||
"critical": 1.00,
|
||
}
|
||
|
||
_MINOR_AGE_REWRITES: tuple[tuple[str, str], ...] = (
|
||
# Chronological-minor identity wording -> non-age style phrasing.
|
||
# Do NOT rewrite into mature-adult locks; age band is owned by content prompts.
|
||
(r"\bpre[\s-]?adolescent(?:\s+to\s+early\s+adolescent)?\b", "neotenic facial proportions"),
|
||
(r"\bearly\s+adolescent\b", "youthful facial proportions"),
|
||
(r"\badolescent\b", "youthful facial proportions"),
|
||
(r"\bteen(?:age[dr]?)?\b", "youthful facial proportions"),
|
||
(r"\bminor(?:-aged)?\b", ""),
|
||
(r"\bchild(?:like|ish)?\s+proportions\b", "stylized compact proportions"),
|
||
(r"\bchild(?:like|ish)?\b", "neotenic stylization"),
|
||
(r"\bkid\b", ""),
|
||
(r"\bloli(?:ta)?\b", "neotenic stylized character design"),
|
||
(r"\bmoe-style\s+juvenile\s+face\b", "moe-style neotenic facial stylization"),
|
||
(r"\bjuvenile\b", "neotenic"),
|
||
(r"\btoddler\s+proportions\b", "compressed chibi anatomy"),
|
||
(r"\b(?:2|3|4)\s+heads?\s+tall\b", "compressed chibi anatomy"),
|
||
)
|
||
_MATURE_BODY_LOCK_PATTERNS: tuple[str, ...] = (
|
||
r"\bmature\s+adult(?:\s+limb\s+lengths)?\b",
|
||
r"\bexplicitly\s+adult(?:\s+subject)?(?:\s+with\s+mature\s+(?:adult\s+)?(?:body\s+)?proportions)?\b",
|
||
r"\bnatural\s+adult\s+body\b",
|
||
r"\bbetween\s+6\.5\s+and\s+7\.5\s+heads?\s+tall\b",
|
||
r"\b6\.5\s*[–\-]\s*7\.5\s+heads?(?:-tall)?\b",
|
||
r"\b6\.5\s+to\s+7\.5\s+heads?(?:-tall)?\b",
|
||
r"\bmust\s+adapt\s+to\s+a\s+natural\s+6\.5.*?proportion[^.]*\.?",
|
||
r"\bstyle\s+preservation\s+must\s+adapt\s+to\s+a\s+natural\s+6\.5.*?proportion[^.]*\.?",
|
||
)
|
||
# Full-body anatomy soft band for style transfer validation.
|
||
# This is NOT a mature-adult identity lock. It only prevents free collapse into
|
||
# chibi/super-deformed (approx 2-4 heads) when the style face is large-eyed/neotenic.
|
||
_ANATOMY_SOFT_MIN_HEADS = 5.0
|
||
_ANATOMY_SOFT_MAX_HEADS = 7.0
|
||
_ANATOMY_SOFT_DEFAULT_HEADS = (5.5, 6.5)
|
||
_HEAD_RATIO_PATTERNS: tuple[re.Pattern[str], ...] = (
|
||
re.compile(
|
||
r"1\s*:\s*(?P<a>\d+(?:\.\d+)?)\s*(?:to|[-–—]|~|~)\s*1\s*:\s*(?P<b>\d+(?:\.\d+)?)",
|
||
re.IGNORECASE,
|
||
),
|
||
re.compile(
|
||
r"(?P<a>\d+(?:\.\d+)?)\s*(?:to|[-–—]|~|~)\s*(?P<b>\d+(?:\.\d+)?)\s*heads?(?:-tall)?",
|
||
re.IGNORECASE,
|
||
),
|
||
re.compile(
|
||
r"(?P<a>\d+(?:\.\d+)?)\s*heads?(?:-tall)?",
|
||
re.IGNORECASE,
|
||
),
|
||
re.compile(
|
||
r"1\s*:\s*(?P<a>\d+(?:\.\d+)?)",
|
||
re.IGNORECASE,
|
||
),
|
||
)
|
||
|
||
_AGE_ATTRIBUTE_KEYS = {
|
||
"age",
|
||
"actual_age",
|
||
"age_range",
|
||
"apparent_age",
|
||
"subject_age",
|
||
"character_age",
|
||
}
|
||
_VARIABLE_CONTENT_PATTERNS = re.compile(
|
||
r"\b(background|environment|scene|rooftop|garden|bokeh|lens\s+flare|dawn|sunset|sunrise|golden\s+hour|"
|
||
r"neon|volumetric|rim\s+light|petals?|confetti|particles?|"
|
||
r"hdr|cinematic\s+lighting|dramatic\s+backlight)\b",
|
||
re.IGNORECASE,
|
||
)
|
||
_SEMANTIC_OBJECT_RISK_PATTERNS = re.compile(
|
||
r"\b(ring|halo|band|bar|stripe|crown|orb|ribbon|frame|glow\s+line)\b",
|
||
re.IGNORECASE,
|
||
)
|
||
_HAIR_HIGHLIGHT_PATTERNS = re.compile(
|
||
r"\b(hair|strand|fringe|bangs?|crown)\b.*\b(highlight|shine|sheen|glow|band|halo|stripe|bar)\b|"
|
||
r"\b(highlight|shine|sheen|glow|band|halo|stripe|bar)\b.*\b(hair|strand|fringe|bangs?|crown)\b",
|
||
re.IGNORECASE,
|
||
)
|
||
_HAIR_HIGHLIGHT_SAFE_REWRITE = (
|
||
"broken, semi-transparent satin sheen painted directly across selected hair strands, "
|
||
"following the curved hair volume, with irregular tapered edges and variable opacity"
|
||
)
|
||
_LITERAL_SHAPE_NEGATIVE_GUARDS: tuple[str, ...] = (
|
||
"solid white stripe",
|
||
"continuous luminous band",
|
||
"geometric zigzag bar",
|
||
"uniform horizontal block",
|
||
)
|
||
|
||
# The provider still returns a free-form review, but fidelity is deliberately
|
||
# recomputed on the server from a stable set of dimensions. This keeps scores
|
||
# comparable across providers and prevents a visually attractive image from
|
||
# receiving a high score while missing the actual line, face or shading grammar.
|
||
STYLE_REVIEW_SCHEMA_VERSION = 2
|
||
STYLE_REVIEW_DIMENSIONS: tuple[str, ...] = (
|
||
"face_structure",
|
||
"eye_design",
|
||
"hair_rendering",
|
||
"linework",
|
||
"palette_global",
|
||
"palette_local",
|
||
"shading",
|
||
"highlights",
|
||
"anatomy_scale",
|
||
"texture_detail",
|
||
"background_rendering",
|
||
)
|
||
STYLE_REVIEW_DIMENSION_ALIASES: dict[str, tuple[str, ...]] = {
|
||
"face_structure": ("face", "face_proportions", "facial_structure", "chin", "mouth_nose"),
|
||
"eye_design": ("eyes", "eye_geometry", "iris", "iris_construction", "eyelashes"),
|
||
"hair_rendering": ("hair", "hair_strands", "hair_mass", "strand_grouping"),
|
||
"linework": ("lines", "line_art", "lineart", "outline", "line_topology"),
|
||
"palette_global": ("palette", "color", "colour", "global_palette", "value_range", "saturation"),
|
||
"palette_local": ("local_color", "local_colour", "local_contrast", "color_separation", "colour_separation"),
|
||
"shading": ("shadow", "shadows", "shadow_rendering", "cel_shading"),
|
||
"highlights": ("highlight", "highlight_shape", "specular"),
|
||
"anatomy_scale": ("anatomy", "body_ratio", "head_to_body_ratio", "proportions"),
|
||
"texture_detail": ("texture", "detail", "detail_density", "rendering_detail"),
|
||
"background_rendering": ("background", "scene_rendering", "environment", "optical_effects"),
|
||
}
|
||
STYLE_REVIEW_WEIGHTS: dict[str, dict[str, float]] = {
|
||
"portrait": {
|
||
"face_structure": 0.20, "eye_design": 0.18, "hair_rendering": 0.11, "linework": 0.16,
|
||
"palette_global": 0.08, "palette_local": 0.07, "shading": 0.09, "highlights": 0.06,
|
||
"anatomy_scale": 0.02, "texture_detail": 0.03, "background_rendering": 0.00,
|
||
},
|
||
"fullbody": {
|
||
"face_structure": 0.08, "eye_design": 0.06, "hair_rendering": 0.09, "linework": 0.19,
|
||
"palette_global": 0.10, "palette_local": 0.09, "shading": 0.13, "highlights": 0.05,
|
||
"anatomy_scale": 0.14, "texture_detail": 0.06, "background_rendering": 0.01,
|
||
},
|
||
"novel_scene": {
|
||
"face_structure": 0.08, "eye_design": 0.06, "hair_rendering": 0.08, "linework": 0.18,
|
||
"palette_global": 0.12, "palette_local": 0.09, "shading": 0.12, "highlights": 0.06,
|
||
"anatomy_scale": 0.07, "texture_detail": 0.07, "background_rendering": 0.07,
|
||
},
|
||
}
|
||
|
||
DEFAULT_VALIDATION_CASES: tuple[dict[str, Any], ...] = (
|
||
{
|
||
"case_key": "portrait",
|
||
"content_prompt": (
|
||
"a new fictional heroine with short silver hair and teal eyes wearing a simple navy jacket, "
|
||
"head-and-shoulders portrait against a neutral matte studio background with even diffuse lighting, "
|
||
"with a distinct identity and wardrobe; keep subject age band neutral unless the user instruction specifies it"
|
||
),
|
||
"aspect_ratio": "1:1",
|
||
},
|
||
{
|
||
"case_key": "fullbody",
|
||
"content_prompt": (
|
||
"the same new fictional heroine with short silver hair and teal eyes wearing the same simple navy jacket, "
|
||
"complete head-to-toe standing pose in neutral daylight, natural stylized anime proportions about 5.5 to 6.5 "
|
||
"heads tall with slender limbs, not chibi and not super-deformed, with a distinct identity and wardrobe"
|
||
),
|
||
"aspect_ratio": "2:3",
|
||
},
|
||
{
|
||
"case_key": "novel_scene",
|
||
"content_prompt": (
|
||
"the same new fictional heroine with short silver hair, teal eyes and navy jacket walking through an ordinary town square "
|
||
"during overcast daytime, wide environmental view with diffuse light, clear depth and a scene unrelated to the source samples"
|
||
),
|
||
"aspect_ratio": "16:9",
|
||
},
|
||
)
|
||
|
||
|
||
class AdvancedStyleSetService:
|
||
"""Coordinate style-set work while reusing the advanced-material runtime."""
|
||
|
||
def __init__(self, owner: Any):
|
||
self.owner = owner
|
||
self.repository = owner.repository
|
||
self.material_repository = owner.material_repository
|
||
self.queue_service = owner.queue_service
|
||
self.audit_service = owner.audit_service
|
||
self.storage = owner.storage
|
||
self.normalization_service = owner.normalization_service
|
||
self.model_provider_runtime = owner.model_provider_runtime
|
||
self.single_flow_engine = owner.single_flow_engine
|
||
|
||
@staticmethod
|
||
def is_style_set(record: Any) -> bool:
|
||
metadata = record.metadata if isinstance(getattr(record, "metadata", None), dict) else {}
|
||
return str(metadata.get("workflow_kind") or "").strip() == STYLE_SET_KIND
|
||
|
||
def upload(self, payload: dict[str, Any], ctx: RequestContext) -> dict[str, Any]:
|
||
category_id = _optional_text(payload.get("category_id"))
|
||
category = self.material_repository.get_category(category_id) if category_id else None
|
||
if category is None:
|
||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "material category not found", {"category_id": category_id})
|
||
if not _is_style_category(category):
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"multi-image advanced imports are only available for the style category",
|
||
{"category_id": category_id, "category_name": category.name, "required_category": "风格"},
|
||
)
|
||
images = payload.get("images")
|
||
if not isinstance(images, list) or not MIN_STYLE_SOURCES <= len(images) <= MAX_STYLE_SOURCES:
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"advanced style sets require between 2 and 8 source images",
|
||
{"field": "images", "min_items": MIN_STYLE_SOURCES, "max_items": MAX_STYLE_SOURCES},
|
||
)
|
||
if any(not isinstance(item, dict) for item in images):
|
||
raise ApiError(ErrorCode.VALIDATION_FAILED, "every style source must be an object", {"field": "images"})
|
||
|
||
import_id = f"advimp_{uuid4().hex}"
|
||
display_name = str(payload.get("display_name") or "进阶画风组").strip() or "进阶画风组"
|
||
upload_metadata = _normalize_upload_metadata(payload.get("metadata"))
|
||
recognition_route = _optional_text(payload.get("recognition_model_route")) or self.owner._recognition_route()
|
||
generation_route = _optional_text(payload.get("generation_model_route")) or self.owner._generation_route()
|
||
persisted: list[dict[str, Any]] = []
|
||
try:
|
||
for index, image in enumerate(images):
|
||
source_id = f"advsrc_{uuid4().hex}"
|
||
filename = _normalize_upload_filename(image.get("filename"))
|
||
uri, detected_type, size_bytes, original_filename = self.owner._persist_source(
|
||
import_id=f"{import_id}/{source_id}",
|
||
image_data_url=str(image.get("image_data_url") or "").strip(),
|
||
image_uri=str(image.get("image_uri") or "").strip(),
|
||
filename=filename,
|
||
declared_content_type=_normalize_upload_content_type(image.get("content_type")),
|
||
declared_size=_normalize_upload_size(image),
|
||
)
|
||
persisted.append(
|
||
{
|
||
"source_id": source_id,
|
||
"uri": uri,
|
||
"filename": original_filename,
|
||
"content_type": detected_type,
|
||
"size_bytes": size_bytes,
|
||
"sort_order": index,
|
||
"is_primary": index == 0,
|
||
"upload_source": str(image.get("upload_source") or "file"),
|
||
}
|
||
)
|
||
|
||
record, sources = self.repository.create_style_import_with_sources(
|
||
{
|
||
"import_id": import_id,
|
||
"category_id": category_id,
|
||
"display_name": display_name,
|
||
"source_image_uri": persisted[0]["uri"],
|
||
"source_thumbnail_uri": persisted[0]["uri"],
|
||
"status": "style_source_recognition_queued",
|
||
"recognition_model_route": recognition_route,
|
||
"prompt_revision": 0,
|
||
"created_by": ctx.actor_id,
|
||
"metadata": {
|
||
"workflow_kind": STYLE_SET_KIND,
|
||
"style_source_count": len(persisted),
|
||
"generation_model_route": generation_route,
|
||
"governance_context": {"user_id": ctx.actor_id, "entrypoint": "web"},
|
||
"material_tier": "advanced_pending",
|
||
"upload": {**upload_metadata, "storage_root": "advanced_materials", "source": "style_set"},
|
||
},
|
||
},
|
||
[
|
||
{
|
||
"source_id": item["source_id"],
|
||
"import_id": import_id,
|
||
"sort_order": item["sort_order"],
|
||
"is_primary": item["is_primary"],
|
||
"source_image_uri": item["uri"],
|
||
"source_thumbnail_uri": item["uri"],
|
||
"status": "queued",
|
||
"recognition_model_route": recognition_route,
|
||
"metadata": {
|
||
"upload": {
|
||
"filename": item["filename"],
|
||
"content_type": item["content_type"],
|
||
"size_bytes": item["size_bytes"],
|
||
"source": item["upload_source"],
|
||
},
|
||
"retry_count": 0,
|
||
},
|
||
}
|
||
for item in persisted
|
||
],
|
||
)
|
||
except Exception:
|
||
for item in persisted:
|
||
self.owner._remove_uri(str(item.get("uri") or ""))
|
||
raise
|
||
|
||
tasks = []
|
||
for source in sources:
|
||
try:
|
||
tasks.append(self._queue_source(record, source, ctx))
|
||
except Exception as exc:
|
||
self.repository.update_style_source(
|
||
source.source_id,
|
||
{"status": "failed", "error": _safe_failure(exc)},
|
||
)
|
||
# If task creation itself failed for every source there will be no
|
||
# worker callback to advance the parent record. Running the barrier
|
||
# here is harmless while any source is still queued and prevents a
|
||
# permanently busy import when all queue writes failed.
|
||
self._after_source_terminal(import_id, ctx)
|
||
current = self.owner._require_import(import_id)
|
||
self.audit_service.record(
|
||
ctx=ctx,
|
||
action="upload_advanced_style_set",
|
||
target_type="advanced_material_import",
|
||
target_id=import_id,
|
||
after_snapshot=current.to_dict(),
|
||
metadata={"source_count": len(sources), "task_ids": [task.task_id for task in tasks]},
|
||
)
|
||
return {
|
||
"import": current.to_dict(),
|
||
"sources": [item.to_dict() for item in self.repository.list_style_sources(import_id)],
|
||
"tasks": [task.to_dict() for task in tasks],
|
||
"queued": bool(tasks),
|
||
}
|
||
|
||
def detail(self, record: Any) -> dict[str, Any]:
|
||
record = self.reconcile_missing_tasks(record)
|
||
sources = self.repository.list_style_sources(record.import_id)
|
||
consensuses = self.repository.list_style_consensus_runs(record.import_id)
|
||
attempts = self.repository.list_attempts(record.import_id)
|
||
cases = (
|
||
self.repository.list_style_validation_cases(record.current_attempt_id)
|
||
if record.current_attempt_id
|
||
else []
|
||
)
|
||
return {
|
||
"style_set": True,
|
||
"sources": [item.to_dict() for item in sources],
|
||
"consensus": consensuses[0].to_dict() if consensuses else None,
|
||
"consensus_history": [item.to_dict() for item in consensuses],
|
||
"validation_cases": [item.to_dict() for item in cases],
|
||
"attempts": [item.to_dict() for item in attempts],
|
||
}
|
||
|
||
def reconcile_missing_tasks(self, record: Any) -> Any:
|
||
"""Turn dangling active child rows into explicit retryable failures.
|
||
|
||
Queue retention and manual queue deletion legitimately remove terminal
|
||
task rows. If a split write left a child in queued/running state, the
|
||
task foreign key becomes NULL and the style workbench used to remain
|
||
busy forever. Reconciliation is deliberately side-effect free with
|
||
respect to providers: it never enqueues new work.
|
||
"""
|
||
|
||
import_id = record.import_id
|
||
missing_sources: list[str] = []
|
||
for source in self.repository.list_style_sources(import_id):
|
||
if source.status not in STYLE_SOURCE_EXECUTABLE or not self._task_missing(source.task_id):
|
||
continue
|
||
failure = _missing_queue_task_failure(source.task_id, "style_source_recognition")
|
||
self.repository.update_style_source(
|
||
source.source_id,
|
||
{"status": "failed", "task_id": None, "error": failure},
|
||
)
|
||
missing_sources.append(source.source_id)
|
||
|
||
missing_consensus: list[str] = []
|
||
for consensus in self.repository.list_style_consensus_runs(import_id):
|
||
if consensus.status not in STYLE_CONSENSUS_EXECUTABLE or not self._task_missing(consensus.task_id):
|
||
continue
|
||
failure = _missing_queue_task_failure(consensus.task_id, "style_consensus")
|
||
self.repository.update_style_consensus_run(
|
||
consensus.consensus_id,
|
||
{"status": "failed", "task_id": None, "error": failure},
|
||
expected_statuses=STYLE_CONSENSUS_EXECUTABLE,
|
||
)
|
||
missing_consensus.append(consensus.consensus_id)
|
||
|
||
affected_attempts: set[str] = set()
|
||
for attempt in self.repository.list_attempts(import_id):
|
||
for case in self.repository.list_style_validation_cases(attempt.attempt_id):
|
||
if case.status not in STYLE_CASE_EXECUTABLE or not self._task_missing(case.task_id):
|
||
continue
|
||
failure = _missing_queue_task_failure(case.task_id, "style_validation_generation")
|
||
self.repository.update_style_validation_case(
|
||
case.case_id,
|
||
{"status": "failed", "task_id": None, "error": failure},
|
||
expected_statuses=STYLE_CASE_EXECUTABLE,
|
||
)
|
||
affected_attempts.add(attempt.attempt_id)
|
||
for attempt_id in affected_attempts:
|
||
self.repository.finalize_style_validation_batch_if_ready(import_id, attempt_id)
|
||
|
||
if missing_sources:
|
||
record = self.repository.update_import(
|
||
import_id,
|
||
{
|
||
"status": "style_source_review_required",
|
||
"error": {
|
||
"error_type": "queue_task_missing",
|
||
"stage": "style_source_recognition",
|
||
"source_ids": missing_sources,
|
||
},
|
||
},
|
||
) or record
|
||
elif missing_consensus:
|
||
record = self.repository.update_import(
|
||
import_id,
|
||
{
|
||
"status": "style_consensus_failed",
|
||
"error": {
|
||
"error_type": "queue_task_missing",
|
||
"stage": "style_consensus",
|
||
"consensus_ids": missing_consensus,
|
||
},
|
||
},
|
||
) or record
|
||
elif affected_attempts:
|
||
record = self.repository.get_import(import_id) or record
|
||
return record
|
||
|
||
def _task_missing(self, task_id: str | None) -> bool:
|
||
if not str(task_id or "").strip():
|
||
return True
|
||
try:
|
||
self.queue_service.get_job(str(task_id))
|
||
except ApiError:
|
||
return True
|
||
return False
|
||
|
||
def retry_source(self, import_id: str, source_id: str, ctx: RequestContext) -> dict[str, Any]:
|
||
record = self.owner._require_import(import_id)
|
||
self._require_style_set(record)
|
||
self._require_sources_editable(record)
|
||
source = self._require_source(import_id, source_id)
|
||
if source.status not in {"failed", "excluded", "cancelled"}:
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"style source is not retryable",
|
||
{"source_id": source_id, "status": source.status},
|
||
)
|
||
retry_count = int(source.metadata.get("retry_count") or 0) + 1
|
||
# Failed source tasks persist the route that was active when the style
|
||
# set was uploaded. A configuration repair must take effect when the
|
||
# user explicitly retries; otherwise an obsolete route such as
|
||
# ``material_vlm`` is frozen forever and every retry repeats the same
|
||
# pre-provider failure. Keep normal first-run tasks immutable, but
|
||
# refresh both the child and parent route at this explicit retry
|
||
# boundary so the later consensus stage uses the same repaired chain.
|
||
refreshed_route = self.owner._recognition_route()
|
||
retry_route = (
|
||
refreshed_route
|
||
or source.recognition_model_route
|
||
or record.recognition_model_route
|
||
)
|
||
if retry_route and retry_route != record.recognition_model_route:
|
||
record = self.repository.update_import(
|
||
import_id,
|
||
{"recognition_model_route": retry_route},
|
||
) or record
|
||
previous_task_id = _optional_text(source.task_id)
|
||
source = self.repository.update_style_source(
|
||
source_id,
|
||
{
|
||
"status": "queued",
|
||
"task_id": None,
|
||
"error": None,
|
||
"excluded_at": None,
|
||
"recognition_model_route": retry_route,
|
||
"metadata": {**source.metadata, "retry_count": retry_count},
|
||
},
|
||
)
|
||
try:
|
||
task = self._queue_source(record, source, ctx)
|
||
except Exception as exc:
|
||
self.repository.update_style_source(
|
||
source_id,
|
||
{"status": "failed", "task_id": None, "error": _safe_failure(exc)},
|
||
)
|
||
self._after_source_terminal(import_id, ctx)
|
||
raise
|
||
self._mark_retried_task_superseded(previous_task_id, task.task_id, ctx)
|
||
return {"import": self.owner._require_import(import_id).to_dict(), "source": source.to_dict(), "task": task.to_dict()}
|
||
|
||
def exclude_source(
|
||
self,
|
||
import_id: str,
|
||
source_id: str,
|
||
excluded: bool,
|
||
ctx: RequestContext,
|
||
) -> dict[str, Any]:
|
||
record = self.owner._require_import(import_id)
|
||
self._require_style_set(record)
|
||
self._require_sources_editable(record)
|
||
source = self._require_source(import_id, source_id)
|
||
if excluded:
|
||
if source.is_primary:
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"the primary style source cannot be excluded; choose another primary before upload",
|
||
{"source_id": source_id},
|
||
)
|
||
remaining = [item for item in self.repository.list_style_sources(import_id) if item.source_id != source_id and item.status != "excluded"]
|
||
if len(remaining) < MIN_STYLE_SOURCES:
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"at least two style sources must remain active",
|
||
{"min_sources": MIN_STYLE_SOURCES},
|
||
)
|
||
if source.task_id:
|
||
try:
|
||
self.queue_service.cancel_job(source.task_id, ctx, reason="style source excluded")
|
||
except ApiError:
|
||
pass
|
||
updated = self.repository.update_style_source(
|
||
source_id,
|
||
{
|
||
"status": "excluded",
|
||
"metadata": {**source.metadata, "status_before_exclusion": source.status},
|
||
},
|
||
)
|
||
else:
|
||
restored = str(source.metadata.get("status_before_exclusion") or "").strip()
|
||
if source.recognition_result:
|
||
restored = "succeeded"
|
||
elif restored not in {"failed", "cancelled"}:
|
||
restored = "failed"
|
||
updated = self.repository.update_style_source(
|
||
source_id,
|
||
{"status": restored, "excluded_at": None},
|
||
)
|
||
self.repository.update_import(import_id, {"status": "style_source_review_required"})
|
||
return {"import": self.owner._require_import(import_id).to_dict(), "source": updated.to_dict()}
|
||
|
||
def create_consensus(
|
||
self,
|
||
import_id: str,
|
||
payload: dict[str, Any],
|
||
ctx: RequestContext,
|
||
) -> dict[str, Any]:
|
||
record = self.owner._require_import(import_id)
|
||
self._require_style_set(record)
|
||
if record.status in {"accepted", "archived", "cancelled"}:
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"advanced style set no longer accepts consensus work",
|
||
{"import_id": import_id, "status": record.status},
|
||
)
|
||
if record.current_attempt_id:
|
||
current_attempt = self.repository.get_attempt(record.current_attempt_id)
|
||
if current_attempt is not None and current_attempt.status in STYLE_ATTEMPT_BLOCKING:
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"finish or retry the current style validation batch before creating another consensus",
|
||
{
|
||
"import_id": import_id,
|
||
"attempt_id": current_attempt.attempt_id,
|
||
"attempt_status": current_attempt.status,
|
||
},
|
||
)
|
||
latest = self.repository.get_latest_style_consensus_run(import_id)
|
||
if latest is not None and latest.status in STYLE_CONSENSUS_EXECUTABLE:
|
||
task = None
|
||
if latest.task_id:
|
||
try:
|
||
task = self.queue_service.get_job(latest.task_id)
|
||
except ApiError:
|
||
task = None
|
||
if task is not None and task.status in {"queued", "running"}:
|
||
return {"import": record.to_dict(), "consensus": latest.to_dict(), "task": task.to_dict()}
|
||
# Repair a split write (child row says active but its queue record
|
||
# is absent/terminal) before attempting the next revision. The
|
||
# partial unique index otherwise rejects every future retry.
|
||
stale_error = {
|
||
"error_type": "missing_or_terminal_consensus_task",
|
||
"task_id": latest.task_id,
|
||
"task_status": getattr(task, "status", None),
|
||
}
|
||
latest = self.repository.update_style_consensus_run(
|
||
latest.consensus_id,
|
||
{"status": "failed", "error": stale_error},
|
||
expected_statuses=STYLE_CONSENSUS_EXECUTABLE,
|
||
) or latest
|
||
|
||
sources = self.repository.list_style_sources(import_id, include_excluded=False)
|
||
active = [item for item in sources if item.status not in STYLE_SOURCE_TERMINAL]
|
||
succeeded = [item for item in sources if item.status == "succeeded"]
|
||
if active:
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"style source recognition is still running",
|
||
{"active_source_ids": [item.source_id for item in active]},
|
||
)
|
||
if len(succeeded) < MIN_STYLE_SOURCES:
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"at least two successfully recognized style sources are required",
|
||
{"succeeded": len(succeeded), "min_sources": MIN_STYLE_SOURCES},
|
||
)
|
||
revision = int(latest.revision if latest else 0) + 1
|
||
legacy_adjustment = _optional_text(payload.get("adjustment_text"))
|
||
style_adjustment = _optional_text(payload.get("style_adjustment_text")) or legacy_adjustment
|
||
validation_instruction = _optional_text(payload.get("validation_instruction"))
|
||
reuse_cases = payload.get("reuse_validation_cases") if not validation_instruction else []
|
||
style_adjustment_plan = _style_adjustment_plan(payload.get("style_adjustment_plan"), style_adjustment)
|
||
analysis_strategy = _optional_text(payload.get("analysis_strategy")) or "text_only"
|
||
if analysis_strategy not in {"text_only", "vision_assisted"}:
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"analysis_strategy must be text_only or vision_assisted",
|
||
{"analysis_strategy": analysis_strategy},
|
||
)
|
||
consensus = self.repository.create_style_consensus_run(
|
||
{
|
||
"consensus_id": f"advconsensus_{uuid4().hex}",
|
||
"import_id": import_id,
|
||
"revision": revision,
|
||
"status": "queued",
|
||
"model_route": self.owner._consensus_route(),
|
||
"source_ids": [item.source_id for item in succeeded],
|
||
"metadata": {
|
||
"adjustment_text": style_adjustment,
|
||
"style_adjustment_text": style_adjustment,
|
||
"validation_instruction": validation_instruction,
|
||
"reuse_validation_cases": reuse_cases if isinstance(reuse_cases, list) else [],
|
||
"style_adjustment_plan": style_adjustment_plan,
|
||
"analysis_strategy": analysis_strategy,
|
||
"comparison_attempt_id": _optional_text(payload.get("comparison_attempt_id")),
|
||
"comparison_consensus_id": _optional_text(payload.get("comparison_consensus_id")),
|
||
"comparison_source_ids": _string_list(payload.get("comparison_source_ids"))[:3],
|
||
**_validation_reference_metadata_from_payload(payload),
|
||
"requested_by": ctx.actor_id,
|
||
},
|
||
}
|
||
)
|
||
try:
|
||
task = self.queue_service.create_task(
|
||
{
|
||
"task_type": "advanced_style_consensus",
|
||
"status": "queued",
|
||
"model_route": consensus.model_route,
|
||
"idempotency_key": f"advanced-style-consensus:{import_id}:{revision}",
|
||
"request": {"import_id": import_id, "consensus_id": consensus.consensus_id, "operation": "style_consensus"},
|
||
"metadata": {
|
||
"source": "advanced_material",
|
||
"advanced_material_import_id": import_id,
|
||
"advanced_style_consensus_id": consensus.consensus_id,
|
||
"stage": "style_consensus",
|
||
"task_summary": f"进阶画风/{record.display_name}/第{revision}版交叉分析",
|
||
},
|
||
},
|
||
ctx,
|
||
)
|
||
consensus = self.repository.update_style_consensus_run(consensus.consensus_id, {"task_id": task.task_id}) or consensus
|
||
self.repository.update_import(
|
||
import_id,
|
||
{
|
||
"status": "style_consensus_queued",
|
||
"error": None,
|
||
"metadata": {**record.metadata, "current_consensus_id": consensus.consensus_id},
|
||
},
|
||
)
|
||
except Exception as exc:
|
||
failure = _safe_failure(exc)
|
||
self.repository.update_style_consensus_run(
|
||
consensus.consensus_id,
|
||
{"status": "failed", "error": failure},
|
||
expected_statuses=STYLE_CONSENSUS_EXECUTABLE,
|
||
)
|
||
self.repository.update_import(
|
||
import_id,
|
||
{
|
||
"status": "style_consensus_failed",
|
||
"error": failure,
|
||
"metadata": {**record.metadata, "current_consensus_id": consensus.consensus_id},
|
||
},
|
||
)
|
||
raise
|
||
return {"import": self.owner._require_import(import_id).to_dict(), "consensus": consensus.to_dict(), "task": task.to_dict()}
|
||
|
||
def retry_validation_case(self, import_id: str, case_id: str, ctx: RequestContext) -> dict[str, Any]:
|
||
record = self.owner._require_import(import_id)
|
||
self._require_style_set(record)
|
||
case = self._require_case(import_id, case_id)
|
||
if case.status not in {"failed", "cancelled"}:
|
||
raise ApiError(ErrorCode.VALIDATION_FAILED, "validation case is not retryable", {"case_id": case_id, "status": case.status})
|
||
retry_count = int(case.metadata.get("retry_count") or 0) + 1
|
||
previous_task_id = _optional_text(case.task_id)
|
||
case = self.repository.update_style_validation_case(
|
||
case_id,
|
||
{
|
||
"status": "queued",
|
||
"task_id": None,
|
||
"error": None,
|
||
"metadata": {**case.metadata, "retry_count": retry_count},
|
||
},
|
||
)
|
||
try:
|
||
task = self._queue_validation_case(record, case, ctx)
|
||
except Exception as exc:
|
||
self.repository.update_style_validation_case(
|
||
case_id,
|
||
{"status": "failed", "task_id": None, "error": _safe_failure(exc)},
|
||
expected_statuses={"queued"},
|
||
)
|
||
self.repository.finalize_style_validation_batch_if_ready(import_id, case.attempt_id)
|
||
raise
|
||
attempt = self.repository.update_attempt(case.attempt_id, {"status": "running", "error": None})
|
||
self.repository.update_import(import_id, {"status": "validating", "error": None})
|
||
self._mark_retried_task_superseded(previous_task_id, task.task_id, ctx)
|
||
return {"import": self.owner._require_import(import_id).to_dict(), "attempt": attempt.to_dict(), "case": case.to_dict(), "task": task.to_dict()}
|
||
|
||
def _mark_retried_task_superseded(self, previous_task_id: str | None, replacement_task_id: str, ctx: RequestContext) -> None:
|
||
previous_id = _optional_text(previous_task_id)
|
||
if not previous_id or previous_id == replacement_task_id:
|
||
return
|
||
try:
|
||
previous = self.queue_service.get_job(previous_id)
|
||
lineage_key = self._retry_lineage_key(previous)
|
||
candidate_ids = {previous_id}
|
||
if lineage_key is not None:
|
||
for status in ("failed", "cancelled", "dead_letter"):
|
||
for candidate in self.queue_service.list_jobs(status=status, page=1, page_size=100).items:
|
||
if self._retry_lineage_key(candidate) == lineage_key:
|
||
candidate_ids.add(candidate.task_id)
|
||
for candidate_id in candidate_ids:
|
||
self._mark_task_superseded(candidate_id, replacement_task_id, ctx)
|
||
except Exception:
|
||
return
|
||
|
||
def _retry_lineage_key(self, task: Any) -> tuple[str, str] | None:
|
||
metadata = task.metadata if isinstance(getattr(task, "metadata", None), dict) else {}
|
||
source_id = _optional_text(metadata.get("advanced_style_source_id"))
|
||
case_id = _optional_text(metadata.get("advanced_style_validation_case_id"))
|
||
if task.task_type == "advanced_style_source_recognition" and source_id:
|
||
return ("source", source_id)
|
||
if task.task_type == "advanced_style_validation_generation" and case_id:
|
||
return ("validation", case_id)
|
||
return None
|
||
|
||
def _mark_task_superseded(self, task_id: str, replacement_task_id: str, ctx: RequestContext) -> None:
|
||
before = self.queue_service.get_job(task_id)
|
||
metadata = {
|
||
**before.metadata,
|
||
"superseded_by": replacement_task_id,
|
||
"retry_execution": {
|
||
**dict(before.metadata.get("retry_execution") or {}),
|
||
"status": "superseded",
|
||
"replacement_task_id": replacement_task_id,
|
||
"superseded_at": _now(),
|
||
},
|
||
}
|
||
retrying = self.queue_service.update_task_if_status(
|
||
task_id,
|
||
{"failed", "cancelled", "dead_letter"},
|
||
{"status": "retrying", "metadata": metadata},
|
||
ctx,
|
||
)
|
||
if retrying is not None:
|
||
self.queue_service.update_task_if_status(
|
||
task_id,
|
||
{"retrying"},
|
||
{"status": "superseded", "metadata": metadata},
|
||
ctx,
|
||
)
|
||
|
||
def ensure_validation(
|
||
self,
|
||
record: Any,
|
||
payload: dict[str, Any],
|
||
ctx: RequestContext,
|
||
) -> dict[str, Any]:
|
||
"""Return the active batch or create it from the latest consensus."""
|
||
|
||
self._require_style_set(record)
|
||
if record.current_attempt_id:
|
||
attempt = self.repository.get_attempt(record.current_attempt_id)
|
||
cases = self.repository.list_style_validation_cases(record.current_attempt_id)
|
||
if attempt is not None and cases and attempt.status in {
|
||
"queued",
|
||
"running",
|
||
"validation_partial_failed",
|
||
"awaiting_review",
|
||
}:
|
||
tasks = []
|
||
for item in cases:
|
||
if not item.task_id:
|
||
continue
|
||
try:
|
||
tasks.append(self.queue_service.get_job(item.task_id).to_dict())
|
||
except ApiError:
|
||
# The persisted case state remains authoritative. A
|
||
# missing old queue row must not make the detail/retry
|
||
# endpoint unusable after queue retention cleanup.
|
||
continue
|
||
return {
|
||
"import": record.to_dict(),
|
||
"attempt": attempt.to_dict(),
|
||
"validation_cases": [item.to_dict() for item in cases],
|
||
"tasks": tasks,
|
||
}
|
||
adjustment = _optional_text(payload.get("adjustment_text"))
|
||
if adjustment:
|
||
return self.create_consensus(
|
||
record.import_id,
|
||
{"adjustment_text": adjustment},
|
||
ctx,
|
||
)
|
||
consensus = self.repository.get_latest_style_consensus_run(record.import_id)
|
||
if consensus is None or consensus.status != "succeeded":
|
||
return self.create_consensus(record.import_id, {}, ctx)
|
||
attempt, cases, tasks = self._queue_validation_batch(record, consensus, ctx)
|
||
return {
|
||
"import": self.owner._require_import(record.import_id).to_dict(),
|
||
"attempt": attempt.to_dict(),
|
||
"validation_cases": [item.to_dict() for item in cases],
|
||
"tasks": [item.to_dict() for item in tasks],
|
||
}
|
||
|
||
def execute_task(self, task: Any, ctx: RequestContext) -> WorkerTaskUpdate:
|
||
if task.task_type == "advanced_style_source_recognition":
|
||
return self._execute_source(task, ctx)
|
||
if task.task_type == "advanced_style_consensus":
|
||
return self._execute_consensus(task, ctx)
|
||
if task.task_type == "advanced_style_validation_generation":
|
||
return self._execute_validation_case(task, ctx)
|
||
raise RuntimeError(f"unsupported advanced style task type: {task.task_type}")
|
||
|
||
def handle_failure(self, task: Any, exc: Exception, ctx: RequestContext) -> None:
|
||
import_id = _optional_text(task.metadata.get("advanced_material_import_id"))
|
||
if not import_id:
|
||
return
|
||
record = self.repository.get_import(import_id)
|
||
if record is None or record.status in {"accepted", "archived", "cancelled"}:
|
||
return
|
||
failure = _safe_failure(exc)
|
||
handled = False
|
||
if task.task_type == "advanced_style_source_recognition":
|
||
source_id = _optional_text(task.metadata.get("advanced_style_source_id"))
|
||
if source_id:
|
||
source = self.repository.get_style_source(source_id)
|
||
if source is not None and source.import_id == import_id and source.task_id == task.task_id:
|
||
changed = self.repository.update_style_source_if_status(
|
||
source_id,
|
||
STYLE_SOURCE_EXECUTABLE,
|
||
{"status": "failed", "error": failure},
|
||
)
|
||
if changed is not None:
|
||
handled = True
|
||
self._after_source_terminal(import_id, ctx)
|
||
elif task.task_type == "advanced_style_consensus":
|
||
consensus_id = _optional_text(task.metadata.get("advanced_style_consensus_id"))
|
||
if consensus_id:
|
||
consensus = self.repository.get_style_consensus_run(consensus_id)
|
||
if consensus is not None and consensus.import_id == import_id and consensus.task_id == task.task_id:
|
||
changed = self.repository.update_style_consensus_run(
|
||
consensus_id,
|
||
{"status": "failed", "error": failure},
|
||
expected_statuses=STYLE_CONSENSUS_EXECUTABLE,
|
||
)
|
||
if changed is not None:
|
||
handled = True
|
||
self.repository.update_import(import_id, {"status": "style_consensus_failed", "error": failure})
|
||
elif task.task_type == "advanced_style_validation_generation":
|
||
case_id = _optional_text(task.metadata.get("advanced_style_validation_case_id"))
|
||
if case_id:
|
||
case = self.repository.get_style_validation_case(case_id)
|
||
if case is not None and case.import_id == import_id and case.task_id == task.task_id:
|
||
changed = self.repository.update_style_validation_case(
|
||
case_id,
|
||
{"status": "failed", "error": failure},
|
||
expected_statuses=STYLE_CASE_EXECUTABLE,
|
||
)
|
||
if changed is not None:
|
||
handled = True
|
||
self.repository.finalize_style_validation_batch_if_ready(import_id, case.attempt_id)
|
||
if not handled:
|
||
# A stale worker/result must not overwrite a later retry,
|
||
# exclusion, cancellation or already-published success.
|
||
return
|
||
self.audit_service.record(
|
||
ctx=ctx,
|
||
action="advanced_style_task_failed",
|
||
target_type="advanced_material_import",
|
||
target_id=import_id,
|
||
metadata={"task_id": task.task_id, "task_type": task.task_type, "failure": failure},
|
||
)
|
||
|
||
def reject_and_rebuild(
|
||
self,
|
||
record: Any,
|
||
attempt: Any,
|
||
payload: dict[str, Any],
|
||
ctx: RequestContext,
|
||
) -> dict[str, Any]:
|
||
cases = self.repository.list_style_validation_cases(attempt.attempt_id)
|
||
if record.current_attempt_id != attempt.attempt_id or record.status != "awaiting_review" or attempt.status != "awaiting_review":
|
||
raise ApiError(ErrorCode.VALIDATION_FAILED, "only the current completed style validation batch can be rejected")
|
||
legacy_adjustment = _optional_text(payload.get("adjustment_text"))
|
||
style_adjustment = _optional_text(payload.get("style_adjustment_text"))
|
||
validation_instruction = _optional_text(payload.get("validation_instruction"))
|
||
if legacy_adjustment and not style_adjustment and not validation_instruction:
|
||
# Backward compatibility: the legacy single text box did not say
|
||
# whether feedback targeted style or validation content. Apply it
|
||
# to both paths so it can no longer be silently discarded.
|
||
style_adjustment = legacy_adjustment
|
||
validation_instruction = legacy_adjustment
|
||
if not style_adjustment and not validation_instruction:
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"style_adjustment_text or validation_instruction is required",
|
||
{"fields": ["style_adjustment_text", "validation_instruction"]},
|
||
)
|
||
style_adjustment_plan = _parse_style_adjustment(style_adjustment)
|
||
comparison_consensus_id = _optional_text(attempt.metadata.get("style_consensus_id"))
|
||
comparison_consensus = (
|
||
self.repository.get_style_consensus_run(comparison_consensus_id)
|
||
if comparison_consensus_id
|
||
else None
|
||
)
|
||
comparison_source_ids = (
|
||
_string_list(comparison_consensus.result.get("representative_source_ids"))[:3]
|
||
if comparison_consensus is not None
|
||
else []
|
||
)
|
||
adjustment_summary = _review_adjustment_summary(style_adjustment, validation_instruction)
|
||
self.repository.update_attempt(
|
||
attempt.attempt_id,
|
||
{
|
||
"status": "rejected",
|
||
"decision": "rejected",
|
||
"adjustment_text": adjustment_summary,
|
||
"decision_notes": _optional_text(payload.get("decision_notes")) or adjustment_summary,
|
||
"fidelity_score": _optional_score(payload.get("fidelity_score")),
|
||
"metadata": {
|
||
**attempt.metadata,
|
||
"style_adjustment_text": style_adjustment,
|
||
"validation_instruction": validation_instruction,
|
||
"style_adjustment_plan": style_adjustment_plan,
|
||
"comparison_consensus_id": comparison_consensus_id,
|
||
"comparison_source_ids": comparison_source_ids,
|
||
"rejected_by": ctx.actor_id,
|
||
},
|
||
},
|
||
)
|
||
self.repository.update_import(record.import_id, {"status": "adjustment_required"})
|
||
reuse_cases = [] if validation_instruction else [
|
||
{"case_key": item.case_key, "content_prompt": item.content_prompt, "metadata": item.metadata}
|
||
for item in cases
|
||
]
|
||
rebuild_payload = {
|
||
"style_adjustment_text": style_adjustment,
|
||
"validation_instruction": validation_instruction,
|
||
"reuse_validation_cases": reuse_cases,
|
||
"style_adjustment_plan": style_adjustment_plan,
|
||
"comparison_attempt_id": attempt.attempt_id,
|
||
"comparison_consensus_id": comparison_consensus_id,
|
||
"comparison_source_ids": comparison_source_ids,
|
||
}
|
||
rebuild_payload.update(_validation_reference_metadata_from_payload(payload))
|
||
# Preserve previous reference selection when the client omits the fields.
|
||
if "validation_reference_enabled" not in payload and "validation_reference_source_id" not in payload:
|
||
prev_enabled = attempt.metadata.get("validation_reference_enabled")
|
||
prev_source = _optional_text(attempt.metadata.get("validation_reference_source_id"))
|
||
if prev_enabled is not None:
|
||
rebuild_payload["validation_reference_enabled"] = bool(prev_enabled)
|
||
if prev_source:
|
||
rebuild_payload["validation_reference_source_id"] = prev_source
|
||
elif prev_enabled is False:
|
||
rebuild_payload["validation_reference_enabled"] = False
|
||
return self.create_consensus(
|
||
record.import_id,
|
||
rebuild_payload,
|
||
ctx,
|
||
)
|
||
|
||
def accept(
|
||
self,
|
||
record: Any,
|
||
attempt: Any,
|
||
payload: dict[str, Any],
|
||
ctx: RequestContext,
|
||
) -> dict[str, Any]:
|
||
if record.accepted_material_id:
|
||
material = self.repository.get_material(record.accepted_material_id)
|
||
if material is not None:
|
||
return {"import": record.to_dict(), "attempt": attempt.to_dict(), "material": material.to_dict()}
|
||
cases = self.repository.list_style_validation_cases(attempt.attempt_id)
|
||
if (
|
||
record.current_attempt_id != attempt.attempt_id
|
||
or record.status != "awaiting_review"
|
||
or attempt.status != "awaiting_review"
|
||
or len(cases) != 3
|
||
or any(item.status != "succeeded" or not item.validation_image_uri for item in cases)
|
||
):
|
||
raise ApiError(ErrorCode.VALIDATION_FAILED, "all three current style validation cases must succeed before sealing")
|
||
consensus = self.repository.get_latest_style_consensus_run(record.import_id)
|
||
if consensus is None or consensus.status != "succeeded":
|
||
raise ApiError(ErrorCode.VALIDATION_FAILED, "the current style consensus is not complete")
|
||
expected_consensus = str(attempt.metadata.get("style_consensus_id") or "")
|
||
if expected_consensus and expected_consensus != consensus.consensus_id:
|
||
raise ApiError(ErrorCode.VALIDATION_FAILED, "validation batch does not belong to the latest style consensus")
|
||
sources = [item for item in self.repository.list_style_sources(record.import_id) if item.status == "succeeded"]
|
||
source_by_id = {item.source_id: item for item in sources}
|
||
result = consensus.result if isinstance(consensus.result, dict) else {}
|
||
representative_ids = [item for item in _string_list(result.get("representative_source_ids")) if item in source_by_id]
|
||
if not representative_ids:
|
||
representative_ids = [item.source_id for item in sources[:3]]
|
||
primary = next((item for item in sources if item.is_primary), sources[0])
|
||
capsules = result.get("generation_capsules") if isinstance(result.get("generation_capsules"), dict) else {}
|
||
profile = result.get("normalized_asset_profile") if isinstance(result.get("normalized_asset_profile"), dict) else {}
|
||
fidelity_score = _optional_score(payload.get("fidelity_score"))
|
||
decision_notes = _optional_text(payload.get("decision_notes"))
|
||
attempt = self.repository.update_attempt(
|
||
attempt.attempt_id,
|
||
{"fidelity_score": fidelity_score, "decision_notes": decision_notes},
|
||
) or attempt
|
||
validation_payload = [item.to_dict() for item in cases]
|
||
metadata = {
|
||
"library_kind": "advanced",
|
||
"priority_class": "advanced_verified",
|
||
"material_priority": 100,
|
||
"workflow_kind": STYLE_SET_KIND,
|
||
"style_set": {
|
||
"source_ids": [item.source_id for item in sources],
|
||
"source_image_uris": [item.source_image_uri for item in sources],
|
||
"representative_source_ids": representative_ids[:3],
|
||
"reference_source_uris": [source_by_id[item].source_image_uri for item in representative_ids[:3]],
|
||
"consensus_id": consensus.consensus_id,
|
||
"consensus_revision": consensus.revision,
|
||
"validation_cases": validation_payload,
|
||
},
|
||
"verification": {
|
||
"import_id": record.import_id,
|
||
"attempt_id": attempt.attempt_id,
|
||
"attempt_number": attempt.attempt_number,
|
||
"fidelity_score": fidelity_score,
|
||
"decision_notes": decision_notes,
|
||
"validation_image_uri": cases[0].validation_image_uri,
|
||
"prompt_revision": consensus.revision,
|
||
},
|
||
"recognition": result.get("recognition") or {},
|
||
"raw_analysis": result.get("raw_analysis") or {},
|
||
"normalized_asset_profile": {**profile, "category": "style", "role": "style", "body_slots": ["global_style"], "confirmed": True},
|
||
"generation_capsules": capsules,
|
||
"content_leakage_exclusions": result.get("content_leakage_exclusions") or [],
|
||
"confirmed": True,
|
||
}
|
||
material = self.repository.accept_attempt(
|
||
record.import_id,
|
||
attempt.attempt_id,
|
||
{
|
||
"material_id": f"advmat_{uuid4().hex}",
|
||
"category_id": record.category_id,
|
||
"display_name": record.display_name,
|
||
"source_image_uri": primary.source_image_uri,
|
||
"source_thumbnail_uri": primary.source_thumbnail_uri,
|
||
"validation_image_uri": cases[0].validation_image_uri,
|
||
"validation_thumbnail_uri": cases[0].validation_thumbnail_uri,
|
||
"prompt": consensus.prompt or record.recognition_prompt,
|
||
"negative_prompt": consensus.negative_prompt or record.recognition_negative_prompt,
|
||
"tags": consensus.tags,
|
||
"priority": 100,
|
||
"status": "active",
|
||
"capsule": capsules,
|
||
"decision_notes": decision_notes,
|
||
"metadata": metadata,
|
||
},
|
||
)
|
||
return {
|
||
"import": self.owner._require_import(record.import_id).to_dict(),
|
||
"attempt": self.owner._require_attempt(record.import_id, attempt.attempt_id).to_dict(),
|
||
"material": material.to_dict(),
|
||
}
|
||
|
||
def cancel(self, record: Any, ctx: RequestContext) -> None:
|
||
task_ids: set[str] = set()
|
||
for source in self.repository.list_style_sources(record.import_id):
|
||
if source.task_id and source.status in STYLE_SOURCE_EXECUTABLE:
|
||
task_ids.add(source.task_id)
|
||
self.repository.update_style_source(source.source_id, {"status": "cancelled"})
|
||
for consensus in self.repository.list_style_consensus_runs(record.import_id):
|
||
if consensus.task_id and consensus.status in STYLE_CONSENSUS_EXECUTABLE:
|
||
task_ids.add(consensus.task_id)
|
||
self.repository.update_style_consensus_run(consensus.consensus_id, {"status": "cancelled"})
|
||
for attempt in self.repository.list_attempts(record.import_id):
|
||
for case in self.repository.list_style_validation_cases(attempt.attempt_id):
|
||
if case.task_id and case.status in STYLE_CASE_EXECUTABLE:
|
||
task_ids.add(case.task_id)
|
||
self.repository.update_style_validation_case(case.case_id, {"status": "cancelled"})
|
||
for task_id in task_ids:
|
||
try:
|
||
self.queue_service.cancel_job(task_id, ctx, reason="advanced style set cancelled")
|
||
except ApiError:
|
||
pass
|
||
|
||
def _queue_source(self, record: Any, source: Any, ctx: RequestContext):
|
||
retry_count = int(source.metadata.get("retry_count") or 0)
|
||
task = self.queue_service.create_task(
|
||
{
|
||
"task_type": "advanced_style_source_recognition",
|
||
"status": "queued",
|
||
"model_route": source.recognition_model_route or record.recognition_model_route or self.owner._recognition_route(),
|
||
"idempotency_key": f"advanced-style-source:{source.source_id}:{retry_count}",
|
||
"request": {"import_id": record.import_id, "source_id": source.source_id, "operation": "recognize_style_source"},
|
||
"metadata": {
|
||
"source": "advanced_material",
|
||
"advanced_material_import_id": record.import_id,
|
||
"advanced_style_source_id": source.source_id,
|
||
"stage": "style_source_recognition",
|
||
"task_summary": f"进阶画风/{record.display_name}/样本{source.sort_order + 1}",
|
||
},
|
||
},
|
||
ctx,
|
||
)
|
||
self.repository.update_style_source(source.source_id, {"status": "queued", "task_id": task.task_id, "error": None})
|
||
self.repository.update_import(record.import_id, {"status": "style_source_recognition_queued", "error": None})
|
||
return task
|
||
|
||
def _execute_source(self, task: Any, ctx: RequestContext) -> WorkerTaskUpdate:
|
||
import_id = _required_task_value(task, "advanced_material_import_id")
|
||
source_id = _required_task_value(task, "advanced_style_source_id")
|
||
record = self.owner._require_import(import_id)
|
||
self._require_style_set(record)
|
||
source = self._require_source(import_id, source_id)
|
||
if source.status == "succeeded":
|
||
return WorkerTaskUpdate(result={"source_id": source_id, "status": "succeeded"})
|
||
if source.status not in STYLE_SOURCE_EXECUTABLE or source.task_id != task.task_id:
|
||
raise RuntimeError("style source recognition no longer owns the source")
|
||
source = self.repository.update_style_source_if_status(source_id, {"queued", "running"}, {"status": "running", "error": None})
|
||
if source is None:
|
||
raise RuntimeError("style source recognition could not claim the source")
|
||
self.repository.update_import(import_id, {"status": "recognizing_style_sources"})
|
||
route_name = source.recognition_model_route or record.recognition_model_route or self.owner._recognition_route()
|
||
provider = self.model_provider_runtime.vision_provider_for_route(
|
||
{"route_name": route_name, "modality": "vision_recognition", "material_id": source_id, "image_uri": source.source_image_uri}
|
||
)
|
||
if provider is None:
|
||
raise ApiError(ErrorCode.MODEL_CAPABILITY_UNCONFIRMED, "advanced style vision route is unavailable", {"route_name": route_name})
|
||
image_data_url, content_type, size_bytes = _storage_image_data_url(source.source_image_uri, {}, self.storage)
|
||
request = VisionRecognitionRequest(
|
||
display_name=f"{record.display_name} / sample {source.sort_order + 1}",
|
||
image_uri=image_data_url,
|
||
metadata={
|
||
"recognition_instruction": _single_style_instruction(),
|
||
"style_set": True,
|
||
"source_id": source_id,
|
||
"vision_input": {"source_uri": source.source_image_uri, "content_type": content_type, "size_bytes": size_bytes},
|
||
},
|
||
)
|
||
try:
|
||
recognition = provider.recognize_material(request)
|
||
except VisionProviderError as exc:
|
||
raise ApiError(ErrorCode.GENERATION_FAILED, "advanced style source recognition failed", _vision_provider_error(exc)) from None
|
||
structures = self.normalization_service.normalize_recognition(
|
||
recognition.structured_data,
|
||
prompt=recognition.prompt,
|
||
negative_prompt=recognition.negative_prompt,
|
||
tags=recognition.tags,
|
||
confidence=recognition.confidence,
|
||
provider_id=recognition.provider_id,
|
||
model=recognition.model,
|
||
route_name=route_name,
|
||
category_hint="风格",
|
||
)
|
||
structures = _force_style_structures(structures)
|
||
result = {"recognition": _recognition_metadata(recognition, route_name=route_name), **structures}
|
||
updated = self.repository.update_style_source_if_status(
|
||
source_id,
|
||
{"running"},
|
||
{
|
||
"status": "succeeded",
|
||
"provider_id": recognition.provider_id,
|
||
"provider_model": recognition.model,
|
||
"recognition_prompt": recognition.prompt,
|
||
"recognition_negative_prompt": recognition.negative_prompt,
|
||
"recognition_tags": recognition.tags,
|
||
"recognition_result": result,
|
||
"error": None,
|
||
},
|
||
)
|
||
if updated is None:
|
||
raise RuntimeError("style source result arrived after exclusion or cancellation")
|
||
self._after_source_terminal(import_id, ctx)
|
||
return WorkerTaskUpdate(result={"import_id": import_id, "source_id": source_id, "status": "succeeded"}, metadata={"pipeline_stage": "style_source_completed"})
|
||
|
||
def _after_source_terminal(self, import_id: str, ctx: RequestContext) -> None:
|
||
record = self.repository.get_import(import_id)
|
||
if record is None or record.status in {"cancelled", "accepted", "archived"}:
|
||
return
|
||
sources = self.repository.list_style_sources(import_id, include_excluded=False)
|
||
if any(item.status not in STYLE_SOURCE_TERMINAL for item in sources):
|
||
return
|
||
succeeded = [item for item in sources if item.status == "succeeded"]
|
||
if len(succeeded) < MIN_STYLE_SOURCES:
|
||
self.repository.update_import(import_id, {"status": "style_source_review_required", "error": {"error_type": "insufficient_style_sources"}})
|
||
return
|
||
failed = [item for item in sources if item.status in {"failed", "cancelled"}]
|
||
if failed:
|
||
self.repository.update_import(import_id, {"status": "style_source_review_required", "error": {"error_type": "style_source_partial_failure", "source_ids": [item.source_id for item in failed]}})
|
||
return
|
||
try:
|
||
self.create_consensus(import_id, {"automatic": True}, ctx)
|
||
except Exception:
|
||
latest = self.repository.get_latest_style_consensus_run(import_id)
|
||
if latest is None or latest.status not in {"queued", "running", "succeeded"}:
|
||
# Source recognition itself succeeded. A queue/database
|
||
# failure while scheduling the next stage is represented on
|
||
# the parent/consensus and must not retroactively fail the
|
||
# source task that happened to cross the barrier last.
|
||
failure = latest.error if latest is not None else {"error_type": "consensus_enqueue_failed"}
|
||
self.repository.update_import(
|
||
import_id,
|
||
{"status": "style_consensus_failed", "error": failure},
|
||
)
|
||
return
|
||
|
||
def _execute_consensus(self, task: Any, ctx: RequestContext) -> WorkerTaskUpdate:
|
||
import_id = _required_task_value(task, "advanced_material_import_id")
|
||
consensus_id = _required_task_value(task, "advanced_style_consensus_id")
|
||
record = self.owner._require_import(import_id)
|
||
self._require_style_set(record)
|
||
consensus = self.repository.get_style_consensus_run(consensus_id)
|
||
if consensus is None or consensus.import_id != import_id:
|
||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "style consensus does not exist", {"consensus_id": consensus_id})
|
||
if consensus.status == "succeeded":
|
||
return WorkerTaskUpdate(result={"consensus_id": consensus_id, "status": "succeeded"})
|
||
if consensus.status not in STYLE_CONSENSUS_EXECUTABLE or consensus.task_id != task.task_id:
|
||
raise RuntimeError("style consensus no longer owns the import")
|
||
consensus = self.repository.update_style_consensus_run(
|
||
consensus_id,
|
||
{"status": "running", "error": None},
|
||
expected_statuses={"queued", "running"},
|
||
)
|
||
if consensus is None:
|
||
raise RuntimeError("style consensus could not be claimed")
|
||
self.repository.update_import(import_id, {"status": "style_consensus_running"})
|
||
sources = [
|
||
self._require_source(import_id, source_id)
|
||
for source_id in consensus.source_ids
|
||
]
|
||
sources = [item for item in sources if item.status == "succeeded"]
|
||
if len(sources) < MIN_STYLE_SOURCES:
|
||
raise ApiError(ErrorCode.VALIDATION_FAILED, "style consensus lost its successful source quorum")
|
||
source_by_id = {item.source_id: item for item in sources}
|
||
source_reports: list[dict[str, Any]] = []
|
||
for source in sources:
|
||
source_reports.append(
|
||
{
|
||
"source_id": source.source_id,
|
||
"sort_order": source.sort_order,
|
||
"analysis": _consensus_source_analysis(source.recognition_result),
|
||
}
|
||
)
|
||
style_adjustment = _optional_text(
|
||
consensus.metadata.get("style_adjustment_text") or consensus.metadata.get("adjustment_text")
|
||
)
|
||
style_adjustment_plan = _style_adjustment_plan(
|
||
consensus.metadata.get("style_adjustment_plan"),
|
||
style_adjustment,
|
||
)
|
||
comparison_attempt_id = _optional_text(consensus.metadata.get("comparison_attempt_id"))
|
||
comparison_consensus_id = _optional_text(consensus.metadata.get("comparison_consensus_id"))
|
||
comparison_consensus = (
|
||
self.repository.get_style_consensus_run(comparison_consensus_id)
|
||
if comparison_consensus_id
|
||
else None
|
||
)
|
||
previous_cases = (
|
||
[
|
||
item
|
||
for item in self.repository.list_style_validation_cases(comparison_attempt_id)
|
||
if item.import_id == import_id and item.validation_image_uri
|
||
][:3]
|
||
if comparison_attempt_id
|
||
else []
|
||
)
|
||
image_urls: list[str] = []
|
||
image_manifest: list[dict[str, Any]] = []
|
||
comparison_source_ids = [
|
||
item
|
||
for item in _string_list(consensus.metadata.get("comparison_source_ids"))
|
||
if item in source_by_id
|
||
][:3]
|
||
if previous_cases and not comparison_source_ids and comparison_consensus is not None:
|
||
comparison_source_ids = [
|
||
item
|
||
for item in _string_list(comparison_consensus.result.get("representative_source_ids"))
|
||
if item in source_by_id
|
||
][:3]
|
||
if previous_cases:
|
||
comparison_source_ids = comparison_source_ids or [item.source_id for item in sources[:3]]
|
||
for source_id in comparison_source_ids:
|
||
source = source_by_id[source_id]
|
||
image_url, _, _ = _storage_image_data_url(source.source_image_uri, {}, self.storage)
|
||
image_urls.append(image_url)
|
||
image_manifest.append(
|
||
{
|
||
"image_index": len(image_urls) - 1,
|
||
"kind": "source_reference",
|
||
"source_id": source.source_id,
|
||
"image_uri": source.source_image_uri,
|
||
}
|
||
)
|
||
for case in previous_cases:
|
||
image_url, _, _ = _storage_image_data_url(str(case.validation_image_uri), {}, self.storage)
|
||
image_urls.append(image_url)
|
||
image_manifest.append(
|
||
{
|
||
"image_index": len(image_urls) - 1,
|
||
"kind": "previous_validation",
|
||
"case_id": case.case_id,
|
||
"case_key": case.case_key,
|
||
"image_uri": case.validation_image_uri,
|
||
}
|
||
)
|
||
else:
|
||
for source in sources:
|
||
image_url, _, _ = _storage_image_data_url(source.source_image_uri, {}, self.storage)
|
||
image_urls.append(image_url)
|
||
image_manifest.append(
|
||
{
|
||
"image_index": len(image_urls) - 1,
|
||
"kind": "source_reference",
|
||
"source_id": source.source_id,
|
||
"image_uri": source.source_image_uri,
|
||
}
|
||
)
|
||
automatic_review_feedback = _aggregate_style_review_feedback(previous_cases)
|
||
comparison_payload = {
|
||
"attempt_id": comparison_attempt_id,
|
||
"consensus_id": comparison_consensus_id,
|
||
"previous_prompt": comparison_consensus.prompt if comparison_consensus is not None else None,
|
||
"previous_negative_prompt": comparison_consensus.negative_prompt if comparison_consensus is not None else None,
|
||
"previous_generation_capsules": (
|
||
comparison_consensus.result.get("generation_capsules")
|
||
if comparison_consensus is not None
|
||
else None
|
||
),
|
||
"validation_cases": [
|
||
{
|
||
"case_id": item.case_id,
|
||
"case_key": item.case_key,
|
||
"content_prompt": item.content_prompt,
|
||
"final_prompt": item.final_prompt,
|
||
"style_review": item.style_review,
|
||
"validation_image_uri": item.validation_image_uri,
|
||
}
|
||
for item in previous_cases
|
||
],
|
||
"automatic_review_feedback": automatic_review_feedback,
|
||
}
|
||
route_name = self.owner._consensus_route() or consensus.model_route
|
||
requested_analysis_strategy = _optional_text(consensus.metadata.get("analysis_strategy")) or "text_only"
|
||
text_callable_factory = getattr(self.model_provider_runtime, "structured_text_callable", None)
|
||
text_callable = (
|
||
text_callable_factory(route_name)
|
||
if requested_analysis_strategy == "text_only" and callable(text_callable_factory)
|
||
else None
|
||
)
|
||
provider = None
|
||
if text_callable is None:
|
||
provider = self.model_provider_runtime.vision_provider_for_route(
|
||
{"route_name": route_name, "modality": "vision_recognition", "material_id": consensus_id, "image_uri": sources[0].source_image_uri}
|
||
)
|
||
if provider is None:
|
||
raise ApiError(ErrorCode.MODEL_CAPABILITY_UNCONFIRMED, "advanced style consensus route is unavailable", {"route_name": route_name})
|
||
analysis_strategy = "text_only" if text_callable is not None else "single_pass"
|
||
try:
|
||
if text_callable is not None:
|
||
payload = {
|
||
"purpose": "advanced style cross-image text consensus",
|
||
"source_ids": [item.source_id for item in sources],
|
||
"source_reports": source_reports,
|
||
"comparison_context": comparison_payload,
|
||
"style_adjustment_plan": style_adjustment_plan,
|
||
"human_style_feedback": style_adjustment,
|
||
"recognition_instruction": _consensus_instruction(
|
||
source_ids=[item.source_id for item in sources],
|
||
style_adjustment=style_adjustment,
|
||
style_adjustment_plan=style_adjustment_plan,
|
||
validation_instruction=_optional_text(consensus.metadata.get("validation_instruction")),
|
||
has_previous_validation=bool(previous_cases),
|
||
),
|
||
}
|
||
raw_result = text_callable(payload)
|
||
if isinstance(raw_result, str):
|
||
try:
|
||
raw_result = json.loads(raw_result)
|
||
except json.JSONDecodeError as exc:
|
||
raise ApiError(
|
||
ErrorCode.GENERATION_FAILED,
|
||
"advanced style text consensus returned invalid JSON",
|
||
{"route_name": route_name, "error": str(exc)[:300]},
|
||
) from None
|
||
if not isinstance(raw_result, dict):
|
||
raise ApiError(
|
||
ErrorCode.GENERATION_FAILED,
|
||
"advanced style text consensus returned a non-object result",
|
||
{"route_name": route_name},
|
||
)
|
||
structured = dict(raw_result)
|
||
recognition = VisionRecognitionResult(
|
||
provider_id=route_name,
|
||
model=route_name,
|
||
prompt=str(structured.get("prompt") or "").strip(),
|
||
negative_prompt=(str(structured["negative_prompt"]).strip() if structured.get("negative_prompt") is not None else None),
|
||
tags=_string_list(structured.get("tags")),
|
||
confidence=structured.get("confidence"),
|
||
description=(str(structured["description"]).strip() if structured.get("description") is not None else None),
|
||
structured_data=structured,
|
||
raw_metadata={"analysis_strategy": "text_only"},
|
||
)
|
||
image_manifest = []
|
||
elif len(sources) <= 4 or previous_cases:
|
||
request = VisionRecognitionRequest(
|
||
display_name=f"{record.display_name} / cross-image style consensus",
|
||
image_uri=image_urls[0],
|
||
image_uris=tuple(image_urls[1:]),
|
||
prompt=json.dumps(
|
||
{
|
||
"source_reports": source_reports,
|
||
"image_manifest": image_manifest,
|
||
"previous_validation": comparison_payload,
|
||
"automatic_review_feedback": automatic_review_feedback,
|
||
"style_adjustment_plan": style_adjustment_plan,
|
||
"human_style_feedback": style_adjustment,
|
||
},
|
||
ensure_ascii=False,
|
||
),
|
||
metadata={
|
||
"recognition_instruction": _consensus_instruction(
|
||
source_ids=[item.source_id for item in sources],
|
||
style_adjustment=style_adjustment,
|
||
style_adjustment_plan=style_adjustment_plan,
|
||
validation_instruction=_optional_text(consensus.metadata.get("validation_instruction")),
|
||
has_previous_validation=bool(previous_cases),
|
||
),
|
||
"style_set": True,
|
||
"consensus_id": consensus_id,
|
||
"comparison_attempt_id": comparison_attempt_id,
|
||
"comparison_consensus_id": comparison_consensus_id,
|
||
"image_manifest": image_manifest,
|
||
"style_adjustment_plan": style_adjustment_plan,
|
||
"automatic_review_feedback": automatic_review_feedback,
|
||
},
|
||
)
|
||
recognition = provider.recognize_material(request)
|
||
else:
|
||
analysis_strategy = "grouped_two_stage"
|
||
groups = [sources[index:index + 4] for index in range(0, len(sources), 4)]
|
||
partial_analyses: list[dict[str, Any]] = []
|
||
for group_index, group in enumerate(groups):
|
||
group_reports = [item for item in source_reports if item["source_id"] in {source.source_id for source in group}]
|
||
# Some OpenAI-compatible relays stall on multiple image
|
||
# parts. Keep one visual anchor and carry the remaining
|
||
# evidence through the normalized source reports.
|
||
group_urls = [_storage_image_data_url(group[0].source_image_uri, {}, self.storage)[0]]
|
||
partial_request = VisionRecognitionRequest(
|
||
display_name=f"{record.display_name} / style consensus group {group_index + 1}",
|
||
image_uri=group_urls[0],
|
||
image_uris=tuple(group_urls[1:]),
|
||
prompt=json.dumps(
|
||
{
|
||
"analysis_mode": "partial_style_consensus",
|
||
"source_reports": group_reports,
|
||
"source_ids": [source.source_id for source in group],
|
||
},
|
||
ensure_ascii=False,
|
||
),
|
||
metadata={
|
||
"recognition_instruction": _consensus_instruction(
|
||
source_ids=[source.source_id for source in group],
|
||
style_adjustment=style_adjustment,
|
||
style_adjustment_plan=style_adjustment_plan,
|
||
validation_instruction=None,
|
||
has_previous_validation=False,
|
||
),
|
||
"style_set": True,
|
||
"consensus_id": consensus_id,
|
||
"analysis_mode": "partial_style_consensus",
|
||
},
|
||
)
|
||
partial = provider.recognize_material(partial_request)
|
||
structured = partial.structured_data if isinstance(partial.structured_data, dict) else {}
|
||
partial_analyses.append(
|
||
{
|
||
"group_index": group_index,
|
||
"source_ids": [source.source_id for source in group],
|
||
"prompt": partial.prompt,
|
||
"negative_prompt": partial.negative_prompt,
|
||
"tags": partial.tags,
|
||
"confidence": partial.confidence,
|
||
"description": partial.description,
|
||
"analysis": {
|
||
key: structured.get(key)
|
||
for key in (
|
||
"consensus_style",
|
||
"artist_signature",
|
||
"variable_style_features",
|
||
"source_content_features",
|
||
"content_leakage_exclusions",
|
||
"feature_evidence",
|
||
"generation_capsules",
|
||
"prompt_priority_groups",
|
||
"scale_capsules",
|
||
"semantic_risk_audit",
|
||
)
|
||
if key in structured
|
||
},
|
||
}
|
||
)
|
||
|
||
representative_sources = [group[0] for group in groups[:2]]
|
||
final_urls = [_storage_image_data_url(representative_sources[0].source_image_uri, {}, self.storage)[0]]
|
||
image_manifest = [
|
||
{
|
||
"image_index": index,
|
||
"kind": "representative_source_reference",
|
||
"source_id": source.source_id,
|
||
"image_uri": source.source_image_uri,
|
||
}
|
||
for index, source in enumerate(representative_sources)
|
||
]
|
||
request = VisionRecognitionRequest(
|
||
display_name=f"{record.display_name} / final style consensus",
|
||
image_uri=final_urls[0],
|
||
image_uris=tuple(final_urls[1:]),
|
||
prompt=json.dumps(
|
||
{
|
||
"analysis_mode": "final_style_consensus",
|
||
"source_ids": [source.source_id for source in sources],
|
||
"partial_analyses": partial_analyses,
|
||
"style_adjustment_plan": style_adjustment_plan,
|
||
"human_style_feedback": style_adjustment,
|
||
},
|
||
ensure_ascii=False,
|
||
),
|
||
metadata={
|
||
"recognition_instruction": _consensus_instruction(
|
||
source_ids=[item.source_id for item in sources],
|
||
style_adjustment=style_adjustment,
|
||
style_adjustment_plan=style_adjustment_plan,
|
||
validation_instruction=_optional_text(consensus.metadata.get("validation_instruction")),
|
||
has_previous_validation=False,
|
||
),
|
||
"style_set": True,
|
||
"consensus_id": consensus_id,
|
||
"analysis_mode": "final_style_consensus",
|
||
"image_manifest": image_manifest,
|
||
"style_adjustment_plan": style_adjustment_plan,
|
||
},
|
||
)
|
||
recognition = provider.recognize_material(request)
|
||
except VisionProviderError as exc:
|
||
raise ApiError(ErrorCode.GENERATION_FAILED, "advanced style cross-image analysis failed", _vision_provider_error(exc)) from None
|
||
try:
|
||
structures = self.normalization_service.normalize_recognition(
|
||
recognition.structured_data,
|
||
prompt=recognition.prompt,
|
||
negative_prompt=recognition.negative_prompt,
|
||
tags=recognition.tags,
|
||
confidence=recognition.confidence,
|
||
provider_id=recognition.provider_id,
|
||
model=recognition.model,
|
||
route_name=route_name,
|
||
category_hint="风格",
|
||
)
|
||
except MaterialNormalizationError as exc:
|
||
raise ApiError(ErrorCode.GENERATION_FAILED, "style consensus returned invalid structures") from exc
|
||
structures = _force_style_structures(structures)
|
||
effective_prompt, effective_negative_prompt, structures, adjustment_guard = _apply_style_adjustment_guard(
|
||
recognition.prompt,
|
||
recognition.negative_prompt,
|
||
structures,
|
||
style_adjustment_plan,
|
||
)
|
||
valid_ids = {item.source_id for item in sources}
|
||
provider_result = recognition.structured_data if isinstance(recognition.structured_data, dict) else {}
|
||
reused = consensus.metadata.get("reuse_validation_cases")
|
||
validation_instruction = _optional_text(consensus.metadata.get("validation_instruction"))
|
||
validation_cases = _validation_cases(
|
||
provider_result.get("validation_cases"),
|
||
reused,
|
||
validation_instruction=validation_instruction,
|
||
)
|
||
result = {
|
||
"recognition": {
|
||
**_recognition_metadata(recognition, route_name=route_name),
|
||
"effective_prompt": effective_prompt,
|
||
"effective_negative_prompt": effective_negative_prompt,
|
||
},
|
||
**structures,
|
||
"consensus_style": _object(provider_result.get("consensus_style")),
|
||
"artist_signature": _normalize_artist_signature(provider_result.get("artist_signature"), sources),
|
||
"variable_style_features": _list(provider_result.get("variable_style_features")),
|
||
"source_content_features": _list(provider_result.get("source_content_features")),
|
||
"content_leakage_exclusions": _string_list(provider_result.get("content_leakage_exclusions")),
|
||
"outlier_source_ids": [item for item in _string_list(provider_result.get("outlier_source_ids")) if item in valid_ids],
|
||
"representative_source_ids": [item for item in _string_list(provider_result.get("representative_source_ids")) if item in valid_ids][:3]
|
||
or [item.source_id for item in sources[:3]],
|
||
"consensus_confidence": _calibrated_style_confidence(
|
||
provider_result,
|
||
recognition.confidence,
|
||
structures,
|
||
),
|
||
"feature_evidence": _list(provider_result.get("feature_evidence")),
|
||
"prompt_priority_groups": _object(provider_result.get("prompt_priority_groups")),
|
||
"scale_capsules": _object(provider_result.get("scale_capsules")),
|
||
"semantic_risk_audit": _list(provider_result.get("semantic_risk_audit")),
|
||
"validation_cases": validation_cases,
|
||
"human_adjustments": {
|
||
"style_adjustment_text": _optional_text(consensus.metadata.get("style_adjustment_text")),
|
||
"validation_instruction": validation_instruction,
|
||
"style_adjustment_plan": style_adjustment_plan,
|
||
"guard": adjustment_guard,
|
||
},
|
||
"comparison_context": comparison_payload,
|
||
"automatic_review_feedback": automatic_review_feedback,
|
||
"analysis_strategy": analysis_strategy,
|
||
"requested_analysis_strategy": requested_analysis_strategy,
|
||
"image_manifest": image_manifest,
|
||
"effective_style_prompt": effective_prompt,
|
||
"effective_style_negative_prompt": effective_negative_prompt,
|
||
"source_ids": [item.source_id for item in sources],
|
||
}
|
||
updated = self.repository.update_style_consensus_run(
|
||
consensus_id,
|
||
{
|
||
"status": "succeeded",
|
||
"provider_id": recognition.provider_id,
|
||
"provider_model": recognition.model,
|
||
"prompt": effective_prompt,
|
||
"negative_prompt": effective_negative_prompt,
|
||
"tags": recognition.tags,
|
||
"result": result,
|
||
"error": None,
|
||
"metadata": {
|
||
**consensus.metadata,
|
||
"style_adjustment_plan": style_adjustment_plan,
|
||
"analysis_strategy": analysis_strategy,
|
||
"comparison_attempt_id": comparison_attempt_id,
|
||
"comparison_consensus_id": comparison_consensus_id,
|
||
"image_manifest": image_manifest,
|
||
},
|
||
},
|
||
expected_statuses={"running"},
|
||
)
|
||
if updated is None:
|
||
raise RuntimeError("style consensus result arrived after cancellation or replacement")
|
||
self.repository.update_import(
|
||
import_id,
|
||
{
|
||
"status": "ready_for_validation",
|
||
"recognition_prompt": effective_prompt,
|
||
"recognition_negative_prompt": effective_negative_prompt,
|
||
"recognition_tags": recognition.tags,
|
||
"recognition_result": result,
|
||
"prompt_revision": consensus.revision,
|
||
"error": None,
|
||
"metadata": {**record.metadata, "current_consensus_id": consensus_id},
|
||
},
|
||
)
|
||
try:
|
||
attempt, cases, tasks = self._queue_validation_batch(
|
||
self.owner._require_import(import_id),
|
||
updated,
|
||
ctx,
|
||
)
|
||
except Exception as exc:
|
||
# The reusable consensus is already valid. Keep it and expose a
|
||
# retryable validation-stage failure instead of forcing another
|
||
# paid/expensive cross-image analysis.
|
||
self.repository.update_import(
|
||
import_id,
|
||
{"status": "validation_failed", "error": _safe_failure(exc)},
|
||
)
|
||
raise
|
||
return WorkerTaskUpdate(
|
||
result={"import_id": import_id, "consensus_id": consensus_id, "attempt_id": attempt.attempt_id, "case_ids": [item.case_id for item in cases], "next_task_ids": [item.task_id for item in tasks]},
|
||
metadata={"pipeline_stage": "style_consensus_completed"},
|
||
)
|
||
|
||
def _queue_validation_batch(self, record: Any, consensus: Any, ctx: RequestContext):
|
||
route_name = _optional_text(record.metadata.get("generation_model_route")) or self.owner._generation_route()
|
||
validation_instruction = _optional_text(consensus.metadata.get("validation_instruction"))
|
||
style_adjustment = _optional_text(consensus.metadata.get("style_adjustment_text") or consensus.metadata.get("adjustment_text"))
|
||
plans = _validation_cases(
|
||
consensus.result.get("validation_cases"),
|
||
consensus.metadata.get("reuse_validation_cases"),
|
||
validation_instruction=validation_instruction,
|
||
)
|
||
leakage = _string_list(consensus.result.get("content_leakage_exclusions"))
|
||
detailed_style_prompt = _detailed_style_prompt(consensus.result, consensus.prompt)
|
||
detailed_style_negative = _detailed_style_negative_prompt(consensus.result, consensus.negative_prompt)
|
||
sources = [item for item in self.repository.list_style_sources(record.import_id) if item.status == "succeeded"]
|
||
reference_plan = _resolve_validation_reference_plan(
|
||
sources=sources,
|
||
consensus_result=consensus.result if isinstance(consensus.result, dict) else {},
|
||
consensus_metadata=consensus.metadata if isinstance(consensus.metadata, dict) else {},
|
||
)
|
||
negative = _join_prompt(
|
||
detailed_style_negative,
|
||
"exact source character identity, exact source costume design, exact source pose, exact source setting, "
|
||
"generic semi-realistic reinterpretation, realistic facial anatomy when absent from the source style, text, watermark",
|
||
)
|
||
if reference_plan["enabled"]:
|
||
negative = _join_prompt(
|
||
negative,
|
||
"copy identity from reference image, copy costume from reference image, copy pose from reference image, "
|
||
"copy scene from reference image, exact reference character face",
|
||
)
|
||
case_payloads = []
|
||
for index, plan in enumerate(plans):
|
||
content_prompt = str(plan["content_prompt"]).strip()
|
||
case_key = str(plan["case_key"])
|
||
scale_style_prompt = _scale_specific_style_prompt(consensus.result, case_key, detailed_style_prompt)
|
||
scale_style_negative = _scale_specific_style_negative_prompt(consensus.result, case_key)
|
||
anatomy_range = _source_anatomy_range_from_style_result(consensus.result)
|
||
anatomy_phrase = _anatomy_soft_band_phrase(anatomy_range) if case_key == "fullbody" else None
|
||
if case_key == "portrait":
|
||
scale_style_negative = _join_prompt(
|
||
scale_style_negative,
|
||
"long narrow face, sharp V-shaped jaw, pointed chin, widened jaw, altered eye spacing, asymmetric facial contour",
|
||
)
|
||
if case_key == "fullbody":
|
||
content_prompt = _join_prompt(content_prompt, anatomy_phrase)
|
||
scale_style_prompt = _join_prompt(scale_style_prompt, anatomy_phrase)
|
||
scale_style_negative = _join_prompt(
|
||
scale_style_negative,
|
||
"chibi or super-deformed anatomy, unnaturally compressed body proportions",
|
||
)
|
||
case_negative = _join_prompt(negative, scale_style_negative)
|
||
ref_clause = (
|
||
"optional style reference image is attached for visual grammar only; "
|
||
"copy high-frequency visual grammar such as line pressure, palette relationships, eye geometry, hair-lock grouping, opaque pearlescent highlight shapes, shading edges and anatomy abstraction from the reference, "
|
||
"but invent a new character identity, costume, pose and scene from the content prompt; "
|
||
"do not copy face identity, hairstyle identity, outfit design, props, pose or background from the reference"
|
||
if reference_plan["enabled"]
|
||
else "transfer style from the detailed style prompt only; no source image is attached"
|
||
)
|
||
final_prompt = _join_prompt(
|
||
detailed_style_prompt,
|
||
scale_style_prompt,
|
||
content_prompt,
|
||
_scale_transfer_instruction(case_key),
|
||
"transfer the complete shared visual grammar, including facial proportions, eye geometry, nose and mouth simplification, "
|
||
"hair strand grouping, line topology, local line colors, shading edges, palette relationships and highlight shapes; "
|
||
"do not reproduce any exact source identity, costume, pose or setting; preserve the requested validation subject and composition",
|
||
ref_clause,
|
||
)
|
||
case_payloads.append(
|
||
{
|
||
"case_id": f"advcase_{uuid4().hex}",
|
||
"case_key": case_key,
|
||
"sort_order": index,
|
||
"status": "queued",
|
||
"content_prompt": content_prompt,
|
||
"final_prompt": final_prompt,
|
||
"negative_prompt": case_negative,
|
||
"model_route": route_name,
|
||
"seed": _style_validation_seed(index, consensus.revision, refresh_content=bool(validation_instruction)),
|
||
"width": 1024,
|
||
"height": 1024,
|
||
"metadata": {
|
||
"aspect_ratio": plan.get("aspect_ratio") or DEFAULT_VALIDATION_CASES[index]["aspect_ratio"],
|
||
"style_consensus_id": consensus.consensus_id,
|
||
"style_consensus_revision": consensus.revision,
|
||
"style_adjustment_text": style_adjustment,
|
||
"style_adjustment_plan": consensus.result.get("human_adjustments", {}).get("style_adjustment_plan", {}),
|
||
"validation_instruction": validation_instruction,
|
||
"content_refresh": bool(validation_instruction),
|
||
"style_capsule_tier": "detailed",
|
||
"effective_style_prompt": detailed_style_prompt,
|
||
"effective_style_negative_prompt": detailed_style_negative,
|
||
"scale_style_prompt": scale_style_prompt,
|
||
"scale_style_negative_prompt": scale_style_negative,
|
||
"style_compiler_version": 3,
|
||
"content_leakage_exclusions": leakage,
|
||
"retry_count": 0,
|
||
"prompt_only": not reference_plan["enabled"],
|
||
"validation_reference_enabled": reference_plan["enabled"],
|
||
"validation_reference_source_id": reference_plan["source_id"],
|
||
"validation_reference_source_uri": reference_plan["source_uri"],
|
||
"validation_reference_mode": reference_plan["mode"],
|
||
"reference_image_applied": False,
|
||
"generation_timeout_seconds": STYLE_VALIDATION_TIMEOUT_SECONDS,
|
||
},
|
||
}
|
||
)
|
||
attempt, cases = self.repository.create_style_validation_batch(
|
||
{
|
||
"attempt_id": f"advattempt_{uuid4().hex}",
|
||
"import_id": record.import_id,
|
||
"status": "running",
|
||
"adjustment_text": _review_adjustment_summary(style_adjustment, validation_instruction),
|
||
"prompt": detailed_style_prompt or consensus.prompt or record.recognition_prompt,
|
||
"negative_prompt": negative,
|
||
"model_route": route_name,
|
||
"seed": _style_validation_seed(0, consensus.revision, refresh_content=bool(validation_instruction)),
|
||
"width": 1024,
|
||
"height": 1024,
|
||
"metadata": {
|
||
"style_consensus_id": consensus.consensus_id,
|
||
"style_consensus_revision": consensus.revision,
|
||
"style_set": True,
|
||
"style_adjustment_text": style_adjustment,
|
||
"validation_instruction": validation_instruction,
|
||
"validation_reference_enabled": reference_plan["enabled"],
|
||
"validation_reference_source_id": reference_plan["source_id"],
|
||
"validation_reference_source_uri": reference_plan["source_uri"],
|
||
"validation_reference_mode": reference_plan["mode"],
|
||
},
|
||
},
|
||
case_payloads,
|
||
)
|
||
tasks = []
|
||
for case in cases:
|
||
try:
|
||
tasks.append(self._queue_validation_case(record, case, ctx))
|
||
except Exception as exc:
|
||
self.repository.update_style_validation_case(
|
||
case.case_id,
|
||
{"status": "failed", "task_id": None, "error": _safe_failure(exc)},
|
||
expected_statuses={"queued"},
|
||
)
|
||
self.repository.update_import(
|
||
record.import_id,
|
||
{
|
||
"status": "validating",
|
||
"current_attempt_id": attempt.attempt_id,
|
||
"error": None,
|
||
"metadata": {**record.metadata, "active_task_ids": [item.task_id for item in tasks], "current_consensus_id": consensus.consensus_id},
|
||
},
|
||
)
|
||
# All three task insertions may fail before any worker exists to run
|
||
# the aggregation callback. Re-evaluate immediately so the batch is
|
||
# retryable instead of remaining in a false "validating" state.
|
||
_, attempt, cases = self.repository.finalize_style_validation_batch_if_ready(
|
||
record.import_id,
|
||
attempt.attempt_id,
|
||
)
|
||
return attempt, cases, tasks
|
||
|
||
def _queue_validation_case(self, record: Any, case: Any, ctx: RequestContext):
|
||
retry_count = int(case.metadata.get("retry_count") or 0)
|
||
task = self.queue_service.create_task(
|
||
{
|
||
"task_type": "advanced_style_validation_generation",
|
||
"status": "queued",
|
||
"model_route": case.model_route or self.owner._generation_route(),
|
||
"idempotency_key": f"advanced-style-validation:{case.case_id}:{retry_count}",
|
||
"request": {
|
||
"import_id": record.import_id,
|
||
"attempt_id": case.attempt_id,
|
||
"case_id": case.case_id,
|
||
"operation": "validate_style_transfer",
|
||
"timeout_seconds": STYLE_VALIDATION_TIMEOUT_SECONDS,
|
||
},
|
||
"metadata": {
|
||
"source": "advanced_material",
|
||
"advanced_material_import_id": record.import_id,
|
||
"advanced_material_attempt_id": case.attempt_id,
|
||
"advanced_style_validation_case_id": case.case_id,
|
||
"stage": "style_validation_generation",
|
||
"task_summary": f"进阶画风/{record.display_name}/{case.case_key}迁移验证",
|
||
"timeout_seconds": STYLE_VALIDATION_TIMEOUT_SECONDS,
|
||
},
|
||
},
|
||
ctx,
|
||
)
|
||
self.repository.update_style_validation_case(case.case_id, {"status": "queued", "task_id": task.task_id, "error": None})
|
||
return task
|
||
|
||
def _execute_validation_case(self, task: Any, ctx: RequestContext) -> WorkerTaskUpdate:
|
||
import_id = _required_task_value(task, "advanced_material_import_id")
|
||
attempt_id = _required_task_value(task, "advanced_material_attempt_id")
|
||
case_id = _required_task_value(task, "advanced_style_validation_case_id")
|
||
record = self.owner._require_import(import_id)
|
||
self._require_style_set(record)
|
||
attempt = self.owner._require_attempt(import_id, attempt_id)
|
||
case = self._require_case(import_id, case_id)
|
||
if case.status == "succeeded" and case.validation_image_uri:
|
||
return WorkerTaskUpdate(result={"case_id": case_id, "status": "succeeded"})
|
||
if case.status not in STYLE_CASE_EXECUTABLE or case.task_id != task.task_id:
|
||
raise RuntimeError("style validation case no longer owns the task")
|
||
case = self.repository.update_style_validation_case(case_id, {"status": "running", "error": None}, expected_statuses={"queued", "running"})
|
||
if case is None:
|
||
raise RuntimeError("style validation case could not be claimed")
|
||
self.repository.update_attempt(attempt_id, {"status": "running", "error": None})
|
||
self.repository.update_import(import_id, {"status": "validating", "error": None})
|
||
case_attempt = replace(attempt, prompt=case.final_prompt, negative_prompt=case.negative_prompt, model_route=case.model_route)
|
||
requested_route = case.model_route or self.owner._generation_route()
|
||
governance = self.owner._govern_validation_prompt(record=record, attempt=case_attempt, requested_route=requested_route, ctx=ctx)
|
||
route_name = str(governance["selected_route"])
|
||
resolved_route = self.owner._resolve_generation_route(route_name)
|
||
cost_guard = self.owner._check_validation_budget(route_name, resolved_route)
|
||
node = WorkflowNode(
|
||
node_id=f"advanced-style:{case_id}",
|
||
workflow_id=f"advanced-style:{import_id}",
|
||
node_key="advanced_style_transfer_validation",
|
||
title=f"进阶画风迁移验证 / {case.case_key}",
|
||
description=None,
|
||
step_name="style_validation",
|
||
node_type="generation",
|
||
status="running",
|
||
branch_name="main",
|
||
prompt=governance["prompt"],
|
||
negative_prompt=governance["negative_prompt"],
|
||
model_route=route_name,
|
||
selected_image_id=None,
|
||
sort_order=case.sort_order,
|
||
metadata={"advanced_material": True, "style_set": True, "prompt_only": not bool(case.metadata.get("validation_reference_enabled")), "validation_reference_enabled": bool(case.metadata.get("validation_reference_enabled")), "validation_reference_source_id": case.metadata.get("validation_reference_source_id")},
|
||
created_at=case.created_at,
|
||
updated_at=case.updated_at,
|
||
)
|
||
request = {
|
||
"count": 1,
|
||
"aspect_ratio": str(case.metadata.get("aspect_ratio") or "1:1"),
|
||
"quality": "standard",
|
||
"seed": int(case.seed or STYLE_VALIDATION_SEEDS[case.sort_order]),
|
||
"timeout_seconds": int(case.metadata.get("generation_timeout_seconds") or STYLE_VALIDATION_TIMEOUT_SECONDS),
|
||
"model_route": route_name,
|
||
"resolved_model_route": resolved_route,
|
||
"allow_provider_fallback": False,
|
||
"expanded_prompt": governance["prompt"],
|
||
"expanded_negative_prompt": governance["negative_prompt"],
|
||
"prompt_governance": governance["snapshot"],
|
||
**({"cost_guard": cost_guard} if cost_guard else {}),
|
||
"output_scope": {
|
||
"kind": "advanced_style_validation",
|
||
"import_id": import_id,
|
||
"attempt_id": attempt_id,
|
||
"case_id": case_id,
|
||
"case_key": case.case_key,
|
||
},
|
||
}
|
||
reference_meta = _validation_reference_runtime_metadata(case.metadata)
|
||
input_images: list[str] = []
|
||
if reference_meta["enabled"] and reference_meta["source_uri"]:
|
||
try:
|
||
image_data_url, _, _ = _storage_image_data_url(str(reference_meta["source_uri"]), {}, self.storage)
|
||
input_images = [image_data_url]
|
||
request["input_images"] = input_images
|
||
reference_meta["reference_image_applied"] = True
|
||
except Exception as exc: # best-effort pure-text fallback
|
||
reference_meta["reference_image_applied"] = False
|
||
reference_meta["reference_image_error"] = str(exc)[:240]
|
||
else:
|
||
reference_meta["reference_image_applied"] = False
|
||
assets = []
|
||
try:
|
||
try:
|
||
assets = self.single_flow_engine.generate(node, task, request)
|
||
except ImageProviderError as exc:
|
||
if input_images and _is_reference_image_unsupported_error(exc):
|
||
request.pop("input_images", None)
|
||
reference_meta["reference_image_applied"] = False
|
||
reference_meta["reference_image_error"] = str(exc)[:240]
|
||
reference_meta["reference_image_fallback"] = "pure_text"
|
||
try:
|
||
assets = self.single_flow_engine.generate(node, task, request)
|
||
exc = None # type: ignore[assignment]
|
||
except ImageProviderError as retry_exc:
|
||
exc = retry_exc
|
||
if exc is not None:
|
||
fallback = self.owner._content_safety_fallback(
|
||
exc=exc,
|
||
runtime_request=request,
|
||
governance=governance,
|
||
requested_route=requested_route,
|
||
record=record,
|
||
ctx=ctx,
|
||
)
|
||
if fallback is None:
|
||
raise
|
||
route_name = fallback["model_route"]
|
||
resolved_route = fallback["resolved_model_route"]
|
||
request = fallback["request"]
|
||
assets = self.single_flow_engine.generate(node, task, request)
|
||
except Exception:
|
||
for asset in assets:
|
||
self.owner._remove_uri(str(getattr(asset, "image_uri", "") or ""))
|
||
self.owner._remove_uri(str(getattr(asset, "thumbnail_uri", "") or ""))
|
||
raise
|
||
if len(assets) != 1:
|
||
raise ImageProviderError("advanced style validation must return exactly one image", provider_id=str(resolved_route.get("provider_id") or "unknown"), error_type="invalid_response")
|
||
asset = assets[0]
|
||
latest = self.repository.get_style_validation_case(case_id)
|
||
if latest is None or latest.status != "running" or latest.task_id != task.task_id:
|
||
self.owner._remove_uri(asset.image_uri)
|
||
self.owner._remove_uri(asset.thumbnail_uri)
|
||
raise RuntimeError("style validation result arrived after cancellation or retry")
|
||
style_review, fidelity_score = self._review_validation_image(
|
||
record=record,
|
||
case=case,
|
||
validation_image_uri=asset.image_uri,
|
||
)
|
||
updated = self.repository.update_style_validation_case(
|
||
case_id,
|
||
{
|
||
"status": "succeeded",
|
||
"model_route": route_name,
|
||
"provider_id": asset.metadata.get("provider_id") or resolved_route.get("provider_id"),
|
||
"provider_model": asset.metadata.get("provider_model") or resolved_route.get("model_id"),
|
||
"width": asset.width,
|
||
"height": asset.height,
|
||
"validation_image_uri": asset.image_uri,
|
||
"validation_thumbnail_uri": asset.thumbnail_uri,
|
||
"usage": asset.metadata.get("usage") or {},
|
||
"style_review": style_review,
|
||
"fidelity_score": fidelity_score,
|
||
"metadata": {
|
||
**case.metadata,
|
||
"generation": asset.metadata,
|
||
"prompt_governance": governance["snapshot"],
|
||
"prompt_only": not bool(reference_meta.get("reference_image_applied")),
|
||
"validation_reference_enabled": bool(reference_meta.get("enabled")),
|
||
"validation_reference_source_id": reference_meta.get("source_id"),
|
||
"validation_reference_source_uri": reference_meta.get("source_uri"),
|
||
"validation_reference_mode": reference_meta.get("mode"),
|
||
"reference_image_applied": bool(reference_meta.get("reference_image_applied")),
|
||
"reference_image_error": reference_meta.get("reference_image_error"),
|
||
"reference_image_fallback": reference_meta.get("reference_image_fallback"),
|
||
"input_image_count": 1 if reference_meta.get("reference_image_applied") else 0,
|
||
"automatic_style_review": style_review,
|
||
},
|
||
"error": None,
|
||
},
|
||
expected_statuses={"running"},
|
||
)
|
||
if updated is None:
|
||
self.owner._remove_uri(asset.image_uri)
|
||
self.owner._remove_uri(asset.thumbnail_uri)
|
||
raise RuntimeError("style validation result could not be published")
|
||
current_import, current_attempt, cases = self.repository.finalize_style_validation_batch_if_ready(import_id, attempt_id)
|
||
review_scores = [float(item.fidelity_score) for item in cases if item.fidelity_score is not None]
|
||
if review_scores:
|
||
current_attempt = self.repository.update_attempt(
|
||
attempt_id,
|
||
{
|
||
"fidelity_score": sum(review_scores) / len(review_scores),
|
||
"metadata": {
|
||
**current_attempt.metadata,
|
||
"automatic_style_review": {
|
||
"reviewed_cases": len(review_scores),
|
||
"total_cases": len(cases),
|
||
"average_fidelity_score": sum(review_scores) / len(review_scores),
|
||
},
|
||
},
|
||
},
|
||
) or current_attempt
|
||
if current_attempt.status == "awaiting_review":
|
||
attempts = self.repository.list_attempts(import_id)
|
||
previous_attempt = next(
|
||
(
|
||
item
|
||
for item in attempts
|
||
if item.attempt_id != current_attempt.attempt_id and item.fidelity_score is not None
|
||
),
|
||
None,
|
||
)
|
||
comparison = _style_attempt_comparison(
|
||
current_attempt=current_attempt,
|
||
current_cases=cases,
|
||
previous_attempt=previous_attempt,
|
||
previous_cases=(
|
||
self.repository.list_style_validation_cases(previous_attempt.attempt_id)
|
||
if previous_attempt is not None
|
||
else []
|
||
),
|
||
)
|
||
current_attempt = self.repository.update_attempt(
|
||
attempt_id,
|
||
{
|
||
"metadata": {
|
||
**current_attempt.metadata,
|
||
"style_regression_evaluation": comparison,
|
||
}
|
||
},
|
||
) or current_attempt
|
||
return WorkerTaskUpdate(
|
||
result={
|
||
"import_id": import_id,
|
||
"attempt_id": attempt_id,
|
||
"case_id": case_id,
|
||
"status": updated.status,
|
||
"batch_status": current_attempt.status,
|
||
"validation_image_uri": updated.validation_image_uri,
|
||
"fidelity_score": updated.fidelity_score,
|
||
},
|
||
metadata={"pipeline_stage": "style_validation_case_completed" if current_attempt.status != "awaiting_review" else "awaiting_human_review"},
|
||
)
|
||
|
||
def _review_validation_image(
|
||
self,
|
||
*,
|
||
record: Any,
|
||
case: Any,
|
||
validation_image_uri: str,
|
||
) -> tuple[dict[str, Any], float | None]:
|
||
consensus_id = _optional_text(case.metadata.get("style_consensus_id"))
|
||
consensus = self.repository.get_style_consensus_run(consensus_id) if consensus_id else None
|
||
if consensus is None or consensus.status != "succeeded":
|
||
return {"status": "skipped", "reason": "style_consensus_unavailable"}, None
|
||
sources = [item for item in self.repository.list_style_sources(record.import_id) if item.status == "succeeded"]
|
||
source_by_id = {item.source_id: item for item in sources}
|
||
representative_ids = [
|
||
item
|
||
for item in _string_list(consensus.result.get("representative_source_ids"))
|
||
if item in source_by_id
|
||
][:3]
|
||
representative_ids = representative_ids or [item.source_id for item in sources[:3]]
|
||
image_urls: list[str] = []
|
||
image_manifest: list[dict[str, Any]] = []
|
||
try:
|
||
for source_id in representative_ids:
|
||
source = source_by_id[source_id]
|
||
image_url, _, _ = _storage_image_data_url(source.source_image_uri, {}, self.storage)
|
||
image_urls.append(image_url)
|
||
image_manifest.append(
|
||
{
|
||
"image_index": len(image_urls) - 1,
|
||
"kind": "source_reference",
|
||
"source_id": source_id,
|
||
}
|
||
)
|
||
validation_url, _, _ = _storage_image_data_url(validation_image_uri, {}, self.storage)
|
||
image_urls.append(validation_url)
|
||
image_manifest.append(
|
||
{
|
||
"image_index": len(image_urls) - 1,
|
||
"kind": "generated_validation",
|
||
"case_id": case.case_id,
|
||
"case_key": case.case_key,
|
||
}
|
||
)
|
||
except Exception as exc:
|
||
return {"status": "review_failed", "error": _safe_failure(exc)}, None
|
||
route_name = self.owner._consensus_route() or consensus.model_route or record.recognition_model_route or self.owner._recognition_route()
|
||
provider = self.model_provider_runtime.vision_provider_for_route(
|
||
{
|
||
"route_name": route_name,
|
||
"modality": "vision_recognition",
|
||
"material_id": case.case_id,
|
||
"image_uri": validation_image_uri,
|
||
}
|
||
)
|
||
if provider is None:
|
||
return {"status": "review_failed", "reason": "vision_route_unavailable", "route_name": route_name}, None
|
||
request = VisionRecognitionRequest(
|
||
display_name=f"{record.display_name} / {case.case_key} style fidelity review",
|
||
image_uri=image_urls[0],
|
||
image_uris=tuple(image_urls[1:]),
|
||
prompt=json.dumps(
|
||
{
|
||
"image_manifest": image_manifest,
|
||
"consensus_prompt": consensus.prompt,
|
||
"consensus_negative_prompt": consensus.negative_prompt,
|
||
"generation_capsules": consensus.result.get("generation_capsules"),
|
||
"prompt_priority_groups": consensus.result.get("prompt_priority_groups"),
|
||
"scale_capsules": consensus.result.get("scale_capsules"),
|
||
"semantic_risk_audit": consensus.result.get("semantic_risk_audit"),
|
||
"validation_content_prompt": case.content_prompt,
|
||
"actual_generation_prompt": case.final_prompt,
|
||
"validation_scenario": case.case_key,
|
||
},
|
||
ensure_ascii=False,
|
||
),
|
||
metadata={
|
||
"recognition_instruction": _style_fidelity_review_instruction(),
|
||
"style_validation_review": True,
|
||
"style_consensus_id": consensus.consensus_id,
|
||
"style_validation_case_id": case.case_id,
|
||
"image_manifest": image_manifest,
|
||
},
|
||
)
|
||
try:
|
||
recognition = provider.recognize_material(request)
|
||
except VisionProviderError as exc:
|
||
return {"status": "review_failed", "error": _vision_provider_error(exc)}, None
|
||
payload = recognition.structured_data if isinstance(recognition.structured_data, dict) else {}
|
||
dimension_scores = _normalize_style_dimension_scores(payload.get("dimension_scores"))
|
||
provider_score = _score(payload.get("fidelity_score"), recognition.confidence)
|
||
mismatches = _string_list(payload.get("mismatches"))
|
||
style_deltas = _normalize_style_deltas(payload.get("style_deltas"), case_key=case.case_key)
|
||
prompt_patch = _normalize_style_prompt_patch(
|
||
payload.get("prompt_patch"),
|
||
correction_prompt=_optional_text(payload.get("correction_prompt")) or recognition.prompt,
|
||
negative_correction_prompt=_optional_text(payload.get("negative_correction_prompt")) or recognition.negative_prompt,
|
||
)
|
||
score_prompt_patch = (
|
||
prompt_patch
|
||
if isinstance(payload.get("prompt_patch"), dict)
|
||
or _optional_text(payload.get("correction_prompt"))
|
||
or _optional_text(payload.get("negative_correction_prompt"))
|
||
else {}
|
||
)
|
||
semantic_risks = _normalize_semantic_risks(payload.get("semantic_risks"))
|
||
score, score_meta = _weighted_style_fidelity_score(
|
||
case.case_key,
|
||
dimension_scores,
|
||
provider_score,
|
||
mismatches=mismatches,
|
||
deltas=style_deltas,
|
||
prompt_patch=score_prompt_patch,
|
||
semantic_risks=semantic_risks,
|
||
)
|
||
review = {
|
||
"status": "succeeded",
|
||
"schema_version": STYLE_REVIEW_SCHEMA_VERSION,
|
||
"provider_id": recognition.provider_id,
|
||
"provider_model": recognition.model,
|
||
"fidelity_score": score,
|
||
"provider_fidelity_score": provider_score,
|
||
"score_calibration": score_meta,
|
||
"dimension_scores": dimension_scores,
|
||
"matches": _string_list(payload.get("matches")),
|
||
"mismatches": mismatches,
|
||
"style_deltas": style_deltas,
|
||
"prompt_patch": prompt_patch,
|
||
"semantic_risks": semantic_risks,
|
||
"content_leakage": _string_list(payload.get("content_leakage")),
|
||
"correction_prompt": _optional_text(payload.get("correction_prompt")) or recognition.prompt,
|
||
"negative_correction_prompt": _optional_text(payload.get("negative_correction_prompt")) or recognition.negative_prompt,
|
||
"description": recognition.description,
|
||
"image_manifest": image_manifest,
|
||
}
|
||
return review, score
|
||
|
||
def _require_style_set(self, record: Any) -> None:
|
||
if not self.is_style_set(record):
|
||
raise ApiError(ErrorCode.VALIDATION_FAILED, "advanced material import is not a multi-image style set", {"import_id": record.import_id})
|
||
|
||
def _require_sources_editable(self, record: Any) -> None:
|
||
if record.status in {"accepted", "archived", "cancelled"}:
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"advanced style source set is no longer editable",
|
||
{"import_id": record.import_id, "status": record.status},
|
||
)
|
||
latest = self.repository.get_latest_style_consensus_run(record.import_id)
|
||
if latest is not None and latest.status not in {"failed", "cancelled"}:
|
||
raise ApiError(
|
||
ErrorCode.VALIDATION_FAILED,
|
||
"style sources cannot change after a consensus has started",
|
||
{
|
||
"import_id": record.import_id,
|
||
"consensus_id": latest.consensus_id,
|
||
"consensus_status": latest.status,
|
||
},
|
||
)
|
||
|
||
def _require_source(self, import_id: str, source_id: str):
|
||
source = self.repository.get_style_source(source_id)
|
||
if source is None or source.import_id != import_id:
|
||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "advanced style source not found", {"source_id": source_id})
|
||
return source
|
||
|
||
def _require_case(self, import_id: str, case_id: str):
|
||
case = self.repository.get_style_validation_case(case_id)
|
||
if case is None or case.import_id != import_id:
|
||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "advanced style validation case not found", {"case_id": case_id})
|
||
return case
|
||
|
||
|
||
def _is_style_category(category: Any) -> bool:
|
||
name = str(getattr(category, "name", "") or "").strip().casefold()
|
||
metadata = getattr(category, "metadata", None)
|
||
group = str(metadata.get("frontend_group") or "").strip().casefold() if isinstance(metadata, dict) else ""
|
||
category_id = str(getattr(category, "category_id", "") or "").strip().casefold()
|
||
return name in {"风格", "画风", "style"} or group in {"风格", "画风", "style"} or category_id == "cat_imagestudio_风格".casefold()
|
||
|
||
|
||
def _single_style_instruction() -> str:
|
||
return (
|
||
"Analyze only observable visual style and separate it from exact subject identity, costume, pose, props, text, "
|
||
"scene and composition. Extract both surface rendering and structural style grammar. Required structural dimensions "
|
||
"include face width/length and chin shape, eye-to-face ratio, eye contour and iris layers, eyelid/lash design, "
|
||
"nose and mouth simplification, facial age stylization separated from actual age and body maturity, head-to-body ratio, "
|
||
"hand/anatomy simplification, hair mass "
|
||
"and strand grouping, line topology/closure/taper/local color, shadow hue/band count/edge softness, skin blush and "
|
||
"subsurface tones, highlight shape and placement, palette contrast/saturation/value range, texture, background detail "
|
||
"rhythm and optical effects. Do not replace these observations with generic labels such as anime style, beautiful, "
|
||
"high quality or vibrant. Return JSON with prompt, negative_prompt, tags, confidence, description, raw_analysis.attributes, "
|
||
"normalized_asset_profile(category=style, role=style, body_slots=[global_style], attributes, must_preserve and "
|
||
"forbidden_changes), generation_capsules compact/standard/detailed, and source_content_features. Detailed capsule "
|
||
"parts must remain complete semantic phrases, not comma fragments. Never name an artist when traits can be described."
|
||
)
|
||
|
||
|
||
def _consensus_instruction(
|
||
*,
|
||
source_ids: list[str],
|
||
style_adjustment: str | None,
|
||
style_adjustment_plan: dict[str, Any],
|
||
validation_instruction: str | None,
|
||
has_previous_validation: bool,
|
||
) -> str:
|
||
style_clause = (
|
||
"Human style correction. Apply this only to transferable style fields, prompts and generation capsules: "
|
||
f"{style_adjustment}"
|
||
if style_adjustment
|
||
else ""
|
||
)
|
||
validation_clause = (
|
||
"Human validation-content request. Apply this to every validation_cases.content_prompt while adapting framing "
|
||
f"for portrait, fullbody and novel_scene. Do not absorb it into the transferable style prompt: {validation_instruction}"
|
||
if validation_instruction
|
||
else ""
|
||
)
|
||
comparison_clause = (
|
||
"The image_manifest labels source_reference images and previous_validation images. Compare the failed validation "
|
||
"images directly against the source references, diagnose which transferable traits moved in the wrong direction, "
|
||
"and revise the prompt rather than merely repeating the previous wording. Use automatic_review_feedback to distinguish "
|
||
"cross-scenario failures from portrait/fullbody/novel_scene scale-specific failures. A problem repeated in two or more "
|
||
"scenarios is a common-style correction; a problem isolated to one scenario belongs in that scenario's scale capsule. "
|
||
if has_previous_validation
|
||
else ""
|
||
)
|
||
adjustment_plan_clause = (
|
||
"Treat this structured adjustment plan as a hard directional constraint. Never invert decrease, increase, remove "
|
||
f"or preserve instructions: {json.dumps(style_adjustment_plan, ensure_ascii=False)}. "
|
||
if style_adjustment
|
||
else ""
|
||
)
|
||
return (
|
||
"Read every supplied style sample and its visual report. Ignore any source prompt, artist name, tags, title or textual style label; infer transferable style only from image evidence and structured visual observations. Cross-compare them instead of averaging blindly. Identify "
|
||
"features shared by at least 60% of samples as core style, 40-60% as variable, and lower support as content or "
|
||
"an outlier. Separate character identity, age, gender/species, hair, clothing, pose, props, text, setting and "
|
||
"composition from transferable style, while preserving domain-coupled structural stylization such as face proportions, "
|
||
"eye geometry, nose/mouth simplification, head-to-body ratio and anatomy abstraction. Report facial_age_stylization, "
|
||
"actual_age_evidence and body_maturity_proportions separately. Chronological or identity age is not transferable style, "
|
||
"but facial age stylization, eye scale, compact lower-face geometry and anatomy abstraction may be transferable only as soft structural cues. Never convert them into a fixed childlike, youthful or adult identity lock. "
|
||
"The user content prompt controls chronological age, gender presentation, body maturity, build and requested proportions. For full-body generation use a flexible stylized "
|
||
"anatomy band of about 5-7 heads tall only when compatible with that content prompt; never collapse "
|
||
"into chibi/super-deformed 2-4 heads, and never force a mature adult-woman body canon. "
|
||
"Validation subject age band is controlled only by validation_cases.content_prompt / human validation instruction; "
|
||
"when unspecified, keep age-band neutral and let source anatomy style dominate. Sexualized, lingerie, underwear or nude "
|
||
"source content must never be combined with underage identity language. Do not classify a whole subject "
|
||
"class such as young characters, animal-eared characters or dresses as a negative constraint; exclude only exact source "
|
||
"identity and exact designs. Return one JSON object with compatibility keys prompt, negative_prompt, "
|
||
"tags, confidence and description; raw_analysis; normalized_asset_profile(category=style, role=style, "
|
||
"body_slots=[global_style], attributes containing structural and rendering dimensions, and 8-16 concrete must_preserve "
|
||
"phrases); generation_capsules compact/standard/detailed with intact semantic phrases; prompt_priority_groups with "
|
||
"core, structural and rendering arrays totaling no more than 14 non-redundant phrases; scale_capsules keyed by portrait, "
|
||
"fullbody and novel_scene; semantic_risk_audit for wording that could be literalized as an object, effect or composition; "
|
||
"each semantic risk item must include original_phrase, risk, safe_rewrite and negative_guard. Prefer physically grounded "
|
||
"surface/shape/placement descriptions over metaphors or object-like shorthand. consensus_style; "
|
||
"variable_style_features; source_content_features; content_leakage_exclusions; outlier_source_ids; "
|
||
"representative_source_ids (1-3); artist_signature containing 4-8 high-specificity, source-supported painterly traits that distinguish this artist from generic anime. Balance the signature across available dimensions: face_structure, eye_design, hair_rendering, linework, shading, palette, highlights, texture and anatomy. Each signature item must include dimension, phrase, supporting_source_ids, support_ratio and confidence. Prefer concrete observable shape, edge, color and placement relationships over labels such as anime, beautiful or high quality; do not spend all signature slots on one dimension; consensus_confidence; feature_evidence where each item has trait, "
|
||
"supporting_source_ids, support_ratio and confidence; and validation_cases with exactly portrait, fullbody and "
|
||
"novel_scene. Every validation content_prompt must describe a character, clothing, pose, camera and scene "
|
||
"semantically different from all source samples but remain in a comparable illustration domain. Use neutral matte studio "
|
||
"lighting for portrait, neutral daylight for fullbody and ordinary overcast daytime for novel_scene; do not introduce dawn, "
|
||
"sunset, neon, bokeh, lens flare, volumetric light or dramatic backlight. Validation prompts must not contain style wording. "
|
||
"Negative prompts must prevent semi-realistic or generic reinterpretation only when "
|
||
"the source evidence supports stylized anatomy. Valid source IDs are: "
|
||
f"{', '.join(source_ids)}. {comparison_clause}{adjustment_plan_clause}{style_clause} {validation_clause}"
|
||
).strip()
|
||
|
||
|
||
def _force_style_structures(value: dict[str, Any]) -> dict[str, Any]:
|
||
result = deepcopy(value)
|
||
profile = dict(result.get("normalized_asset_profile") or {})
|
||
profile.update({"category": "style", "role": "style", "body_slots": ["global_style"], "wear_layer": None, "stackable": False})
|
||
attributes = dict(profile.get("attributes") or {})
|
||
for key, raw in list(attributes.items()):
|
||
key_text = str(key or "")
|
||
item = dict(raw) if isinstance(raw, dict) else {"value": raw}
|
||
value_text = str(item.get("value") or "")
|
||
if key_text in {"facial_age_stylization"} or key_text.endswith("facial_age_stylization"):
|
||
item["eligible_for_generation"] = True
|
||
item["transfer_role"] = "style_structure"
|
||
item["value"] = _strip_mature_body_locks(value_text) or value_text
|
||
elif (
|
||
key_text in {"body_maturity_proportions", "head_to_body_ratio", "anatomy_scale"}
|
||
or "body_maturity" in key_text
|
||
or "head_to_body" in key_text
|
||
):
|
||
cleaned = _strip_mature_body_locks(value_text)
|
||
item["value"] = cleaned or value_text
|
||
item["eligible_for_generation"] = True
|
||
item["transfer_role"] = "style_structure"
|
||
elif _is_age_attribute_key(key_text):
|
||
item["eligible_for_generation"] = False
|
||
item["transfer_role"] = "reference_only"
|
||
attributes[key] = item
|
||
profile["attributes"] = attributes
|
||
profile["must_preserve"] = _sanitize_style_prompt_parts(profile.get("must_preserve"))
|
||
style_structure_bits: list[str] = []
|
||
for key, raw in attributes.items():
|
||
if not isinstance(raw, dict) or raw.get("transfer_role") != "style_structure":
|
||
continue
|
||
value = _optional_text(raw.get("value"))
|
||
if not value:
|
||
continue
|
||
if key in {"facial_age_stylization"} or str(key).endswith("facial_age_stylization"):
|
||
style_structure_bits.append(f"facial age stylization: {value}")
|
||
elif "body_maturity" in str(key) or "head_to_body" in str(key) or key in {"anatomy_scale"}:
|
||
style_structure_bits.append(value)
|
||
if style_structure_bits:
|
||
profile["must_preserve"] = _unique_prompt_parts([*profile["must_preserve"], *style_structure_bits])
|
||
profile["must_preserve"] = _sanitize_style_prompt_parts(profile["must_preserve"])
|
||
result["normalized_asset_profile"] = profile
|
||
|
||
capsules = result.get("generation_capsules")
|
||
if isinstance(capsules, dict):
|
||
sanitized_capsules: dict[str, Any] = {}
|
||
for tier, raw in capsules.items():
|
||
if not isinstance(raw, dict):
|
||
sanitized_capsules[tier] = raw
|
||
continue
|
||
capsule = dict(raw)
|
||
capsule["prompt_parts"] = _sanitize_style_prompt_parts(capsule.get("prompt_parts"))
|
||
capsule["must_preserve"] = _sanitize_style_prompt_parts(capsule.get("must_preserve"))
|
||
sanitized_capsules[tier] = capsule
|
||
result["generation_capsules"] = sanitized_capsules
|
||
|
||
priorities = result.get("prompt_priority_groups")
|
||
if isinstance(priorities, dict):
|
||
result["prompt_priority_groups"] = {
|
||
key: _sanitize_style_prompt_parts(raw) for key, raw in priorities.items()
|
||
}
|
||
|
||
scales = result.get("scale_capsules")
|
||
if isinstance(scales, dict):
|
||
sanitized_scales: dict[str, Any] = {}
|
||
for case_key, raw in scales.items():
|
||
if isinstance(raw, dict):
|
||
capsule = dict(raw)
|
||
capsule["prompt_parts"] = _sanitize_style_prompt_parts(
|
||
capsule.get("prompt_parts") or capsule.get("positive_additions")
|
||
)
|
||
sanitized_scales[case_key] = capsule
|
||
else:
|
||
sanitized_scales[case_key] = _join_prompt(*_sanitize_style_prompt_parts(raw))
|
||
result["scale_capsules"] = sanitized_scales
|
||
return result
|
||
|
||
|
||
def _style_fidelity_review_instruction() -> str:
|
||
return (
|
||
"The image_manifest identifies 1-3 source_reference images followed by one generated_validation image. Compare only "
|
||
"transferable visual style, never subject identity or exact clothing. Score the generated image against the shared source "
|
||
"style on face proportions, eye geometry and iris construction, nose/mouth simplification and facial age stylization, "
|
||
"source head-to-body ratio / anatomy abstraction, hair mass and strand grouping, "
|
||
"line topology/taper/local color, skin and blush rendering, shadow hue "
|
||
"and edge softness, highlight shape, palette contrast/saturation/value range, texture/detail rhythm and optical effects. "
|
||
"Separate global palette/value range from local edge, eye, hair and clothing color separation. Do not translate a local "
|
||
"contrast failure into a request for globally stronger saturation. Detect phrases in the actual generation prompt that may "
|
||
"have been literalized as an object, lighting effect or composition instead of a surface-bound style property. Return one "
|
||
"strict JSON object with prompt, negative_prompt, confidence, description and fidelity_score; dimension_scores must use the "
|
||
"fixed keys face_structure, eye_design, hair_rendering, linework, palette_global, palette_local, shading, highlights, "
|
||
"anatomy_scale, texture_detail and background_rendering. Also return matches, mismatches; style_deltas as objects with "
|
||
"dimension, scope(global/local/scale_specific), direction, severity, source_target, observed, evidence and scenario; "
|
||
"prompt_patch with positive_additions, negative_additions, replacements, preserve and scale_overrides; semantic_risks with "
|
||
"original_phrase, risk, safe_rewrite and negative_guard; content_leakage; correction_prompt and negative_correction_prompt. "
|
||
"Do not reward generic prettiness, resolution, subject similarity or cinematic detail. A perfect dimension score is invalid "
|
||
"when mismatches, corrective patches, semantic risks or non-zero style deltas are also reported; keep those fields internally consistent."
|
||
)
|
||
|
||
|
||
def _validation_cases(
|
||
provider_value: Any,
|
||
reuse_value: Any,
|
||
*,
|
||
validation_instruction: str | None = None,
|
||
) -> list[dict[str, Any]]:
|
||
candidates = (
|
||
reuse_value
|
||
if not validation_instruction and isinstance(reuse_value, list) and len(reuse_value) == 3
|
||
else provider_value
|
||
)
|
||
mapped: dict[str, dict[str, Any]] = {}
|
||
if isinstance(candidates, list):
|
||
for item in candidates:
|
||
if not isinstance(item, dict):
|
||
continue
|
||
key = str(item.get("case_key") or item.get("type") or item.get("scenario") or "").strip()
|
||
prompt = str(item.get("content_prompt") or "").strip()
|
||
if key in {"portrait", "fullbody", "novel_scene"} and prompt:
|
||
mapped[key] = {"case_key": key, "content_prompt": prompt, "aspect_ratio": item.get("aspect_ratio") or (item.get("metadata") or {}).get("aspect_ratio")}
|
||
result: list[dict[str, Any]] = []
|
||
for default in DEFAULT_VALIDATION_CASES:
|
||
selected = mapped.get(str(default["case_key"]), {})
|
||
case_key = str(default["case_key"])
|
||
content_prompt = str(selected.get("content_prompt") or default["content_prompt"]).strip()
|
||
if validation_instruction:
|
||
content_prompt = _validation_instruction_prompt(case_key, validation_instruction)
|
||
content_prompt = _sanitize_validation_content(case_key, content_prompt)
|
||
result.append(
|
||
{
|
||
**default,
|
||
**selected,
|
||
"content_prompt": content_prompt,
|
||
"aspect_ratio": selected.get("aspect_ratio") or default["aspect_ratio"],
|
||
}
|
||
)
|
||
return result
|
||
|
||
|
||
def _validation_instruction_prompt(case_key: str, instruction: str) -> str:
|
||
framing = {
|
||
"portrait": (
|
||
"For this controlled portrait comparison, preserve every requested identity, hairstyle, color, wardrobe and "
|
||
"requested age-band cues, adapting only the camera to a head-and-shoulders composition with a neutral matte studio background"
|
||
),
|
||
"fullbody": (
|
||
"For this controlled full-body comparison, preserve every requested identity, hairstyle, color, wardrobe and "
|
||
"requested age-band cues, show the complete character from head to toe, preserve stylized facial geometry without forcing facial neoteny or a childlike identity, and use "
|
||
"a flexible subject-appropriate stylized anatomy band of about 5-7 heads tall when compatible with the requested age and build; not chibi, not "
|
||
"super-deformed, and not a forced mature adult body canon"
|
||
),
|
||
"novel_scene": (
|
||
"For this cross-scene comparison, preserve every requested identity, hairstyle, color, wardrobe and requested "
|
||
"age-band cues, then place the same character in a clearly different ordinary overcast daytime scene"
|
||
),
|
||
}.get(case_key, "Preserve every requested subject, wardrobe and age-band detail in a controlled comparison composition")
|
||
return _join_prompt(f"Mandatory human validation request: {instruction}", framing)
|
||
|
||
|
||
|
||
def _extract_head_ratio_range(*values: Any) -> tuple[float, float] | None:
|
||
"""Parse head-count ratios from free text / attribute dumps.
|
||
|
||
Accepts forms like "1:5.5 to 1:6", "5.5-6 heads tall", or "1:6".
|
||
Returns (low, high) in heads-tall units, or None when unknown.
|
||
"""
|
||
found: list[float] = []
|
||
for value in values:
|
||
text = str(value or "")
|
||
if not text.strip():
|
||
continue
|
||
for pattern in _HEAD_RATIO_PATTERNS:
|
||
for match in pattern.finditer(text):
|
||
groups = match.groupdict()
|
||
for key in ("a", "b"):
|
||
raw = groups.get(key)
|
||
if raw is None:
|
||
continue
|
||
try:
|
||
number = float(raw)
|
||
except ValueError:
|
||
continue
|
||
# Convert body:head style "1:6" into heads-tall = 6.
|
||
if number <= 0:
|
||
continue
|
||
if number < 2.5:
|
||
# likely a face-scale factor; ignore
|
||
continue
|
||
if number > 12:
|
||
continue
|
||
found.append(number)
|
||
if not found:
|
||
return None
|
||
low = min(found)
|
||
high = max(found)
|
||
if high < low:
|
||
low, high = high, low
|
||
return (low, high)
|
||
|
||
|
||
def _clamp_head_ratio_range(range_value: tuple[float, float] | None) -> tuple[float, float]:
|
||
if range_value is None:
|
||
return _ANATOMY_SOFT_DEFAULT_HEADS
|
||
low, high = range_value
|
||
low = max(float(low), _ANATOMY_SOFT_MIN_HEADS)
|
||
high = min(float(high), _ANATOMY_SOFT_MAX_HEADS)
|
||
if high < low:
|
||
mid = max(min((float(range_value[0]) + float(range_value[1])) / 2.0, _ANATOMY_SOFT_MAX_HEADS), _ANATOMY_SOFT_MIN_HEADS)
|
||
return (mid, mid)
|
||
# Keep a small readable band.
|
||
if high - low < 0.4:
|
||
mid = (low + high) / 2.0
|
||
low = max(mid - 0.3, _ANATOMY_SOFT_MIN_HEADS)
|
||
high = min(mid + 0.3, _ANATOMY_SOFT_MAX_HEADS)
|
||
return (round(low, 1), round(high, 1))
|
||
|
||
|
||
def _anatomy_soft_band_phrase(range_value: tuple[float, float] | None = None, *, include_negative: bool = True) -> str:
|
||
low, high = _clamp_head_ratio_range(range_value)
|
||
if abs(low - high) < 0.05:
|
||
ratio = f"around {low:.1f} heads tall"
|
||
else:
|
||
ratio = f"around {low:.1f} to {high:.1f} heads tall"
|
||
core = (
|
||
f"flexible subject-appropriate stylized anatomy, {ratio} only when compatible with the requested age and build, "
|
||
"with natural limb length and no fixed childlike or adult body template"
|
||
)
|
||
if include_negative:
|
||
return f"{core}, not chibi or super-deformed"
|
||
return core
|
||
def _source_anatomy_range_from_style_result(result: Any) -> tuple[float, float] | None:
|
||
payload = result if isinstance(result, dict) else {}
|
||
texts: list[Any] = []
|
||
profile = payload.get("normalized_asset_profile") if isinstance(payload.get("normalized_asset_profile"), dict) else {}
|
||
attributes = profile.get("attributes") if isinstance(profile.get("attributes"), dict) else {}
|
||
for key in ("body_maturity_proportions", "head_to_body_ratio", "anatomy_scale"):
|
||
raw = attributes.get(key)
|
||
if isinstance(raw, dict):
|
||
texts.append(raw.get("value"))
|
||
else:
|
||
texts.append(raw)
|
||
texts.extend(_prompt_parts(profile.get("must_preserve")))
|
||
capsules = payload.get("generation_capsules") if isinstance(payload.get("generation_capsules"), dict) else {}
|
||
detailed = capsules.get("detailed") if isinstance(capsules.get("detailed"), dict) else {}
|
||
texts.extend(_prompt_parts(detailed.get("prompt_parts")))
|
||
texts.extend(_prompt_parts(detailed.get("must_preserve")))
|
||
scales = payload.get("scale_capsules") if isinstance(payload.get("scale_capsules"), dict) else {}
|
||
fullbody = scales.get("fullbody")
|
||
if isinstance(fullbody, dict):
|
||
texts.extend(_prompt_parts(fullbody.get("prompt_parts") or fullbody.get("positive_additions")))
|
||
else:
|
||
texts.append(fullbody)
|
||
# Prefer explicit head-to-body "1:N" evidence after stripping mature-body override sentences.
|
||
cleaned = [_strip_mature_body_locks(str(item or "")) for item in texts if item]
|
||
# If any 1:N ratio exists, prefer those over generic "heads tall" leftovers.
|
||
ratio_only = [item for item in cleaned if re.search(r"1\s*:\s*\d", item)]
|
||
return _extract_head_ratio_range(*(ratio_only or cleaned))
|
||
|
||
|
||
def _sanitize_validation_content(case_key: str, value: Any) -> str:
|
||
text, _ = _sanitize_minor_age_text(str(value or "").strip())
|
||
text = _strip_mature_body_locks(text)
|
||
lighting_patterns = (
|
||
r"\bcircular\s+bokeh\b",
|
||
r"\bbokeh\b",
|
||
r"\bneon(?:\s+rim\s+light(?:ing)?)?\b",
|
||
r"\b(?:at|during|near)\s+(?:dawn|sunset|sunrise|golden\s+hour)\b",
|
||
r"\b(?:dawn|sunset|sunrise|golden\s+hour)\b",
|
||
r"\blens\s+flare\b",
|
||
r"\bvolumetric\s+(?:light|lighting|rays?)\b",
|
||
r"\bdramatic\s+backlight(?:ing)?\b",
|
||
r"\bhdr\b",
|
||
)
|
||
for pattern in lighting_patterns:
|
||
text = re.sub(pattern, "", text, flags=re.IGNORECASE)
|
||
text = _clean_prompt_text(text)
|
||
# Age band is owned by the content prompt / human instruction. Do not inject
|
||
# adult or mature-body canon here; only neutralize theatrical lighting.
|
||
suffix = {
|
||
"portrait": (
|
||
"neutral matte studio background, even diffuse studio lighting, no dramatic optical effects"
|
||
),
|
||
"fullbody": (
|
||
f"complete head-to-toe view with {_anatomy_soft_band_phrase()}, neutral daylight"
|
||
),
|
||
"novel_scene": (
|
||
"ordinary overcast daytime, flat diffuse environmental lighting without theatrical optical effects"
|
||
),
|
||
}.get(case_key, "neutral diffuse lighting")
|
||
already_constrained = {
|
||
"portrait": "neutral matte studio background" in text.casefold() and "even diffuse studio lighting" in text.casefold(),
|
||
"fullbody": (
|
||
"head-to-toe" in text.casefold()
|
||
and (
|
||
"heads tall" in text.casefold()
|
||
or "head-to-body" in text.casefold()
|
||
or "not chibi" in text.casefold()
|
||
or "neutral daylight" in text.casefold()
|
||
)
|
||
),
|
||
"novel_scene": "ordinary overcast daytime" in text.casefold() and "diffuse" in text.casefold(),
|
||
}.get(case_key, "neutral diffuse lighting" in text.casefold())
|
||
return text if already_constrained else _join_prompt(text, suffix)
|
||
|
||
|
||
def _is_age_attribute_key(value: Any) -> bool:
|
||
key = re.sub(r"[^a-z0-9]+", "_", str(value or "").strip().casefold()).strip("_")
|
||
return (
|
||
key in _AGE_ATTRIBUTE_KEYS
|
||
or key.endswith("_age")
|
||
or key.startswith("age_")
|
||
or key.startswith("actual_age")
|
||
or key.endswith("_age_evidence")
|
||
or "chronological_age" in key
|
||
)
|
||
|
||
|
||
def _sanitize_minor_age_text(value: str) -> tuple[str, bool]:
|
||
text = str(value or "").strip()
|
||
changed = False
|
||
for pattern, replacement in _MINOR_AGE_REWRITES:
|
||
rewritten, count = re.subn(pattern, replacement, text, flags=re.IGNORECASE)
|
||
if count:
|
||
changed = True
|
||
text = rewritten
|
||
stripped = _strip_mature_body_locks(text)
|
||
if stripped != text:
|
||
changed = True
|
||
text = stripped
|
||
text = re.sub(
|
||
r"\b(?:adult|youthful)(?:\s+(?:adult|youthful))+\b",
|
||
lambda match: match.group(0).split()[0],
|
||
text,
|
||
flags=re.IGNORECASE,
|
||
)
|
||
text = _clean_prompt_text(text)
|
||
return text, changed
|
||
|
||
|
||
def _strip_mature_body_locks(value: str) -> str:
|
||
text = str(value or "")
|
||
for pattern in _MATURE_BODY_LOCK_PATTERNS:
|
||
text = re.sub(pattern, "", text, flags=re.IGNORECASE)
|
||
text = re.sub(
|
||
r"body maturity proportions\s*:\s*[^.]*mature adult proportion[^.]*\.?",
|
||
"",
|
||
text,
|
||
flags=re.IGNORECASE,
|
||
)
|
||
text = re.sub(
|
||
r"(?:,\s*)?but style preservation must adapt to a natural(?:\s+proportion)?(?:\s+during full-body generation)?\.?",
|
||
"",
|
||
text,
|
||
flags=re.IGNORECASE,
|
||
)
|
||
text = re.sub(
|
||
r"style preservation must adapt to a natural(?:\s+proportion)?(?:\s+during full-body generation)?\.?",
|
||
"",
|
||
text,
|
||
flags=re.IGNORECASE,
|
||
)
|
||
# Drop whole mature-body override clauses that survived partial matches.
|
||
text = re.sub(
|
||
r"(?:with\s+)?(?:a\s+)?natural adult body(?:\s+between[^,.;]*)?",
|
||
"",
|
||
text,
|
||
flags=re.IGNORECASE,
|
||
)
|
||
text = re.sub(
|
||
r"(?:with\s+)?mature\s+limb\s+lengths(?:\s+and)?",
|
||
"",
|
||
text,
|
||
flags=re.IGNORECASE,
|
||
)
|
||
text = re.sub(
|
||
r"actual age evidence\s*:\s*[^.]*\.",
|
||
"",
|
||
text,
|
||
flags=re.IGNORECASE,
|
||
)
|
||
# Repair glue left after removals: "with and a,", dangling connectors.
|
||
text = re.sub(r"\b(?:with|and|or|to|of|a|an|the)\s*(?=,|;|\.|$)", "", text, flags=re.IGNORECASE)
|
||
text = re.sub(r"(?:,\s*){2,}", ", ", text)
|
||
text = re.sub(r"\s{2,}", " ", text)
|
||
return _clean_prompt_text(text)
|
||
|
||
|
||
def _is_transferable_style_age_phrase(value: str) -> bool:
|
||
text = str(value or "").casefold()
|
||
if not text:
|
||
return False
|
||
style_markers = (
|
||
"neotenic",
|
||
"facial age stylization",
|
||
"eye-to-face",
|
||
"high forehead",
|
||
"compact lower face",
|
||
"head-to-body",
|
||
"head to body",
|
||
"limb abstraction",
|
||
"stylized",
|
||
"proportions",
|
||
)
|
||
lock_markers = (
|
||
"chronological",
|
||
"actual age",
|
||
"must adapt to",
|
||
"mature adult",
|
||
"explicitly adult",
|
||
"6.5",
|
||
"7.5",
|
||
)
|
||
if any(marker in text for marker in lock_markers) and not any(
|
||
marker in text for marker in ("neotenic", "facial age stylization", "eye-to-face", "head-to-body")
|
||
):
|
||
return False
|
||
if any(marker in text for marker in style_markers):
|
||
return True
|
||
return "age" not in text
|
||
|
||
|
||
def _sanitize_style_prompt_parts(value: Any) -> list[str]:
|
||
result: list[str] = []
|
||
for part in _prompt_parts(value):
|
||
sanitized, _changed = _sanitize_minor_age_text(part)
|
||
sanitized = _strip_mature_body_locks(sanitized)
|
||
if not sanitized:
|
||
continue
|
||
if re.match(r"^(actual age evidence|body maturity proportions)\s*:", sanitized, re.IGNORECASE):
|
||
if re.search(r"mature adult|6\.5|7\.5|must adapt", sanitized, re.IGNORECASE):
|
||
source_ratio = re.search(
|
||
r"(head-to-body ratio[^.]*?1:\s*\d+(?:\.\d+)?(?:\s*to\s*1:\s*\d+(?:\.\d+)?)?[^.,;]*)",
|
||
sanitized,
|
||
re.IGNORECASE,
|
||
)
|
||
if source_ratio:
|
||
result.append(_clean_prompt_text(source_ratio.group(1)))
|
||
continue
|
||
if re.match(r"^facial age stylization\s*:", sanitized, re.IGNORECASE) or _is_transferable_style_age_phrase(sanitized):
|
||
result.append(sanitized)
|
||
continue
|
||
if re.search(r"\b(actual age|chronological age|subject age|character age)\b", sanitized, re.IGNORECASE):
|
||
continue
|
||
result.append(sanitized)
|
||
return _unique_prompt_parts(result)
|
||
|
||
|
||
def _normalize_style_prompt_conflicts(value: Any) -> str:
|
||
text = str(value or "").strip()
|
||
replacements = (
|
||
("stylized large head-to-body proportion", "flexible subject-appropriate head-to-body proportions"),
|
||
("large head-to-body proportion", "flexible subject-appropriate head-to-body proportions"),
|
||
("slim, simplified anatomy", "subject-appropriate stylized anatomy"),
|
||
("mature angular facial structure", "overly angular realistic facial structure"),
|
||
("mature angular facial proportions", "overly angular realistic facial proportions"),
|
||
("mature facial anatomy", "realistic angular facial anatomy"),
|
||
("airy, low-contrast, diffuse pastel background", "soft low-contrast atmospheric treatment over the requested scene"),
|
||
("airy diffuse low-contrast background", "soft low-contrast atmospheric treatment over the requested scene"),
|
||
("airy, low-density diffuse background", "soft low-contrast atmospheric treatment over the requested scene"),
|
||
("airy, low-contrast, diffuse pastel environment", "soft low-contrast atmospheric treatment over the requested scene"),
|
||
("airy, diffuse pastel environment", "soft low-contrast atmospheric treatment over the requested scene"),
|
||
)
|
||
for source, target in replacements:
|
||
text = re.sub(re.escape(source), target, text, flags=re.IGNORECASE)
|
||
return _clean_prompt_text(text)
|
||
|
||
|
||
def _clean_prompt_text(value: str) -> str:
|
||
text = re.sub(r"\s+", " ", str(value or ""))
|
||
text = re.sub(r"\s+([,.;:])", r"\1", text)
|
||
text = re.sub(r"([,;])\s*([,;])+", r"\1", text)
|
||
text = re.sub(r"\b(?:with|and|at|during)\s*,", ",", text, flags=re.IGNORECASE)
|
||
return text.strip(" ,;.-")
|
||
|
||
|
||
def _style_validation_seed(index: int, revision: int, *, refresh_content: bool) -> int:
|
||
base = STYLE_VALIDATION_SEEDS[index]
|
||
return base + max(1, int(revision)) * 1000 if refresh_content else base
|
||
|
||
|
||
def _review_adjustment_summary(style_adjustment: str | None, validation_instruction: str | None) -> str | None:
|
||
parts = []
|
||
if style_adjustment:
|
||
parts.append(f"画风修正:{style_adjustment}")
|
||
if validation_instruction:
|
||
parts.append(f"验证内容:{validation_instruction}")
|
||
return "\n".join(parts) or None
|
||
|
||
|
||
_STYLE_TRAIT_ALIASES: dict[str, tuple[str, ...]] = {
|
||
"color contrast": ("颜色对比", "色彩对比", "明暗对比", "color contrast", "colour contrast", "contrast"),
|
||
"color saturation": ("颜色饱和", "色彩饱和", "饱和度", "鲜艳度", "color saturation", "colour saturation", "saturation"),
|
||
"brightness": ("亮度", "明度", "brightness", "luminance"),
|
||
"shadow intensity": ("阴影强度", "阴影浓度", "阴影", "shadow intensity", "shadow strength", "shadows"),
|
||
"highlight intensity": ("高光强度", "高光", "highlight intensity", "highlight strength", "highlights"),
|
||
"line weight": ("线条粗细", "线宽", "线条", "line weight", "line thickness"),
|
||
"detail density": ("细节密度", "细节", "detail density", "level of detail"),
|
||
"texture intensity": ("纹理强度", "纹理", "texture intensity", "texture"),
|
||
}
|
||
|
||
_STYLE_DECREASE_MARKERS = (
|
||
"太强", "太重", "太浓", "太高", "太过", "过于", "过度", "降低", "减弱", "减少", "收敛",
|
||
"too strong", "too intense", "too high", "overly", "excessive", "reduce", "decrease", "less",
|
||
)
|
||
_STYLE_INCREASE_MARKERS = (
|
||
"太弱", "太淡", "太低", "不足", "加强", "增强", "增加", "提高",
|
||
"too weak", "too low", "insufficient", "increase", "strengthen", "more",
|
||
)
|
||
_STYLE_REMOVE_MARKERS = ("不要", "去掉", "移除", "删除", "取消", "remove", "eliminate", "without")
|
||
_STYLE_PRESERVE_MARKERS = ("保持", "保留", "不要改变", "维持", "preserve", "retain", "keep")
|
||
|
||
_STYLE_DECREASE_GUARDS: dict[str, dict[str, list[str]]] = {
|
||
"color contrast": {
|
||
"positive_forbidden": [
|
||
"intense color contrast", "extreme color contrast", "high color contrast", "high contrast",
|
||
"strong contrast", "dramatic contrast", "extreme color dynamics",
|
||
],
|
||
"negative_forbidden": ["low contrast", "washed out", "flat contrast", "muted contrast"],
|
||
"positive_add": ["balanced moderate color contrast", "restrained color separation"],
|
||
"negative_add": ["excessive color contrast", "extreme color dynamics", "oversaturated color separation"],
|
||
},
|
||
"color saturation": {
|
||
"positive_forbidden": ["high saturation", "highly saturated", "extreme saturation", "oversaturated", "vivid saturated"],
|
||
"negative_forbidden": ["low saturation", "desaturated", "muted colors", "washed out"],
|
||
"positive_add": ["controlled moderate color saturation", "restrained chroma"],
|
||
"negative_add": ["oversaturated colors", "extreme chroma", "neon color overload"],
|
||
},
|
||
"brightness": {
|
||
"positive_forbidden": ["extreme brightness", "very bright", "blown-out highlights"],
|
||
"negative_forbidden": ["dim", "dark exposure", "underexposed"],
|
||
"positive_add": ["balanced moderate brightness"],
|
||
"negative_add": ["excessive brightness", "overexposure"],
|
||
},
|
||
"shadow intensity": {
|
||
"positive_forbidden": ["heavy shadows", "deep harsh shadows", "intense shadows", "strong shadows"],
|
||
"negative_forbidden": ["weak shadows", "shadowless", "flat lighting"],
|
||
"positive_add": ["controlled moderate shadow intensity"],
|
||
"negative_add": ["excessively heavy shadows", "crushed shadow detail"],
|
||
},
|
||
"highlight intensity": {
|
||
"positive_forbidden": ["intense highlights", "extreme highlights", "strong glossy highlights"],
|
||
"negative_forbidden": ["no highlights", "dull highlights"],
|
||
"positive_add": ["controlled restrained highlights"],
|
||
"negative_add": ["blown-out highlights", "excessive glossy glare"],
|
||
},
|
||
}
|
||
|
||
|
||
def _parse_style_adjustment(value: str | None) -> dict[str, Any]:
|
||
text = _optional_text(value)
|
||
plan: dict[str, Any] = {
|
||
"increase": [],
|
||
"decrease": [],
|
||
"remove": [],
|
||
"preserve": [],
|
||
"unparsed_feedback": [],
|
||
"raw_text": text,
|
||
"parser_version": 1,
|
||
}
|
||
if not text:
|
||
return plan
|
||
matched = False
|
||
clauses = [item.strip() for item in re.split(r"[,,。;;\n]+", text.casefold()) if item.strip()]
|
||
for clause in clauses:
|
||
traits = [
|
||
trait
|
||
for trait, aliases in _STYLE_TRAIT_ALIASES.items()
|
||
if any(alias.casefold() in clause for alias in aliases)
|
||
]
|
||
if not traits:
|
||
continue
|
||
matched = True
|
||
decrease_not_remove = any(marker in clause for marker in ("不要太", "不要过", "not too"))
|
||
if any(marker in clause for marker in _STYLE_PRESERVE_MARKERS):
|
||
direction = "preserve"
|
||
elif any(marker in clause for marker in _STYLE_REMOVE_MARKERS) and not decrease_not_remove:
|
||
direction = "remove"
|
||
elif decrease_not_remove or any(marker in clause for marker in _STYLE_DECREASE_MARKERS):
|
||
direction = "decrease"
|
||
elif any(marker in clause for marker in _STYLE_INCREASE_MARKERS):
|
||
direction = "increase"
|
||
else:
|
||
plan["unparsed_feedback"].append(clause)
|
||
continue
|
||
for trait in traits:
|
||
if trait not in plan[direction]:
|
||
plan[direction].append(trait)
|
||
if not matched:
|
||
plan["unparsed_feedback"].append(text)
|
||
return plan
|
||
|
||
|
||
def _style_adjustment_plan(value: Any, fallback_text: str | None) -> dict[str, Any]:
|
||
if not isinstance(value, dict):
|
||
return _parse_style_adjustment(fallback_text)
|
||
result = {
|
||
"increase": _string_list(value.get("increase")),
|
||
"decrease": _string_list(value.get("decrease")),
|
||
"remove": _string_list(value.get("remove")),
|
||
"preserve": _string_list(value.get("preserve")),
|
||
"unparsed_feedback": _string_list(value.get("unparsed_feedback")),
|
||
"raw_text": _optional_text(value.get("raw_text")) or _optional_text(fallback_text),
|
||
"parser_version": int(value.get("parser_version") or 1),
|
||
}
|
||
if not any(result[key] for key in ("increase", "decrease", "remove", "preserve", "unparsed_feedback")):
|
||
return _parse_style_adjustment(fallback_text)
|
||
return result
|
||
|
||
|
||
def _apply_style_adjustment_guard(
|
||
prompt: str | None,
|
||
negative_prompt: str | None,
|
||
structures: dict[str, Any],
|
||
plan: dict[str, Any],
|
||
) -> tuple[str, str | None, dict[str, Any], dict[str, Any]]:
|
||
positive = str(prompt or "").strip()
|
||
negative = _optional_text(negative_prompt)
|
||
guarded = deepcopy(structures)
|
||
removed_positive: list[str] = []
|
||
removed_negative: list[str] = []
|
||
positive_additions: list[str] = []
|
||
negative_additions: list[str] = []
|
||
for trait in _string_list(plan.get("decrease")):
|
||
rules = _STYLE_DECREASE_GUARDS.get(
|
||
trait,
|
||
{
|
||
"positive_forbidden": [f"extreme {trait}", f"intense {trait}", f"high {trait}", f"strong {trait}"],
|
||
"negative_forbidden": [f"low {trait}", f"weak {trait}"],
|
||
"positive_add": [f"balanced moderate {trait}", f"restrained {trait}"],
|
||
"negative_add": [f"excessive {trait}", f"extreme {trait}"],
|
||
},
|
||
)
|
||
positive, removed = _remove_prompt_phrases(positive, rules["positive_forbidden"])
|
||
removed_positive.extend(removed)
|
||
negative, removed = _remove_prompt_phrases(negative, rules["negative_forbidden"])
|
||
removed_negative.extend(removed)
|
||
positive_additions.extend(rules["positive_add"])
|
||
negative_additions.extend(rules["negative_add"])
|
||
for trait in _string_list(plan.get("increase")):
|
||
positive_additions.append(f"clearly pronounced {trait}")
|
||
negative, removed = _remove_prompt_phrases(negative, [f"high {trait}", f"strong {trait}", f"pronounced {trait}"])
|
||
removed_negative.extend(removed)
|
||
for trait in _string_list(plan.get("remove")):
|
||
positive, removed = _remove_prompt_phrases(positive, [trait])
|
||
removed_positive.extend(removed)
|
||
negative_additions.append(trait)
|
||
for trait in _string_list(plan.get("preserve")):
|
||
positive_additions.append(f"preserve source-reference {trait}")
|
||
positive = _join_prompt(positive, *positive_additions)
|
||
negative = _join_prompt(negative, *negative_additions) or None
|
||
|
||
capsules = guarded.get("generation_capsules")
|
||
if isinstance(capsules, dict):
|
||
for tier, raw_capsule in list(capsules.items()):
|
||
if not isinstance(raw_capsule, dict):
|
||
continue
|
||
capsule = dict(raw_capsule)
|
||
capsule_positive_parts = _prompt_parts(capsule.get("prompt_parts"))
|
||
capsule_negative_parts = _prompt_parts(capsule.get("negative_parts"))
|
||
for trait in _string_list(plan.get("decrease")):
|
||
rules = _STYLE_DECREASE_GUARDS.get(
|
||
trait,
|
||
{
|
||
"positive_forbidden": [f"extreme {trait}", f"intense {trait}", f"high {trait}", f"strong {trait}"],
|
||
"negative_forbidden": [f"low {trait}", f"weak {trait}"],
|
||
"positive_add": [f"balanced moderate {trait}", f"restrained {trait}"],
|
||
"negative_add": [f"excessive {trait}", f"extreme {trait}"],
|
||
},
|
||
)
|
||
capsule_positive_parts, _ = _remove_forbidden_from_parts(
|
||
capsule_positive_parts,
|
||
rules["positive_forbidden"],
|
||
)
|
||
capsule_negative_parts, _ = _remove_forbidden_from_parts(
|
||
capsule_negative_parts,
|
||
rules["negative_forbidden"],
|
||
)
|
||
capsule["prompt_parts"] = _unique_prompt_parts([*capsule_positive_parts, *positive_additions])
|
||
capsule["negative_parts"] = _unique_prompt_parts([*capsule_negative_parts, *negative_additions])
|
||
capsule["style_adjustment_guarded"] = True
|
||
capsules[tier] = capsule
|
||
return (
|
||
positive,
|
||
negative,
|
||
guarded,
|
||
{
|
||
"applied": bool(positive_additions or negative_additions or removed_positive or removed_negative),
|
||
"removed_positive_parts": list(dict.fromkeys(removed_positive)),
|
||
"removed_negative_parts": list(dict.fromkeys(removed_negative)),
|
||
"positive_additions": list(dict.fromkeys(positive_additions)),
|
||
"negative_additions": list(dict.fromkeys(negative_additions)),
|
||
},
|
||
)
|
||
|
||
|
||
def _remove_prompt_phrases(value: str | None, forbidden: list[str]) -> tuple[str, list[str]]:
|
||
parts = [item.strip() for item in re.split(r"[,;\n]+", str(value or "")) if item.strip()]
|
||
removed = [item for item in parts if any(token.casefold() in item.casefold() for token in forbidden)]
|
||
kept = [item for item in parts if item not in removed]
|
||
return ", ".join(kept), removed
|
||
|
||
|
||
def _prompt_parts(value: Any) -> list[str]:
|
||
if isinstance(value, str):
|
||
text = value.strip()
|
||
return [text] if text else []
|
||
if not isinstance(value, (list, tuple, set)):
|
||
return []
|
||
return _unique_prompt_parts([str(item).strip() for item in value if str(item).strip()])
|
||
|
||
|
||
def _unique_prompt_parts(values: list[str]) -> list[str]:
|
||
result: list[str] = []
|
||
seen: set[str] = set()
|
||
for value in values:
|
||
text = str(value or "").strip(" ,;")
|
||
key = text.casefold()
|
||
if text and key not in seen:
|
||
seen.add(key)
|
||
result.append(text)
|
||
return result
|
||
|
||
|
||
def _remove_forbidden_from_parts(parts: list[str], forbidden: list[str]) -> tuple[list[str], list[str]]:
|
||
kept: list[str] = []
|
||
removed: list[str] = []
|
||
for part in parts:
|
||
rewritten = part
|
||
for phrase in forbidden:
|
||
pattern = re.compile(re.escape(phrase), re.IGNORECASE)
|
||
if pattern.search(rewritten):
|
||
removed.append(phrase)
|
||
rewritten = pattern.sub("", rewritten)
|
||
rewritten = re.sub(r"\s*,\s*,+", ", ", rewritten).strip(" ,;")
|
||
if rewritten:
|
||
kept.append(rewritten)
|
||
return _unique_prompt_parts(kept), list(dict.fromkeys(removed))
|
||
|
||
|
||
def _normalize_style_dimension_scores(value: Any) -> dict[str, float]:
|
||
if not isinstance(value, dict):
|
||
return {}
|
||
collected: dict[str, list[float]] = {}
|
||
for raw_key, raw_value in value.items():
|
||
key = _canonical_style_dimension(raw_key)
|
||
if key is None:
|
||
continue
|
||
candidate = raw_value.get("score") if isinstance(raw_value, dict) else raw_value
|
||
score = _score(candidate)
|
||
if score is not None:
|
||
collected.setdefault(key, []).append(score)
|
||
return {key: sum(scores) / len(scores) for key, scores in collected.items() if scores}
|
||
|
||
|
||
def _canonical_style_dimension(value: Any) -> str | None:
|
||
normalized = re.sub(r"[^a-z0-9]+", "_", str(value or "").strip().casefold()).strip("_")
|
||
if not normalized:
|
||
return None
|
||
for canonical in STYLE_REVIEW_DIMENSIONS:
|
||
aliases = (canonical, *STYLE_REVIEW_DIMENSION_ALIASES.get(canonical, ()))
|
||
if normalized in {re.sub(r"[^a-z0-9]+", "_", alias.casefold()).strip("_") for alias in aliases}:
|
||
return canonical
|
||
return None
|
||
|
||
|
||
def _weighted_style_fidelity_score(
|
||
case_key: Any,
|
||
scores: dict[str, float],
|
||
provider_score: float | None,
|
||
*,
|
||
mismatches: list[str] | None = None,
|
||
deltas: list[dict[str, Any]] | None = None,
|
||
prompt_patch: dict[str, Any] | None = None,
|
||
semantic_risks: list[dict[str, Any]] | None = None,
|
||
) -> tuple[float | None, dict[str, Any]]:
|
||
weights = STYLE_REVIEW_WEIGHTS.get(str(case_key), STYLE_REVIEW_WEIGHTS["novel_scene"])
|
||
available_weight = sum(weights.get(key, 0.0) for key in scores)
|
||
if available_weight > 0:
|
||
computed = sum(scores[key] * weights.get(key, 0.0) for key in scores) / available_weight
|
||
if provider_score is not None and available_weight < 0.75:
|
||
score = computed * available_weight + provider_score * (1.0 - available_weight)
|
||
else:
|
||
score = computed
|
||
if available_weight < 0.5:
|
||
score = min(score, 0.65 + 0.35 * available_weight)
|
||
source = "server_weighted_dimensions"
|
||
else:
|
||
score = provider_score
|
||
computed = None
|
||
source = "provider_fallback"
|
||
|
||
mismatch_items = _string_list(mismatches)
|
||
delta_items = [item for item in _list(deltas) if isinstance(item, dict)]
|
||
patch = prompt_patch if isinstance(prompt_patch, dict) else {}
|
||
risk_items = [item for item in _list(semantic_risks) if isinstance(item, dict)]
|
||
patch_count = (
|
||
len(_prompt_parts(patch.get("positive_additions")))
|
||
+ len(_prompt_parts(patch.get("negative_additions")))
|
||
+ len([item for item in _list(patch.get("replacements")) if isinstance(item, dict)])
|
||
+ sum(
|
||
len(_prompt_parts(item.get("positive_additions"))) + len(_prompt_parts(item.get("negative_additions")))
|
||
for item in _object(patch.get("scale_overrides")).values()
|
||
if isinstance(item, dict)
|
||
)
|
||
)
|
||
mismatch_penalty = min(0.12, 0.025 * len(mismatch_items))
|
||
delta_penalty = min(
|
||
0.16,
|
||
sum(float(_score(item.get("severity"), 0.5) or 0.0) * 0.08 for item in delta_items),
|
||
)
|
||
patch_penalty = min(0.10, 0.02 * patch_count)
|
||
semantic_risk_penalty = min(0.12, 0.03 * len(risk_items))
|
||
has_correction_text = bool(mismatch_items or delta_items or patch_count or risk_items)
|
||
all_reported_perfect = bool(scores) and all(value >= 0.98 for value in scores.values())
|
||
review_inconsistent = all_reported_perfect and has_correction_text
|
||
inconsistency_penalty = 0.08 if review_inconsistent else 0.0
|
||
consistency_penalty = (
|
||
mismatch_penalty
|
||
+ delta_penalty
|
||
+ patch_penalty
|
||
+ semantic_risk_penalty
|
||
+ inconsistency_penalty
|
||
)
|
||
if score is not None:
|
||
score = max(0.0, score - consistency_penalty)
|
||
if provider_score is not None:
|
||
score = min(score, provider_score)
|
||
return (
|
||
_score(score),
|
||
{
|
||
"schema_version": STYLE_REVIEW_SCHEMA_VERSION,
|
||
"source": source,
|
||
"scenario": str(case_key),
|
||
"dimension_coverage": round(available_weight, 4),
|
||
"computed_dimension_score": _score(computed),
|
||
"provider_score": provider_score,
|
||
"missing_dimensions": [key for key in STYLE_REVIEW_DIMENSIONS if key not in scores and weights.get(key, 0.0) > 0],
|
||
"review_inconsistent": review_inconsistent,
|
||
"consistency_penalty": round(consistency_penalty, 4),
|
||
"penalty_breakdown": {
|
||
"mismatches": round(mismatch_penalty, 4),
|
||
"style_deltas": round(delta_penalty, 4),
|
||
"prompt_patch": round(patch_penalty, 4),
|
||
"semantic_risks": round(semantic_risk_penalty, 4),
|
||
"internal_inconsistency": round(inconsistency_penalty, 4),
|
||
},
|
||
},
|
||
)
|
||
|
||
|
||
def _normalize_style_deltas(value: Any, *, case_key: Any) -> list[dict[str, Any]]:
|
||
if not isinstance(value, list):
|
||
return []
|
||
result: list[dict[str, Any]] = []
|
||
for item in value:
|
||
if not isinstance(item, dict):
|
||
continue
|
||
dimension = _canonical_style_dimension(item.get("dimension"))
|
||
if dimension is None:
|
||
continue
|
||
scope = str(item.get("scope") or "scale_specific").strip().casefold()
|
||
if scope not in {"global", "local", "scale_specific"}:
|
||
scope = "scale_specific"
|
||
raw_severity = item.get("severity")
|
||
severity = STYLE_DELTA_SEVERITY.get(str(raw_severity or "").strip().casefold())
|
||
if severity is None:
|
||
severity = _score(raw_severity, 0.5)
|
||
result.append(
|
||
{
|
||
"dimension": dimension,
|
||
"scope": scope,
|
||
"direction": _optional_text(item.get("direction")),
|
||
"severity": severity,
|
||
"source_target": _optional_text(item.get("source_target")),
|
||
"observed": _optional_text(item.get("observed")),
|
||
"evidence": _optional_text(item.get("evidence")),
|
||
"scenario": _optional_text(item.get("scenario")) or str(case_key),
|
||
}
|
||
)
|
||
return result
|
||
|
||
|
||
def _normalize_style_prompt_patch(
|
||
value: Any,
|
||
*,
|
||
correction_prompt: str | None,
|
||
negative_correction_prompt: str | None,
|
||
) -> dict[str, Any]:
|
||
payload = value if isinstance(value, dict) else {}
|
||
replacements = []
|
||
for item in _list(payload.get("replacements")):
|
||
if not isinstance(item, dict):
|
||
continue
|
||
original = _optional_text(item.get("from") or item.get("original_phrase"))
|
||
rewrite = _optional_text(item.get("to") or item.get("safe_rewrite"))
|
||
if original and rewrite and original.casefold() != rewrite.casefold():
|
||
replacements.append({"from": original, "to": rewrite, "reason": _optional_text(item.get("reason"))})
|
||
positive = _prompt_parts(payload.get("positive_additions"))
|
||
negative = _prompt_parts(payload.get("negative_additions"))
|
||
if not positive and correction_prompt:
|
||
positive = [correction_prompt]
|
||
if not negative and negative_correction_prompt:
|
||
negative = [negative_correction_prompt]
|
||
scale_overrides: dict[str, Any] = {}
|
||
raw_scales = payload.get("scale_overrides")
|
||
if isinstance(raw_scales, dict):
|
||
for case_key in ("portrait", "fullbody", "novel_scene"):
|
||
raw = raw_scales.get(case_key)
|
||
if isinstance(raw, dict):
|
||
scale_overrides[case_key] = {
|
||
"positive_additions": _prompt_parts(raw.get("positive_additions") or raw.get("prompt_parts")),
|
||
"negative_additions": _prompt_parts(raw.get("negative_additions") or raw.get("negative_parts")),
|
||
}
|
||
elif raw:
|
||
scale_overrides[case_key] = {"positive_additions": _prompt_parts(raw), "negative_additions": []}
|
||
return {
|
||
"positive_additions": _unique_prompt_parts(positive),
|
||
"negative_additions": _unique_prompt_parts(negative),
|
||
"replacements": replacements,
|
||
"preserve": _prompt_parts(payload.get("preserve")),
|
||
"scale_overrides": scale_overrides,
|
||
}
|
||
|
||
|
||
def _normalize_semantic_risks(value: Any) -> list[dict[str, Any]]:
|
||
if not isinstance(value, list):
|
||
return []
|
||
result = []
|
||
for item in value:
|
||
if not isinstance(item, dict):
|
||
continue
|
||
original = _optional_text(item.get("original_phrase"))
|
||
rewrite = _optional_text(item.get("safe_rewrite"))
|
||
if original or rewrite:
|
||
result.append(
|
||
{
|
||
"original_phrase": original,
|
||
"risk": _optional_text(item.get("risk")),
|
||
"safe_rewrite": rewrite,
|
||
"negative_guard": _optional_text(item.get("negative_guard")),
|
||
}
|
||
)
|
||
return result
|
||
|
||
|
||
def _aggregate_style_review_feedback(cases: list[Any]) -> dict[str, Any]:
|
||
reviewed = []
|
||
dimension_values: dict[str, list[float]] = {}
|
||
weak_scenarios: dict[str, list[str]] = {}
|
||
deltas: list[dict[str, Any]] = []
|
||
prompt_patches: list[dict[str, Any]] = []
|
||
semantic_risks: list[dict[str, Any]] = []
|
||
scenario_scores: dict[str, float] = {}
|
||
for case in cases:
|
||
review = case.style_review if isinstance(getattr(case, "style_review", None), dict) else {}
|
||
if review.get("status") != "succeeded":
|
||
continue
|
||
scenario = str(case.case_key)
|
||
reviewed.append(str(case.case_id))
|
||
score = _score(review.get("fidelity_score"), getattr(case, "fidelity_score", None))
|
||
if score is not None:
|
||
scenario_scores[scenario] = score
|
||
scores = _normalize_style_dimension_scores(review.get("dimension_scores"))
|
||
for dimension, dimension_score in scores.items():
|
||
dimension_values.setdefault(dimension, []).append(dimension_score)
|
||
if dimension_score < 0.72:
|
||
weak_scenarios.setdefault(dimension, []).append(scenario)
|
||
deltas.extend(_normalize_style_deltas(review.get("style_deltas"), case_key=scenario))
|
||
patch = review.get("prompt_patch")
|
||
if isinstance(patch, dict):
|
||
prompt_patches.append({"scenario": scenario, **patch})
|
||
semantic_risks.extend(_normalize_semantic_risks(review.get("semantic_risks")))
|
||
recurrent = sorted(dimension for dimension, scenarios in weak_scenarios.items() if len(set(scenarios)) >= 2)
|
||
scale_specific = {
|
||
scenario: sorted(dimension for dimension, scenarios in weak_scenarios.items() if scenario in scenarios and dimension not in recurrent)
|
||
for scenario in ("portrait", "fullbody", "novel_scene")
|
||
}
|
||
return {
|
||
"schema_version": STYLE_REVIEW_SCHEMA_VERSION,
|
||
"reviewed_case_ids": reviewed,
|
||
"scenario_scores": scenario_scores,
|
||
"dimension_averages": {
|
||
key: sum(values) / len(values) for key, values in dimension_values.items() if values
|
||
},
|
||
"recurrent_weak_dimensions": recurrent,
|
||
"scale_specific_weak_dimensions": scale_specific,
|
||
"style_deltas": deltas,
|
||
"prompt_patches": prompt_patches,
|
||
"semantic_risks": semantic_risks,
|
||
"correction_policy": {
|
||
"common_style": "apply only findings repeated across at least two scenarios or explicitly marked global",
|
||
"scale_specific": "keep isolated findings inside the matching portrait/fullbody/novel_scene capsule",
|
||
"color": "separate global palette/value range from local color separation and edge contrast",
|
||
"semantic_safety": "replace object-like metaphors with surface, geometry and placement language",
|
||
},
|
||
}
|
||
|
||
|
||
def _style_attempt_comparison(
|
||
*,
|
||
current_attempt: Any,
|
||
current_cases: list[Any],
|
||
previous_attempt: Any | None,
|
||
previous_cases: list[Any],
|
||
) -> dict[str, Any]:
|
||
current_score = _score(getattr(current_attempt, "fidelity_score", None))
|
||
previous_score = _score(getattr(previous_attempt, "fidelity_score", None)) if previous_attempt is not None else None
|
||
delta = current_score - previous_score if current_score is not None and previous_score is not None else None
|
||
if delta is None:
|
||
recommendation = "baseline"
|
||
elif delta >= 0.02:
|
||
recommendation = "adopt_candidate"
|
||
elif delta <= -0.02:
|
||
recommendation = "keep_previous_candidate"
|
||
else:
|
||
recommendation = "equivalent_requires_human_review"
|
||
current_by_key = {str(item.case_key): _score(item.fidelity_score) for item in current_cases}
|
||
previous_by_key = {str(item.case_key): _score(item.fidelity_score) for item in previous_cases}
|
||
scenario_deltas = {
|
||
key: current_by_key[key] - previous_by_key[key]
|
||
for key in current_by_key.keys() & previous_by_key.keys()
|
||
if current_by_key[key] is not None and previous_by_key[key] is not None
|
||
}
|
||
return {
|
||
"schema_version": 1,
|
||
"current_attempt_id": str(current_attempt.attempt_id),
|
||
"previous_attempt_id": str(previous_attempt.attempt_id) if previous_attempt is not None else None,
|
||
"current_score": current_score,
|
||
"previous_score": previous_score,
|
||
"score_delta": delta,
|
||
"scenario_deltas": scenario_deltas,
|
||
"recommendation": recommendation,
|
||
"regression_threshold": 0.02,
|
||
}
|
||
|
||
|
||
def _normalized_feature_evidence(value: Any) -> list[dict[str, Any]]:
|
||
result: list[dict[str, Any]] = []
|
||
for item in _list(value):
|
||
if not isinstance(item, dict):
|
||
continue
|
||
trait = _optional_text(item.get("trait"))
|
||
support = _score(item.get("support_ratio"))
|
||
confidence = _score(item.get("confidence"))
|
||
if trait:
|
||
result.append(
|
||
{
|
||
"trait": trait,
|
||
"support_ratio": support,
|
||
"confidence": confidence,
|
||
"supported": bool(
|
||
support is not None
|
||
and confidence is not None
|
||
and support >= STYLE_EVIDENCE_MIN_SUPPORT
|
||
and confidence >= STYLE_EVIDENCE_MIN_CONFIDENCE
|
||
),
|
||
}
|
||
)
|
||
return result
|
||
|
||
|
||
def _style_trait_is_supported(
|
||
value: Any,
|
||
evidence: list[dict[str, Any]],
|
||
*,
|
||
required: bool,
|
||
) -> bool:
|
||
text = str(value or "").strip()
|
||
if not text:
|
||
return False
|
||
if _contains_minor_age_term(text):
|
||
return False
|
||
matching = [item for item in evidence if _prompt_traits_overlap(text, item.get("trait"))]
|
||
if matching:
|
||
return any(bool(item.get("supported")) for item in matching)
|
||
return not required
|
||
|
||
|
||
def _contains_minor_age_term(value: Any) -> bool:
|
||
text = str(value or "")
|
||
return any(re.search(pattern, text, re.IGNORECASE) for pattern, _ in _MINOR_AGE_REWRITES)
|
||
|
||
|
||
def _prompt_tokens(value: Any) -> set[str]:
|
||
return {
|
||
item
|
||
for item in re.findall(r"[a-z0-9]+", str(value or "").casefold())
|
||
if len(item) > 2
|
||
and item
|
||
not in {
|
||
"with", "from", "into", "style", "visual", "preserve", "across", "selected",
|
||
"directly", "while", "only", "subject", "character",
|
||
}
|
||
}
|
||
|
||
|
||
def _prompt_traits_overlap(left: Any, right: Any) -> bool:
|
||
left_text = str(left or "").strip().casefold()
|
||
right_text = str(right or "").strip().casefold()
|
||
if not left_text or not right_text:
|
||
return False
|
||
if left_text in right_text or right_text in left_text:
|
||
return True
|
||
left_tokens = _prompt_tokens(left_text)
|
||
right_tokens = _prompt_tokens(right_text)
|
||
if not left_tokens or not right_tokens:
|
||
return False
|
||
overlap = len(left_tokens & right_tokens) / max(1, min(len(left_tokens), len(right_tokens)))
|
||
return overlap >= 0.72
|
||
|
||
|
||
def _split_prompt_clauses(value: Any) -> list[str]:
|
||
if isinstance(value, (list, tuple, set)):
|
||
result: list[str] = []
|
||
for item in value:
|
||
result.extend(_split_prompt_clauses(item))
|
||
return _unique_prompt_parts(result)
|
||
return _unique_prompt_parts(
|
||
[item.strip() for item in re.split(r"[,;\n]+", str(value or "")) if item.strip()]
|
||
)
|
||
|
||
|
||
def _semantic_phrase_is_object_risky(value: Any) -> bool:
|
||
return bool(_SEMANTIC_OBJECT_RISK_PATTERNS.search(str(value or "")))
|
||
|
||
|
||
def _consensus_source_analysis(value: Any) -> dict[str, Any]:
|
||
"""Keep visual evidence while removing textual artist/style anchors."""
|
||
def scrub(item: Any) -> Any:
|
||
if isinstance(item, dict):
|
||
result: dict[str, Any] = {}
|
||
for key, raw in item.items():
|
||
key_text = str(key or "").casefold().replace("-", "_")
|
||
if key_text in {
|
||
"prompt",
|
||
"negative_prompt",
|
||
"tags",
|
||
"artist",
|
||
"artist_name",
|
||
"style_name",
|
||
"creator",
|
||
"author",
|
||
}:
|
||
continue
|
||
cleaned = scrub(raw)
|
||
if cleaned is not None:
|
||
result[key] = cleaned
|
||
return result
|
||
if isinstance(item, list):
|
||
result = []
|
||
for raw in item:
|
||
cleaned = scrub(raw)
|
||
if cleaned is not None:
|
||
result.append(cleaned)
|
||
return result
|
||
if isinstance(item, str) and re.search(r"\bstyle\s+of\b|\bby\s+(?:artist|author)\b", item, re.IGNORECASE):
|
||
return None
|
||
return item
|
||
|
||
cleaned = scrub(value if isinstance(value, dict) else {})
|
||
return cleaned if isinstance(cleaned, dict) else {}
|
||
|
||
|
||
def _normalize_artist_signature(value: Any, sources: list[Any]) -> list[dict[str, Any]]:
|
||
candidates: list[dict[str, Any]] = []
|
||
source_count = max(1, len(sources))
|
||
if isinstance(value, list):
|
||
for item in value:
|
||
if isinstance(item, dict):
|
||
phrase = _optional_text(item.get("phrase") or item.get("trait") or item.get("description"))
|
||
support = _optional_score(item.get("support_ratio") or item.get("support"))
|
||
source_ids = _string_list(item.get("supporting_source_ids") or item.get("source_ids"))
|
||
dimension = _artist_signature_dimension(item.get("dimension"), phrase)
|
||
confidence = _optional_score(item.get("confidence"))
|
||
else:
|
||
phrase = _optional_text(item)
|
||
support = None
|
||
source_ids = []
|
||
dimension = _artist_signature_dimension(None, phrase)
|
||
confidence = None
|
||
if phrase:
|
||
candidates.append({
|
||
"phrase": phrase,
|
||
"support_ratio": support,
|
||
"source_ids": source_ids,
|
||
"dimension": dimension,
|
||
"confidence": confidence,
|
||
})
|
||
for source in sources:
|
||
analysis = source.recognition_result if isinstance(getattr(source, "recognition_result", None), dict) else {}
|
||
profile = analysis.get("normalized_asset_profile") if isinstance(analysis.get("normalized_asset_profile"), dict) else {}
|
||
capsules = analysis.get("generation_capsules") if isinstance(analysis.get("generation_capsules"), dict) else {}
|
||
detailed = capsules.get("detailed") if isinstance(capsules.get("detailed"), dict) else {}
|
||
source_phrases = [
|
||
*_prompt_parts(profile.get("must_preserve")),
|
||
*_prompt_parts(detailed.get("must_preserve")),
|
||
*_prompt_parts(detailed.get("prompt_parts")),
|
||
]
|
||
attributes = profile.get("attributes") if isinstance(profile.get("attributes"), dict) else {}
|
||
for raw in attributes.values():
|
||
if not isinstance(raw, dict) or raw.get("transfer_role") not in {"style_structure", "style_rendering"}:
|
||
continue
|
||
value_text = _optional_text(raw.get("value"))
|
||
if value_text:
|
||
source_phrases.append(value_text)
|
||
source_id = _optional_text(getattr(source, "source_id", None))
|
||
for phrase in source_phrases:
|
||
candidates.append({
|
||
"phrase": phrase,
|
||
"support_ratio": 1.0 / source_count,
|
||
"source_ids": [source_id] if source_id else [],
|
||
"dimension": _artist_signature_dimension(None, phrase),
|
||
"confidence": None,
|
||
})
|
||
clusters: list[dict[str, Any]] = []
|
||
for candidate in candidates:
|
||
phrase = " ".join(str(candidate.get("phrase") or "").split()).strip(" ,.;")
|
||
if len(phrase) < 18 or _artist_signature_is_generic(phrase):
|
||
continue
|
||
if len(phrase) > 220 or _artist_signature_has_multiple_dimensions(phrase):
|
||
continue
|
||
dimension = _artist_signature_dimension(candidate.get("dimension"), phrase)
|
||
source_ids = set(_string_list(candidate.get("source_ids")))
|
||
support = float(candidate.get("support_ratio") or 0.0)
|
||
confidence = float(candidate.get("confidence") or 0.0)
|
||
match = next(
|
||
(item for item in clusters if item["dimension"] == dimension and _prompt_traits_overlap(phrase, item["phrase"])),
|
||
None,
|
||
)
|
||
if match is None:
|
||
clusters.append({
|
||
"phrase": phrase,
|
||
"dimension": dimension,
|
||
"source_ids": source_ids,
|
||
"support_ratio": support,
|
||
"confidence": confidence,
|
||
"best_len": len(phrase),
|
||
})
|
||
else:
|
||
match["source_ids"].update(source_ids)
|
||
match["support_ratio"] = max(float(match["support_ratio"]), support)
|
||
match["confidence"] = max(float(match["confidence"]), confidence)
|
||
if len(phrase) > int(match["best_len"]):
|
||
match["phrase"] = phrase
|
||
match["best_len"] = len(phrase)
|
||
for item in clusters:
|
||
if item["source_ids"]:
|
||
item["support_ratio"] = min(1.0, len(item["source_ids"]) / source_count)
|
||
item["support_ratio"] = round(float(item.get("support_ratio") or 0.0), 3)
|
||
item["confidence"] = round(float(item.get("confidence") or 0.0), 3) or None
|
||
item["specificity_score"] = min(1.0, len(_prompt_tokens(item["phrase"])) / 12.0)
|
||
item["supporting_source_ids"] = sorted(item["source_ids"])
|
||
item["eligible_core"] = float(item.get("support_ratio") or 0.0) >= 0.50
|
||
item.pop("best_len", None)
|
||
item.pop("source_ids", None)
|
||
clusters.sort(
|
||
key=lambda item: (
|
||
float(item.get("support_ratio") or 0.0) * 0.55
|
||
+ float(item.get("confidence") or 0.0) * 0.25
|
||
+ float(item.get("specificity_score") or 0.0) * 0.20,
|
||
len(str(item.get("phrase") or "")),
|
||
),
|
||
reverse=True,
|
||
)
|
||
eligible = [item for item in clusters if item.get("eligible_core")]
|
||
selected: list[dict[str, Any]] = []
|
||
selected_dimensions: set[str] = set()
|
||
for dimension in _ARTIST_SIGNATURE_DIMENSION_ORDER:
|
||
candidate = next((item for item in eligible if item["dimension"] == dimension), None)
|
||
if candidate is not None:
|
||
selected.append(candidate)
|
||
selected_dimensions.add(dimension)
|
||
if len(selected) >= 8:
|
||
break
|
||
if len(selected) < 8:
|
||
selected.extend(item for item in eligible if item["dimension"] not in selected_dimensions and item not in selected)
|
||
for item in selected:
|
||
item.pop("eligible_core", None)
|
||
return selected[:8]
|
||
|
||
def _artist_signature_is_generic(value: str) -> bool:
|
||
text = value.casefold()
|
||
generic = (
|
||
"anime style", "generic anime", "digital illustration", "high quality", "best quality",
|
||
"beautiful character", "masterpiece", "vibrant", "detailed artwork", "soft high-key pastel style",
|
||
"smooth ultra-clean digital paint", "vector-like rendering", "style characterized by",
|
||
)
|
||
return any(token in text for token in generic)
|
||
|
||
|
||
def _artist_signature_has_multiple_dimensions(value: str) -> bool:
|
||
text = value.casefold()
|
||
matched = 0
|
||
for markers in _ARTIST_SIGNATURE_DIMENSION_MARKERS.values():
|
||
if any(re.search(r"\b" + re.escape(marker) + r"\b", text) for marker in markers):
|
||
matched += 1
|
||
return matched >= 3
|
||
|
||
|
||
def _artist_signature_dimension(value: Any, phrase: Any) -> str:
|
||
requested = _optional_text(value)
|
||
if requested:
|
||
normalized = requested.casefold().replace("-", "_").replace(" ", "_")
|
||
aliases = {
|
||
"face": "face_structure",
|
||
"facial_structure": "face_structure",
|
||
"eyes": "eye_design",
|
||
"eye": "eye_design",
|
||
"hair": "hair_rendering",
|
||
"line": "linework",
|
||
"lines": "linework",
|
||
"shadow": "shading",
|
||
"color": "palette",
|
||
"colour": "palette",
|
||
"highlight": "highlights",
|
||
}
|
||
normalized = aliases.get(normalized, normalized)
|
||
if normalized in _ARTIST_SIGNATURE_DIMENSION_ORDER:
|
||
return normalized
|
||
text = str(phrase or "").casefold()
|
||
for dimension in _ARTIST_SIGNATURE_DIMENSION_ORDER:
|
||
if any(re.search(r"\b" + re.escape(marker) + r"\b", text) for marker in _ARTIST_SIGNATURE_DIMENSION_MARKERS[dimension]):
|
||
return dimension
|
||
return "rendering"
|
||
|
||
|
||
def _artist_signature_prompt_parts(payload: dict[str, Any]) -> list[str]:
|
||
raw = payload.get("artist_signature") if isinstance(payload, dict) else None
|
||
parts: list[str] = []
|
||
grouped: dict[str, list[str]] = {}
|
||
if isinstance(raw, list):
|
||
for item in raw:
|
||
phrase = item.get("phrase") if isinstance(item, dict) else item
|
||
phrase = _optional_text(phrase)
|
||
support = float(item.get("support_ratio") or 0.0) if isinstance(item, dict) else 0.0
|
||
confidence = float(item.get("confidence") or 0.0) if isinstance(item, dict) else 0.0
|
||
if (
|
||
phrase
|
||
and not _artist_signature_is_generic(phrase)
|
||
and len(phrase) <= 220
|
||
and not _artist_signature_has_multiple_dimensions(phrase)
|
||
and support >= 0.50
|
||
):
|
||
dimension = _artist_signature_dimension(item.get("dimension") if isinstance(item, dict) else None, phrase)
|
||
grouped.setdefault(dimension, []).append(phrase)
|
||
for dimension in _ARTIST_SIGNATURE_DIMENSION_ORDER:
|
||
phrases = _unique_prompt_parts(grouped.get(dimension, []))
|
||
if phrases:
|
||
parts.append(f"artist-specific {dimension.replace('_', ' ')} signature: {_join_prompt(*phrases)}")
|
||
return _unique_prompt_parts(parts)
|
||
|
||
|
||
def _style_feature_corpus(payload: dict[str, Any]) -> str:
|
||
pieces: list[str] = []
|
||
for key in ("feature_evidence", "variable_style_features", "source_content_features"):
|
||
raw = payload.get(key) if isinstance(payload, dict) else None
|
||
if isinstance(raw, list):
|
||
pieces.extend(
|
||
_optional_text(item.get("trait") if isinstance(item, dict) else item) or ""
|
||
for item in raw
|
||
)
|
||
priorities = payload.get("prompt_priority_groups") if isinstance(payload, dict) else None
|
||
if isinstance(priorities, dict):
|
||
for raw in priorities.values():
|
||
pieces.extend(_prompt_parts(raw))
|
||
capsules = payload.get("generation_capsules") if isinstance(payload, dict) else None
|
||
if isinstance(capsules, dict):
|
||
for raw in capsules.values():
|
||
if isinstance(raw, dict):
|
||
pieces.extend(_prompt_parts(raw.get("prompt_parts")))
|
||
pieces.extend(_prompt_parts(raw.get("must_preserve")))
|
||
profile = payload.get("normalized_asset_profile") if isinstance(payload, dict) else None
|
||
if isinstance(profile, dict):
|
||
pieces.extend(_prompt_parts(profile.get("must_preserve")))
|
||
attributes = profile.get("attributes")
|
||
if isinstance(attributes, dict):
|
||
pieces.extend(
|
||
_optional_text(item.get("value") if isinstance(item, dict) else item) or ""
|
||
for item in attributes.values()
|
||
)
|
||
pieces.extend(_artist_signature_prompt_parts(payload))
|
||
return " ".join(piece for piece in pieces if piece).casefold()
|
||
|
||
|
||
def _style_kernel_parts(payload: dict[str, Any]) -> list[str]:
|
||
corpus = _style_feature_corpus(payload)
|
||
result: list[str] = []
|
||
for patterns, phrase in _STYLE_KERNEL_RULES:
|
||
if all(re.search(pattern, corpus, re.IGNORECASE) for pattern in patterns):
|
||
result.append(phrase)
|
||
return _unique_prompt_parts(result)
|
||
|
||
|
||
def _strip_style_background_effects(value: Any) -> str:
|
||
text = str(value or "").strip()
|
||
for pattern, replacement in _STYLE_BACKGROUND_EFFECT_PATTERNS:
|
||
text = re.sub(pattern, replacement, text, flags=re.IGNORECASE)
|
||
text = re.sub(r"\bfeaturing\s+and\b", "with", text, flags=re.IGNORECASE)
|
||
text = re.sub(r"\s+([,.;])", r"\1", text)
|
||
text = re.sub(r"\b(?:and|with)\s*(?:[,.;]|$)", "", text, flags=re.IGNORECASE)
|
||
return _clean_prompt_text(text)
|
||
|
||
def _detailed_style_prompt(result: Any, fallback: Any) -> str:
|
||
payload = result if isinstance(result, dict) else {}
|
||
capsules = payload.get("generation_capsules") if isinstance(payload.get("generation_capsules"), dict) else {}
|
||
detailed = capsules.get("detailed") if isinstance(capsules.get("detailed"), dict) else {}
|
||
must_preserve = _prompt_parts(detailed.get("must_preserve"))
|
||
profile = payload.get("normalized_asset_profile") if isinstance(payload.get("normalized_asset_profile"), dict) else {}
|
||
must_preserve = _unique_prompt_parts([*must_preserve, *_prompt_parts(profile.get("must_preserve"))])
|
||
priorities = payload.get("prompt_priority_groups") if isinstance(payload.get("prompt_priority_groups"), dict) else {}
|
||
priority_parts = [
|
||
*_prompt_parts(priorities.get("core")),
|
||
*_prompt_parts(priorities.get("structural")),
|
||
*_prompt_parts(priorities.get("rendering")),
|
||
]
|
||
evidence = _normalized_feature_evidence(payload.get("feature_evidence"))
|
||
strict_evidence = bool(evidence)
|
||
gated_must_preserve = [
|
||
item for item in must_preserve
|
||
if _style_trait_is_supported(item, evidence, required=strict_evidence)
|
||
]
|
||
# Preserve compact provider-derived style priorities, but still gate literal
|
||
# scene/optical effects that lack cross-image evidence.
|
||
gated_priorities = [
|
||
item for item in priority_parts
|
||
if _style_trait_is_supported(
|
||
item,
|
||
evidence,
|
||
required=bool(_VARIABLE_CONTENT_PATTERNS.search(item)),
|
||
)
|
||
]
|
||
capsule_parts = [
|
||
item for item in _prompt_parts(detailed.get("prompt_parts"))
|
||
if _style_trait_is_supported(
|
||
item,
|
||
evidence,
|
||
required=bool(_VARIABLE_CONTENT_PATTERNS.search(item)),
|
||
)
|
||
]
|
||
signature_parts = _artist_signature_prompt_parts(payload)
|
||
# Priority groups are the compact style kernel; signatures and capsules are supplementary evidence.
|
||
supplemental_parts = [*gated_must_preserve, *capsule_parts, *signature_parts]
|
||
# Structural age stylization / body-ratio cues are not optional traits; keep them even when
|
||
# feature_evidence only covers linework/eyes. Chronological age attributes stay excluded.
|
||
for key, raw in (profile.get("attributes") or {}).items() if isinstance(profile.get("attributes"), dict) else []:
|
||
if not isinstance(raw, dict) or raw.get("transfer_role") != "style_structure":
|
||
continue
|
||
value = _optional_text(raw.get("value"))
|
||
if not value:
|
||
continue
|
||
if key in {"facial_age_stylization"} or str(key).endswith("facial_age_stylization"):
|
||
supplemental_parts.append(f"facial age stylization: {value}")
|
||
elif "body_maturity" in str(key) or "head_to_body" in str(key) or key in {"anatomy_scale"}:
|
||
supplemental_parts.append(value)
|
||
risks = _normalize_semantic_risks(payload.get("semantic_risk_audit"))
|
||
priority_clauses = []
|
||
for item in gated_priorities:
|
||
priority_clauses.extend(_split_prompt_clauses(item))
|
||
kernel_parts = _style_kernel_parts(payload)
|
||
rewritten = []
|
||
for item in [*kernel_parts, *priority_clauses]:
|
||
safe, _ = _sanitize_minor_age_text(_apply_semantic_risk_rewrites(item, risks))
|
||
safe = _strip_style_background_effects(_normalize_style_prompt_conflicts(_strip_mature_body_locks(safe)))
|
||
if safe:
|
||
rewritten.append(safe)
|
||
for item in supplemental_parts:
|
||
safe, _ = _sanitize_minor_age_text(_apply_semantic_risk_rewrites(item, risks))
|
||
safe = _strip_style_background_effects(_normalize_style_prompt_conflicts(_strip_mature_body_locks(safe)))
|
||
if safe:
|
||
rewritten.append(safe)
|
||
if kernel_parts:
|
||
kernel_anchors = rewritten[:len(kernel_parts)]
|
||
filtered = list(kernel_anchors)
|
||
for item in rewritten[len(kernel_parts):]:
|
||
redundant = any(
|
||
_prompt_traits_overlap(item, anchor) and len(item) <= int(len(anchor) * 0.90)
|
||
for anchor in kernel_anchors
|
||
)
|
||
if not redundant:
|
||
filtered.append(item)
|
||
rewritten = filtered
|
||
# Do not inject mature-adult body locks after scrubbing chronological-minor wording.
|
||
compiled = _compact_style_prompt_parts(rewritten, limit=16)
|
||
if compiled:
|
||
return _join_prompt(
|
||
*compiled,
|
||
"follow the user's requested age, gender presentation, body maturity, build and proportions; do not apply a fixed childlike or adult template",
|
||
)
|
||
safe_fallback, _fallback_changed = _sanitize_minor_age_text(str(fallback or "").strip())
|
||
return _strip_mature_body_locks(safe_fallback)
|
||
|
||
|
||
def _detailed_style_negative_prompt(result: Any, fallback: Any) -> str:
|
||
payload = result if isinstance(result, dict) else {}
|
||
capsules = payload.get("generation_capsules") if isinstance(payload.get("generation_capsules"), dict) else {}
|
||
detailed = capsules.get("detailed") if isinstance(capsules.get("detailed"), dict) else {}
|
||
parts = _prompt_parts(detailed.get("negative_parts"))
|
||
parts.extend(
|
||
item["negative_guard"]
|
||
for item in _normalize_semantic_risks(payload.get("semantic_risk_audit"))
|
||
if item.get("negative_guard")
|
||
)
|
||
source_parts = [
|
||
*_prompt_parts(detailed.get("must_preserve")),
|
||
*_prompt_parts(detailed.get("prompt_parts")),
|
||
*_prompt_parts(
|
||
(payload.get("normalized_asset_profile") or {}).get("must_preserve")
|
||
if isinstance(payload.get("normalized_asset_profile"), dict)
|
||
else None
|
||
),
|
||
]
|
||
risks = _normalize_semantic_risks(payload.get("semantic_risk_audit"))
|
||
if any(_semantic_phrase_is_object_risky(item) for item in source_parts) or any(
|
||
_semantic_phrase_is_object_risky(item.get("safe_rewrite")) for item in risks
|
||
):
|
||
parts.extend(_LITERAL_SHAPE_NEGATIVE_GUARDS)
|
||
normalized = [_normalize_style_prompt_conflicts(item) for item in parts if str(item or "").strip()]
|
||
compacted = _compact_style_prompt_parts(normalized, limit=18)
|
||
return _join_prompt(*compacted) if compacted else _normalize_style_prompt_conflicts(str(fallback or "").strip())
|
||
|
||
|
||
def _scale_specific_style_prompt(result: Any, case_key: str, common_prompt: Any = None) -> str:
|
||
payload = result if isinstance(result, dict) else {}
|
||
scales = payload.get("scale_capsules") if isinstance(payload.get("scale_capsules"), dict) else {}
|
||
raw = scales.get(case_key)
|
||
if isinstance(raw, dict):
|
||
parts = _prompt_parts(raw.get("prompt_parts") or raw.get("positive_additions"))
|
||
else:
|
||
parts = _prompt_parts(raw)
|
||
clauses = []
|
||
for part in parts:
|
||
clauses.extend(_split_prompt_clauses(part))
|
||
common_clauses = _split_prompt_clauses(common_prompt)
|
||
scale_only = [
|
||
item for item in clauses
|
||
if not any(_prompt_traits_overlap(item, common) for common in common_clauses)
|
||
]
|
||
sanitized = []
|
||
for item in scale_only:
|
||
safe, _ = _sanitize_minor_age_text(item)
|
||
safe = _strip_style_background_effects(_normalize_style_prompt_conflicts(safe))
|
||
if safe:
|
||
sanitized.append(safe)
|
||
scale_kernel = _STYLE_SCALE_KERNELS.get(case_key)
|
||
if scale_kernel:
|
||
sanitized.insert(0, scale_kernel)
|
||
return _join_prompt(*_compact_style_prompt_parts(sanitized, limit=6))
|
||
|
||
|
||
def _scale_specific_style_negative_prompt(result: Any, case_key: str) -> str:
|
||
payload = result if isinstance(result, dict) else {}
|
||
scales = payload.get("scale_capsules") if isinstance(payload.get("scale_capsules"), dict) else {}
|
||
raw = scales.get(case_key)
|
||
return _join_prompt(*_prompt_parts(raw.get("negative_parts") or raw.get("negative_additions"))) if isinstance(raw, dict) else ""
|
||
|
||
|
||
def _scale_transfer_instruction(case_key: str) -> str:
|
||
return {
|
||
"portrait": (
|
||
"at portrait scale preserve the source style's face contour and width-to-length ratio, cheek fullness, jawline and chin silhouette, "
|
||
"eye centerline and spacing, eye-to-face ratio, iris layer hierarchy, lash design, nose and mouth abstraction, "
|
||
"hair strand grouping and surface-bound highlight placement; do not redesign or elongate the facial contour"
|
||
),
|
||
"fullbody": (
|
||
"at full-body scale preserve silhouette language and stylized facial geometry without forcing facial neoteny or a childlike identity, while using a flexible stylized anatomy band of about "
|
||
"5-7 heads tall only when compatible with the requested subject; do not collapse into chibi/super-deformed "
|
||
"2-4 heads, and do not force a mature adult-woman body canon; preserve colored line hierarchy, clothing-fold shading regions "
|
||
),
|
||
"novel_scene": (
|
||
"in a wide scene keep the character linework, local palette relationships and shading grammar distinct from the "
|
||
"background while matching the source style's environment detail rhythm instead of becoming photorealistic"
|
||
),
|
||
}.get(case_key, "preserve the transferable style grammar at the requested camera scale")
|
||
|
||
|
||
def _apply_semantic_risk_rewrites(value: str, risks: list[dict[str, Any]]) -> str:
|
||
result = str(value or "").strip()
|
||
for item in risks:
|
||
original = _optional_text(item.get("original_phrase"))
|
||
rewrite = _optional_text(item.get("safe_rewrite"))
|
||
if original and rewrite:
|
||
result = re.sub(re.escape(original), rewrite, result, flags=re.IGNORECASE)
|
||
if _semantic_phrase_is_object_risky(result) and _HAIR_HIGHLIGHT_PATTERNS.search(result):
|
||
return _HAIR_HIGHLIGHT_SAFE_REWRITE
|
||
return result
|
||
|
||
|
||
def _compact_style_prompt_parts(values: list[str], *, limit: int) -> list[str]:
|
||
compacted: list[str] = []
|
||
signatures: list[set[str]] = []
|
||
for value in _unique_prompt_parts(values):
|
||
words = {
|
||
item
|
||
for item in re.findall(r"[a-z0-9]+", value.casefold())
|
||
if len(item) > 2 and item not in {"with", "from", "into", "style", "visual", "preserve"}
|
||
}
|
||
duplicate_index = None
|
||
for index, existing in enumerate(signatures):
|
||
if not words or not existing:
|
||
continue
|
||
overlap = len(words & existing) / max(1, min(len(words), len(existing)))
|
||
if overlap >= 0.82:
|
||
duplicate_index = index
|
||
break
|
||
if duplicate_index is None:
|
||
compacted.append(value)
|
||
signatures.append(words)
|
||
elif len(value) > len(compacted[duplicate_index]):
|
||
compacted[duplicate_index] = value
|
||
signatures[duplicate_index] = words
|
||
if len(compacted) >= limit:
|
||
break
|
||
return compacted
|
||
|
||
|
||
def _required_task_value(task: Any, key: str) -> str:
|
||
value = str(task.metadata.get(key) or "").strip()
|
||
if not value:
|
||
raise RuntimeError(f"advanced style task is missing {key}")
|
||
return value
|
||
|
||
|
||
def _join_prompt(*values: Any) -> str:
|
||
return ", ".join(dict.fromkeys(str(value).strip() for value in values if str(value or "").strip()))
|
||
|
||
|
||
def _now() -> str:
|
||
return datetime.now(timezone.utc).isoformat()
|
||
|
||
|
||
def _optional_text(value: Any) -> str | None:
|
||
text = str(value or "").strip()
|
||
return text or None
|
||
|
||
|
||
def _string_list(value: Any) -> list[str]:
|
||
if isinstance(value, str):
|
||
values = value.split(",")
|
||
elif isinstance(value, (list, tuple, set)):
|
||
values = value
|
||
else:
|
||
values = []
|
||
return list(dict.fromkeys(str(item).strip() for item in values if str(item).strip()))
|
||
|
||
|
||
def _list(value: Any) -> list[Any]:
|
||
return list(value) if isinstance(value, list) else []
|
||
|
||
|
||
def _object(value: Any) -> dict[str, Any]:
|
||
return dict(value) if isinstance(value, dict) else {}
|
||
|
||
|
||
def _score(value: Any, fallback: Any = None) -> float | None:
|
||
try:
|
||
number = float(value if value is not None else fallback)
|
||
except (TypeError, ValueError):
|
||
return None
|
||
return min(max(number, 0.0), 1.0)
|
||
|
||
|
||
def _calibrated_style_confidence(
|
||
provider_result: dict[str, Any],
|
||
fallback: Any,
|
||
structures: dict[str, Any],
|
||
) -> float | None:
|
||
base = _score(provider_result.get("consensus_confidence"), fallback)
|
||
if base is None:
|
||
return None
|
||
evidence_scores: list[float] = []
|
||
for item in _list(provider_result.get("feature_evidence")):
|
||
if not isinstance(item, dict):
|
||
continue
|
||
confidence = _score(item.get("confidence"))
|
||
support = _score(item.get("support_ratio"))
|
||
if confidence is not None and support is not None:
|
||
evidence_scores.append(confidence * support)
|
||
if evidence_scores:
|
||
base = min(base, sum(evidence_scores) / len(evidence_scores) + 0.1)
|
||
profile = structures.get("normalized_asset_profile") if isinstance(structures.get("normalized_asset_profile"), dict) else {}
|
||
attributes = profile.get("attributes") if isinstance(profile.get("attributes"), dict) else {}
|
||
structural_groups = (
|
||
("face", "facial", "chin"),
|
||
("eye", "iris", "lash", "eyelid"),
|
||
("nose", "mouth", "lip"),
|
||
("hair", "strand"),
|
||
("anatomy", "body_ratio", "head_to_body", "hand"),
|
||
("line", "outline", "linework"),
|
||
("shadow", "shading"),
|
||
("palette", "color", "saturation", "contrast"),
|
||
)
|
||
keys = [str(key).casefold() for key in attributes]
|
||
covered = sum(1 for aliases in structural_groups if any(any(alias in key for alias in aliases) for key in keys))
|
||
coverage_cap = 0.55 + 0.45 * (covered / len(structural_groups))
|
||
return min(base, coverage_cap)
|
||
|
||
|
||
def _optional_score(value: Any) -> float | None:
|
||
return _score(value)
|
||
|
||
|
||
def _safe_failure(exc: Exception) -> dict[str, Any]:
|
||
if isinstance(exc, ImageProviderError):
|
||
return provider_failure_attribution(exc)
|
||
text = " ".join(str(exc or type(exc).__name__).split())
|
||
return {"error_type": type(exc).__name__, "message": text[:500] or type(exc).__name__}
|
||
|
||
|
||
def _missing_queue_task_failure(task_id: Any, stage: str) -> dict[str, Any]:
|
||
return {
|
||
"error_type": "queue_task_missing",
|
||
"message": "关联的队列记录已被删除;可重新入队、排除样本或删除整个导入。",
|
||
"stage": stage,
|
||
"task_id": str(task_id or "").strip() or None,
|
||
"retryable": True,
|
||
}
|
||
|
||
|
||
def _validation_reference_metadata_from_payload(payload: dict[str, Any] | None) -> dict[str, Any]:
|
||
data = payload if isinstance(payload, dict) else {}
|
||
meta: dict[str, Any] = {}
|
||
if "validation_reference_enabled" in data:
|
||
meta["validation_reference_enabled"] = bool(data.get("validation_reference_enabled"))
|
||
source_id = _optional_text(data.get("validation_reference_source_id") or data.get("reference_source_id"))
|
||
if source_id:
|
||
meta["validation_reference_source_id"] = source_id
|
||
if "validation_reference_enabled" not in meta:
|
||
meta["validation_reference_enabled"] = True
|
||
if data.get("validation_reference_enabled") is False or data.get("disable_validation_reference") is True:
|
||
meta["validation_reference_enabled"] = False
|
||
meta.pop("validation_reference_source_id", None)
|
||
return meta
|
||
|
||
|
||
def _resolve_validation_reference_plan(
|
||
*,
|
||
sources: list[Any],
|
||
consensus_result: dict[str, Any],
|
||
consensus_metadata: dict[str, Any],
|
||
) -> dict[str, Any]:
|
||
source_by_id = {str(item.source_id): item for item in sources}
|
||
enabled_raw = consensus_metadata.get("validation_reference_enabled")
|
||
if enabled_raw is False:
|
||
return {
|
||
"enabled": False,
|
||
"source_id": None,
|
||
"source_uri": None,
|
||
"mode": "disabled",
|
||
}
|
||
requested_id = _optional_text(consensus_metadata.get("validation_reference_source_id"))
|
||
representative_ids = [
|
||
item for item in _string_list(consensus_result.get("representative_source_ids")) if item in source_by_id
|
||
]
|
||
primary = next((item for item in sources if bool(getattr(item, "is_primary", False))), None)
|
||
default_id = (
|
||
requested_id
|
||
if requested_id in source_by_id
|
||
else (representative_ids[0] if representative_ids else None)
|
||
or (str(primary.source_id) if primary is not None else None)
|
||
or (str(sources[0].source_id) if sources else None)
|
||
)
|
||
# Reference images are opt-in; pure text is the safe/default compatibility path.
|
||
enabled = False if enabled_raw is None else bool(enabled_raw)
|
||
if not enabled or not default_id or default_id not in source_by_id:
|
||
return {
|
||
"enabled": False,
|
||
"source_id": None,
|
||
"source_uri": None,
|
||
"mode": "disabled" if enabled_raw is False else "unavailable",
|
||
}
|
||
source = source_by_id[default_id]
|
||
uri = _optional_text(getattr(source, "source_image_uri", None))
|
||
if not uri:
|
||
return {
|
||
"enabled": False,
|
||
"source_id": default_id,
|
||
"source_uri": None,
|
||
"mode": "unavailable",
|
||
}
|
||
mode = "user_selected" if requested_id and requested_id == default_id else "representative_default"
|
||
return {
|
||
"enabled": True,
|
||
"source_id": default_id,
|
||
"source_uri": uri,
|
||
"mode": mode,
|
||
}
|
||
|
||
|
||
def _validation_reference_runtime_metadata(case_metadata: dict[str, Any] | None) -> dict[str, Any]:
|
||
meta = case_metadata if isinstance(case_metadata, dict) else {}
|
||
enabled = bool(meta.get("validation_reference_enabled"))
|
||
source_id = _optional_text(meta.get("validation_reference_source_id"))
|
||
source_uri = _optional_text(meta.get("validation_reference_source_uri"))
|
||
mode = _optional_text(meta.get("validation_reference_mode")) or ("enabled" if enabled else "disabled")
|
||
return {
|
||
"enabled": enabled and bool(source_uri),
|
||
"source_id": source_id,
|
||
"source_uri": source_uri,
|
||
"mode": mode,
|
||
"reference_image_applied": bool(meta.get("reference_image_applied")),
|
||
"reference_image_error": meta.get("reference_image_error"),
|
||
"reference_image_fallback": meta.get("reference_image_fallback"),
|
||
}
|
||
|
||
|
||
def _is_reference_image_unsupported_error(exc: Exception) -> bool:
|
||
text = " ".join(str(exc or "").split()).casefold()
|
||
markers = (
|
||
"input_images",
|
||
"input image",
|
||
"reference image",
|
||
"image field",
|
||
"does not support image",
|
||
"unsupported image",
|
||
"invalid image",
|
||
"unknown parameter",
|
||
"unexpected keyword",
|
||
"not support",
|
||
"not supported",
|
||
"image edit",
|
||
"multipart",
|
||
)
|
||
if any(marker in text for marker in markers):
|
||
return True
|
||
details = getattr(exc, "details", None)
|
||
if isinstance(details, dict):
|
||
detail_text = " ".join(str(item) for item in details.values()).casefold()
|
||
if any(marker in detail_text for marker in markers):
|
||
return True
|
||
error_type = str(getattr(exc, "error_type", "") or "").casefold()
|
||
if error_type in {"configuration_error", "invalid_request", "bad_request"}:
|
||
return True
|
||
# A number of OpenAI-compatible image gateways hide the actual validation
|
||
# message and return only a generic HTTP error when their text-generation
|
||
# endpoint receives an image field. Retry without the optional reference
|
||
# for client-side request errors, but never hide rate limits or server
|
||
# failures behind this fallback.
|
||
return error_type == "http_error" and getattr(exc, "status_code", None) in {400, 404, 415, 422, 500, 502, 503, 504}
|
||
|