2753 行
137 KiB
Python
2753 行
137 KiB
Python
"""Style-only multi-image extraction and prompt-only 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.
|
|
|
|
No validation request carries a source image. The source set is used only by
|
|
the recognition and consensus stages.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
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 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
|
|
|
|
|
|
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)
|
|
|
|
# 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 softly lit neutral studio background, with a distinct identity and wardrobe"
|
|
),
|
|
"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 full body standing pose in a clean daylight studio, 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 a breezy rooftop garden at dawn, "
|
|
"wide environmental view with 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
|
|
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
|
|
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)
|
|
consensus = self.repository.create_style_consensus_run(
|
|
{
|
|
"consensus_id": f"advconsensus_{uuid4().hex}",
|
|
"import_id": import_id,
|
|
"revision": revision,
|
|
"status": "queued",
|
|
"model_route": record.recognition_model_route or self.owner._recognition_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,
|
|
"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],
|
|
"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
|
|
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})
|
|
return {"import": self.owner._require_import(import_id).to_dict(), "attempt": attempt.to_dict(), "case": case.to_dict(), "task": task.to_dict()}
|
|
|
|
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
|
|
]
|
|
return self.create_consensus(
|
|
record.import_id,
|
|
{
|
|
"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,
|
|
},
|
|
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,
|
|
"prompt": source.recognition_prompt,
|
|
"negative_prompt": source.recognition_negative_prompt,
|
|
"tags": source.recognition_tags,
|
|
"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 = 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": 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})
|
|
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,
|
|
},
|
|
)
|
|
try:
|
|
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")),
|
|
"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,
|
|
"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,
|
|
"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)
|
|
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",
|
|
)
|
|
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)
|
|
scale_style_negative = _scale_specific_style_negative_prompt(consensus.result, case_key)
|
|
case_negative = _join_prompt(negative, scale_style_negative)
|
|
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",
|
|
)
|
|
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": 2,
|
|
"content_leakage_exclusions": leakage,
|
|
"retry_count": 0,
|
|
"prompt_only": True,
|
|
},
|
|
}
|
|
)
|
|
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,
|
|
},
|
|
},
|
|
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"},
|
|
"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}迁移验证",
|
|
},
|
|
},
|
|
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": True},
|
|
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]),
|
|
"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 {}),
|
|
# Intentionally no image/input_images key. This validates style
|
|
# transfer from the consensus text into unrelated content.
|
|
"output_scope": {
|
|
"kind": "advanced_style_validation",
|
|
"import_id": import_id,
|
|
"attempt_id": attempt_id,
|
|
"case_id": case_id,
|
|
"case_key": case.case_key,
|
|
},
|
|
}
|
|
assets = []
|
|
try:
|
|
try:
|
|
assets = self.single_flow_engine.generate(node, task, request)
|
|
except ImageProviderError as exc:
|
|
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": True,
|
|
"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 = 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)
|
|
score, score_meta = _weighted_style_fidelity_score(case.case_key, dimension_scores, provider_score)
|
|
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,
|
|
)
|
|
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": _string_list(payload.get("mismatches")),
|
|
"style_deltas": _normalize_style_deltas(payload.get("style_deltas"), case_key=case.case_key),
|
|
"prompt_patch": prompt_patch,
|
|
"semantic_risks": _normalize_semantic_risks(payload.get("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, apparent-age stylization, 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 report. 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. 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); 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 and apparent-age range, "
|
|
"and 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 = dict(value)
|
|
profile = dict(result.get("normalized_asset_profile") or {})
|
|
profile.update({"category": "style", "role": "style", "body_slots": ["global_style"], "wear_layer": None, "stackable": False})
|
|
result["normalized_asset_profile"] = profile
|
|
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, apparent-age stylization, "
|
|
"head-to-body ratio, 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."
|
|
)
|
|
|
|
|
|
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)
|
|
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 and wardrobe "
|
|
"detail, adapting only the camera to a head-and-shoulders composition with a simple neutral background"
|
|
),
|
|
"fullbody": (
|
|
"For this controlled full-body comparison, preserve every requested identity, hairstyle, color and wardrobe "
|
|
"detail and show the complete character from head to toe in a simple readable environment"
|
|
),
|
|
"novel_scene": (
|
|
"For this cross-scene comparison, preserve every requested identity, hairstyle, color and wardrobe detail, "
|
|
"then place the same character in a clearly different wide environmental scene"
|
|
),
|
|
}.get(case_key, "Preserve every requested subject and wardrobe detail in a controlled comparison composition")
|
|
return _join_prompt(f"Mandatory human validation request: {instruction}", framing)
|
|
|
|
|
|
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,
|
|
) -> 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"
|
|
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],
|
|
},
|
|
)
|
|
|
|
|
|
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"
|
|
severity = _score(item.get("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 _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")),
|
|
]
|
|
parts = [*must_preserve, *priority_parts, *_prompt_parts(detailed.get("prompt_parts"))]
|
|
risks = _normalize_semantic_risks(payload.get("semantic_risk_audit"))
|
|
compiled = _compact_style_prompt_parts([_apply_semantic_risk_rewrites(item, risks) for item in parts], limit=14)
|
|
return _join_prompt(*compiled) if compiled else str(fallback or "").strip()
|
|
|
|
|
|
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")
|
|
)
|
|
return _join_prompt(*parts) if parts else str(fallback or "").strip()
|
|
|
|
|
|
def _scale_specific_style_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)
|
|
if isinstance(raw, dict):
|
|
parts = _prompt_parts(raw.get("prompt_parts") or raw.get("positive_additions"))
|
|
else:
|
|
parts = _prompt_parts(raw)
|
|
return _join_prompt(*_compact_style_prompt_parts(parts, limit=5))
|
|
|
|
|
|
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, eye-to-face ratio, iris layer hierarchy, lash design, "
|
|
"nose and mouth abstraction, hair strand grouping and surface-bound highlight placement"
|
|
),
|
|
"fullbody": (
|
|
"at full-body scale preserve silhouette language, head-to-body ratio, anatomy abstraction, colored line hierarchy, "
|
|
"clothing-fold shading bands and readable but non-exaggerated facial features"
|
|
),
|
|
"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)
|
|
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 _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]:
|
|
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,
|
|
}
|