Extract caption format dispatch route
This commit is contained in:
+31
-15
@@ -3,12 +3,14 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
try:
|
||||
from . import caption_format_route
|
||||
from . import caption_metadata_routes
|
||||
from . import caption_policy
|
||||
from . import caption_text_policy
|
||||
from . import formatter_input as input_policy
|
||||
from .prompt_hygiene import sanitize_prose_text
|
||||
except ImportError: # Allows local smoke tests with `python -c`.
|
||||
import caption_format_route
|
||||
import caption_metadata_routes
|
||||
import caption_policy
|
||||
import caption_text_policy
|
||||
@@ -318,6 +320,23 @@ def _text_to_prose(text: str, detail_level: str, keep_style: bool) -> tuple[str,
|
||||
return prose or _sentence(text), "text(fallback)"
|
||||
|
||||
|
||||
def _caption_format_dependencies() -> caption_format_route.CaptionFormatDependencies:
|
||||
return caption_format_route.CaptionFormatDependencies(
|
||||
apply_caption_profile=lambda profile, detail, style, include: caption_policy.apply_caption_profile(
|
||||
profile,
|
||||
detail_level=detail,
|
||||
style_policy=style,
|
||||
include_trigger=include,
|
||||
),
|
||||
keep_style_terms=caption_policy.keep_style_terms,
|
||||
row_from_inputs=_row_from_inputs,
|
||||
metadata_to_prose=_metadata_to_prose,
|
||||
text_to_prose=_text_to_prose,
|
||||
with_trigger=_with_trigger,
|
||||
sanitize_prose_text=sanitize_prose_text,
|
||||
)
|
||||
|
||||
|
||||
def naturalize_caption(
|
||||
source_text: str,
|
||||
metadata_json: str = "",
|
||||
@@ -329,19 +348,16 @@ def naturalize_caption(
|
||||
caption_profile: str = caption_policy.CAPTION_PROFILE_DEFAULT,
|
||||
) -> tuple[str, str]:
|
||||
"""Rewrite tag-style prompt/caption text into compact natural language."""
|
||||
input_hint = input_hint if input_hint in ("auto", "metadata_json", "caption_or_prompt") else "auto"
|
||||
detail_level, style_policy, include_trigger = caption_policy.apply_caption_profile(
|
||||
caption_profile,
|
||||
detail_level=detail_level,
|
||||
style_policy=style_policy,
|
||||
include_trigger=include_trigger,
|
||||
return caption_format_route.naturalize_caption(
|
||||
caption_format_route.CaptionFormatRequest(
|
||||
source_text=source_text,
|
||||
metadata_json=metadata_json,
|
||||
input_hint=input_hint,
|
||||
trigger=trigger,
|
||||
include_trigger=include_trigger,
|
||||
detail_level=detail_level,
|
||||
style_policy=style_policy,
|
||||
caption_profile=caption_profile,
|
||||
),
|
||||
_caption_format_dependencies(),
|
||||
)
|
||||
keep_style = caption_policy.keep_style_terms(style_policy)
|
||||
row, row_method = _row_from_inputs(source_text, metadata_json, input_hint)
|
||||
if row is not None:
|
||||
prose, method = _metadata_to_prose(row, detail_level, keep_style)
|
||||
caption = sanitize_prose_text(_with_trigger(prose, trigger, include_trigger), triggers=(trigger,))
|
||||
return caption, f"{row_method}:{method}"
|
||||
prose, method = _text_to_prose(source_text, detail_level, keep_style)
|
||||
caption = sanitize_prose_text(_with_trigger(prose, trigger, include_trigger), triggers=(trigger,))
|
||||
return caption, method
|
||||
|
||||
Reference in New Issue
Block a user