Files

138 lines
5.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
def refine_prompt():
return "Rewrite the previous prompt by improving it based on the feedback. Keep structure, subject, and layout unless explicitly contradicted.\nDo NOT change the core subject or scene type from the previous prompt unless explicitly required.\n"
def build_prompt_v1(
previous_prompt: str,
system_prompt: str,
rating_context: str,
text: str,
):
return (
f"Previous Prompt:\n{previous_prompt}\n\n"
f"Rating:\n{rating_context}\n\n---\n\n"
f"New Prompt:\n"
f"{system_prompt}\n\n"
f"User input:\n{text}"
).strip()
def _rewrite_prompt(previous_prompt: str) -> str:
return f"""
{refine_prompt()}
Task:
Rewrite the previous prompt to improve it using the feedback.
Rules:
- Keep the same core scene and subject.
- Only modify parts that are weak according to the feedback.
- Increase specificity, not creativity.
- Do NOT introduce a new scene.
""" if len(previous_prompt) > 0 else ""
def build_prompt(
previous_prompt: str,
system_prompt: str,
feedback: str,
text: str,
):
return f"""
{system_prompt}
{"Previous Prompt:" if len(previous_prompt) > 0 else ""}
{previous_prompt}
{_rewrite_prompt(previous_prompt)}
{"Feedback:" + feedback if len(previous_prompt) > 0 else ""}
User input:
{text}
""".strip()
def build_feedback(rating: dict, previous_prompt: str) -> str:
c = rating["criteria"]
notes = rating.get("notes", "")
feedback = []
def rule_block(name, score):
rules = []
# --- 1: Broken ---
if score == 1:
if name == "lighting":
rules.append(
"Lighting is incorrect or missing: explicitly define a single primary light source with exact position (e.g., upper-left), and describe shadow direction and length.")
elif name == "coherence":
rules.append(
"Scene is incoherent: enforce a physically consistent layout with no contradictions; ensure all objects align spatially in one environment.")
elif name == "detail_richness":
rules.append(
"Insufficient detail: add concrete, visible details to foreground and midground objects (textures, small elements, surface variation).")
elif name == "aesthetics":
rules.append(
"Composition is weak: rebalance subject placement and spatial hierarchy (clear foreground → midground → background separation).")
elif name == "technical_quality":
rules.append(
"Technical issues: remove ambiguity, avoid vague terms, and ensure all descriptions are concrete and directly placeable.")
elif name == "prompt_adherence":
rules.append("Does not follow structure: strictly enforce the 3-sentence format and required elements.")
# --- 2: Weak ---
elif score == 2:
if name == "lighting":
rules.append(
"Improve lighting clarity: define clearer light direction and more consistent shadow placement.")
elif name == "coherence":
rules.append(
"Improve spatial consistency: remove minor contradictions and clarify object relationships.")
elif name == "detail_richness":
rules.append("Increase detail density: add 1–2 more concrete elements per depth layer.")
elif name == "aesthetics":
rules.append("Improve composition balance: adjust subject dominance and spacing.")
elif name == "technical_quality":
rules.append("Reduce ambiguity: replace generic words with precise spatial descriptions.")
elif name == "prompt_adherence":
rules.append("Follow instructions more strictly: ensure all required attributes are present.")
# --- 3: Okay ---
elif score == 3:
if name == "lighting":
rules.append("Refine lighting: specify light intensity and sharper shadow definition.")
elif name == "coherence":
rules.append("Slightly refine spatial relationships for clearer depth separation.")
elif name == "detail_richness":
rules.append("Enhance detail slightly: add subtle texture or secondary elements.")
elif name == "aesthetics":
rules.append("Minor composition polish: improve visual flow between layers.")
elif name == "technical_quality":
rules.append("Tighten wording: remove redundancy and increase precision.")
elif name == "prompt_adherence":
rules.append("Ensure all structural constraints are fully respected.")
# --- 4: Good ---
elif score == 4:
rules.append(f"{name}: Maintain current quality, apply only minor precision improvements.")
# --- 5: Excellent ---
elif score == 5:
rules.append(f"{name}: Preserve exactly, do not modify unless required for consistency.")
return rules
# Build feedback per criterion
for key, value in c.items():
feedback.extend(rule_block(key, value))
# --- Notes handling (with sanity check) ---
feedback.append(f"User note: {notes} (must be applied precisely)")
# --- Priority ordering ---
feedback.append(
"Priority: Fix lowest scoring criteria first, then apply notes, then refine higher scoring areas."
)
return "\n".join(f"- {f}" for f in feedback)