Extract row normalization policy

This commit is contained in:
2026-06-27 01:15:24 +02:00
parent 2165e9fc16
commit b54b8b9421
6 changed files with 237 additions and 62 deletions
+23 -35
View File
@@ -3,9 +3,9 @@ from __future__ import annotations
from typing import Any, Callable
try:
from .prompt_hygiene import sanitize_caption_text, sanitize_negative_text, sanitize_prompt_text
from . import row_normalization as row_policy
except ImportError: # Allows local smoke tests with `python tools/prompt_smoke.py`.
from prompt_hygiene import sanitize_caption_text, sanitize_negative_text, sanitize_prompt_text
import row_normalization as row_policy
def _labeled_expression_sentence(label: str, expression: Any) -> str:
@@ -16,17 +16,11 @@ def _labeled_expression_sentence(label: str, expression: Any) -> str:
def _prepend_trigger(prompt: str, trigger: str, enabled: bool) -> str:
trigger = trigger.strip()
if not enabled or not trigger:
return prompt
if prompt.lower().startswith(trigger.lower()):
return prompt
return f"{trigger}, {prompt}"
return row_policy.prepend_trigger(prompt, trigger, enabled)
def _combined_negative(base: str, extra: str) -> str:
parts = [part.strip() for part in (base, extra) if part and part.strip()]
return ", ".join(parts)
return row_policy.combined_negative(base, extra)
def assemble_insta_pair_metadata(
@@ -109,17 +103,6 @@ def assemble_insta_pair_metadata(
f"{hard_camera_sentence}"
f"{hard_row['positive_suffix']}."
)
if extra_positive.strip():
soft_prompt = f"{soft_prompt.rstrip()} {extra_positive.strip()}"
hard_prompt = f"{hard_prompt.rstrip()} {extra_positive.strip()}"
soft_prompt = _prepend_trigger(soft_prompt, active_trigger, bool(prepend_trigger_to_prompt))
hard_prompt = _prepend_trigger(hard_prompt, active_trigger, bool(prepend_trigger_to_prompt))
soft_prompt = sanitize_prompt_text(soft_prompt, triggers=(active_trigger,))
hard_prompt = sanitize_prompt_text(hard_prompt, triggers=(active_trigger,))
soft_negative = sanitize_negative_text(_combined_negative(soft_negative_base, extra_negative))
hard_negative = sanitize_negative_text(_combined_negative(hard_negative_base, extra_negative))
soft_caption_parts = [
active_trigger,
"Insta/OF softcore mode",
@@ -134,10 +117,6 @@ def assemble_insta_pair_metadata(
soft_row["composition"],
camera_caption_text(soft_camera_config) if soft_camera_directive else "",
]
soft_caption = sanitize_caption_text(
", ".join(str(part).strip() for part in soft_caption_parts if str(part).strip()),
triggers=(active_trigger,),
)
hard_caption_parts = [
active_trigger,
"Insta/OF hardcore mode",
@@ -151,12 +130,20 @@ def assemble_insta_pair_metadata(
hard_composition,
camera_caption_text(hard_camera_config) if hard_camera_directive else "",
]
hard_caption = sanitize_caption_text(
", ".join(str(part).strip() for part in hard_caption_parts if str(part).strip()),
triggers=(active_trigger,),
normalized_text = row_policy.normalize_pair_text_outputs(
active_trigger=active_trigger,
prepend_trigger_to_prompt=bool(prepend_trigger_to_prompt),
extra_positive=extra_positive,
extra_negative=extra_negative,
soft_prompt=soft_prompt,
hard_prompt=hard_prompt,
soft_negative_base=soft_negative_base,
hard_negative_base=hard_negative_base,
soft_caption_parts=soft_caption_parts,
hard_caption_parts=hard_caption_parts,
)
return {
pair = {
"mode": "Insta/OF",
"options": options,
"shared_descriptor": descriptor,
@@ -169,12 +156,12 @@ def assemble_insta_pair_metadata(
"hardcore_clothing_state": hard_clothing_state,
"hardcore_detail_density": hard_detail_density,
"hardcore_position_config": hard_row.get("hardcore_position_config", {}),
"softcore_prompt": soft_prompt,
"hardcore_prompt": hard_prompt,
"softcore_negative_prompt": soft_negative,
"hardcore_negative_prompt": hard_negative,
"softcore_caption": soft_caption,
"hardcore_caption": hard_caption,
"softcore_prompt": normalized_text["soft_prompt"],
"hardcore_prompt": normalized_text["hard_prompt"],
"softcore_negative_prompt": normalized_text["soft_negative"],
"hardcore_negative_prompt": normalized_text["hard_negative"],
"softcore_caption": normalized_text["soft_caption"],
"hardcore_caption": normalized_text["hard_caption"],
"softcore_row": soft_row,
"hardcore_row": hard_row,
"hardcore_women_count": hard_women_count,
@@ -188,3 +175,4 @@ def assemble_insta_pair_metadata(
"softcore_camera_scene_directive": soft_camera_scene_directive,
"hardcore_camera_scene_directive": hard_camera_scene_directive,
}
return row_policy.normalize_pair_metadata(pair, active_trigger=active_trigger)