Files
ComfyUI-Ethanfel-Prompt-Bui…/krea_configured_cast_formatter.py
T

135 lines
5.0 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Callable
@dataclass(frozen=True)
class KreaConfiguredCastRequest:
row: dict[str, Any]
detail_level: str
style_mode: str
primary: str
item: str
scene: str
expression: str
composition: str
source_composition: str
camera: str
camera_scene: str
style: str
@dataclass(frozen=True)
class KreaConfiguredCastPrompt:
prompt: str
method: str = "metadata(configured_cast)"
def as_tuple(self) -> tuple[str, str]:
return self.prompt, self.method
@dataclass(frozen=True)
class KreaConfiguredCastDependencies:
clean: Callable[[Any], str]
prompt_field: Callable[[str, str], str]
sanitize_hardcore_environment_anchors: Callable[[Any], str]
sanitize_hardcore_axis_values: Callable[[Any], Any]
sanitize_scene_text_for_cast: Callable[[Any, list[str]], str]
normalize_hardcore_detail_density: Callable[[Any], str]
row_action_family: Callable[[Any], str]
hardcore_action_sentence: Callable[[str, str, str, Any, str, str], str]
pov_action_phrase: Callable[[str, list[str], str, str, str, Any, str], str]
pov_labels_from_value: Callable[[Any], list[str]]
merge_labels: Callable[..., list[str]]
cast_prose_omit: Callable[[str, list[str]], tuple[str, list[str]]]
filter_pov_labeled_clauses: Callable[[Any, list[str]], str]
natural_label_text: Callable[[Any, list[str]], str]
pov_composition_text: Callable[[Any, list[str]], str]
pov_camera_phrase: Callable[[list[str]], str]
expression_phrase: Callable[[Any], str]
composition_phrase: Callable[..., str]
paragraph: Callable[[list[str]], str]
def format_configured_cast_result(
request: KreaConfiguredCastRequest,
deps: KreaConfiguredCastDependencies,
) -> KreaConfiguredCastPrompt:
row = request.row
subject = deps.clean(row.get("subject_phrase") or request.primary or "adult sexual scene")
cast = deps.clean(row.get("cast_summary"))
try:
women_count = int(row.get("women_count") or 0)
men_count = int(row.get("men_count") or 0)
except (TypeError, ValueError):
women_count = men_count = 0
cast_descriptor_text = (
deps.clean(row.get("cast_descriptor_text"))
or deps.prompt_field(deps.clean(row.get("prompt")), "Characters")
or deps.prompt_field(deps.clean(row.get("prompt")), "Cast descriptors")
)
pov_labels = deps.pov_labels_from_value(row.get("pov_character_labels"))
camera = request.camera
if pov_labels:
camera = ""
cast_prose, cast_labels = deps.cast_prose_omit(cast_descriptor_text, pov_labels)
if not cast_labels and women_count == 1 and men_count == 1:
cast_labels = ["Woman A", "Man A"]
cast_labels = deps.merge_labels(cast_labels, pov_labels)
expression = deps.filter_pov_labeled_clauses(request.expression, pov_labels)
expression = deps.natural_label_text(expression, cast_labels)
composition = deps.sanitize_hardcore_environment_anchors(request.composition)
source_composition = deps.sanitize_hardcore_environment_anchors(request.source_composition)
role_graph = deps.sanitize_scene_text_for_cast(
deps.sanitize_hardcore_environment_anchors(row.get("source_role_graph") or row.get("role_graph")),
cast_labels,
)
item = deps.sanitize_scene_text_for_cast(
deps.sanitize_hardcore_environment_anchors(request.item),
cast_labels,
)
role_graph = deps.natural_label_text(role_graph, cast_labels)
item = deps.natural_label_text(item, cast_labels)
axis_values = deps.sanitize_hardcore_axis_values(row.get("item_axis_values"))
detail_density = deps.normalize_hardcore_detail_density(row.get("hardcore_detail_density"))
action = deps.hardcore_action_sentence(
role_graph,
item,
source_composition,
axis_values,
detail_density,
deps.row_action_family(row),
)
action = deps.pov_action_phrase(
action,
pov_labels,
role_graph,
item,
source_composition,
axis_values,
detail_density,
)
output_composition = deps.pov_composition_text(composition, pov_labels)
parts = [
action,
deps.pov_camera_phrase(pov_labels),
cast_prose,
f"A consensual explicit adult scene with {subject}" if not action else "",
f"The cast includes {cast}" if cast and not cast_prose and not (women_count == 1 and men_count == 1) else "",
f"The setting is {request.scene}" if request.scene else "",
request.camera_scene,
deps.expression_phrase(expression),
deps.composition_phrase(output_composition, action, "The image is framed as", detail_density),
camera,
request.style if request.detail_level != "concise" else "",
]
return KreaConfiguredCastPrompt(deps.paragraph(parts))
def format_configured_cast(
request: KreaConfiguredCastRequest,
deps: KreaConfiguredCastDependencies,
) -> tuple[str, str]:
return format_configured_cast_result(request, deps).as_tuple()