Kezovic commited on
Commit
24c0210
·
verified ·
1 Parent(s): 5bcda87

Simplified prompts

Browse files
Files changed (1) hide show
  1. app.py +40 -32
app.py CHANGED
@@ -47,23 +47,19 @@ format_map = {
47
  "Ballad": "The poem MUST tell a narrative story using four-line stanzas (quatrains) and a simple rhyme scheme like A-B-C-B. It should be 4-6 stanzas long."
48
  }
49
 
50
- # --- Prompt Construction Helper Function (Robust, Delimited Prompt) ---
51
  def build_poem_prompt(voice_instruction, constraint_instruction, format_type, topic):
52
- """Constructs a robust, delimited prompt for initial poem generation."""
53
 
54
- system_context = (
55
- f"SYSTEM: You are a highly disciplined creative writer. You ABSOLUTELY MUST follow all constraints "
56
- f"on line count, structure, and persona. Your entire response MUST contain ONLY the poem and NOTHING else. "
57
- f"Do NOT include any titles, headers, or extra text.\n"
58
- f"PERSONA: {voice_instruction}\n"
59
- f"FORMAT RULES: {constraint_instruction}\n"
 
60
  )
61
-
62
- user_task = (
63
- f"USER: Write a {format_type} about the topic: '{topic}'. The poem MUST strictly adhere to all FORMAT RULES.\n"
64
- )
65
-
66
- return f"### Instruction:\n{system_context}\n{user_task}\n\n"
67
 
68
  # --- LLM Functions ---
69
  def generate_poem(format_type, persona, topic, progress=gr.Progress()):
@@ -86,41 +82,53 @@ def generate_poem(format_type, persona, topic, progress=gr.Progress()):
86
  prompt=full_poem_prompt,
87
  max_tokens=MAX_NEW_TOKENS,
88
  temperature=TEMPERATURE,
89
- stop=["### Instruction:", "USER:", "SYSTEM:", "### Human:"],
 
90
  echo=False
91
  )
 
92
  poem_text = output['choices'][0]['text'].strip()
 
 
93
 
94
- # --- 2. POET SUGGESTION (ANALYSIS) ---
95
  progress(0.5, desc="Analyzing style for literary match...")
96
 
97
- # Secondary prompt for the analysis task - HARDENED CONSTRAINT ADDED HERE
98
  poet_prompt = (
99
- f"### Instruction:\n"
100
- f"SYSTEM: You are a literary analyst specializing in poetry. Your task is to find the single most appropriate historical poet "
101
- f"whose style and themes match the provided poem. The poet MUST be a REAL, historical figure. "
102
- f"Do NOT suggest any fictional characters, places, or entities mentioned in the poem. "
103
- f"Output ONLY the POET'S NAME, followed by a COLON, and then a brief, single-sentence justification. Begin immediately with the poet's name. \n"
104
- f"Do NOT output any other text or headers. You MUST adhere to the requested persona's style.\n"
105
- f"PERSONA: {selected_voice}\n"
106
- f"USER: Analyze the poem below and suggest the best matching poet:\n\n"
107
  f"POEM:\n{poem_text}\n\n"
108
- f"BEGIN ANALYSIS HERE: " # Added a strong generation marker to prevent echoing the input poem
109
  )
110
 
111
  output_poet = llm(
112
  prompt=poet_prompt,
113
- max_tokens=150, # Keep this response short
114
  temperature=0.4, # Lower temperature for analysis/retrieval
115
- # Added "POEM:" and "BEGIN ANALYSIS HERE:" to the stop list for safety
116
- stop=["### Instruction:", "USER:", "SYSTEM:", "### Human:", "\n\n", "POEM:", "BEGIN ANALYSIS HERE:"],
117
  echo=False
118
  )
119
 
120
- # Remove the generation marker from the start of the output if it was successful
121
  poet_suggestion = output_poet['choices'][0]['text'].strip()
122
- if poet_suggestion.startswith("BEGIN ANALYSIS HERE:"):
123
- poet_suggestion = poet_suggestion[len("BEGIN ANALYSIS HERE:"):].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
 
126
  progress(0.9, desc="Poem and analysis complete!")
 
47
  "Ballad": "The poem MUST tell a narrative story using four-line stanzas (quatrains) and a simple rhyme scheme like A-B-C-B. It should be 4-6 stanzas long."
48
  }
49
 
50
+ # --- Prompt Construction Helper Function (Simplified) ---
51
  def build_poem_prompt(voice_instruction, constraint_instruction, format_type, topic):
52
+ """Constructs a simple, direct prompt for initial poem generation."""
53
 
54
+ # Combined, prioritized instruction block for maximum clarity
55
+ full_instruction = (
56
+ f"You are a highly disciplined creative writer. {voice_instruction} "
57
+ f"You must write a {format_type} about '{topic}'.\n"
58
+ f"STRICT FORMAT RULE: {constraint_instruction}\n"
59
+ f"Your output must contain ONLY the poem, nothing else. Do NOT include a title.\n\n"
60
+ f"BEGIN POEM: " # Simple, clear marker for generation start
61
  )
62
+ return full_instruction
 
 
 
 
 
63
 
64
  # --- LLM Functions ---
65
  def generate_poem(format_type, persona, topic, progress=gr.Progress()):
 
82
  prompt=full_poem_prompt,
83
  max_tokens=MAX_NEW_TOKENS,
84
  temperature=TEMPERATURE,
85
+ # Simplified stop list for the generation call
86
+ stop=["STRICT FORMAT RULE:", "BEGIN POEM:"],
87
  echo=False
88
  )
89
+ # Remove potential preamble text markers from output
90
  poem_text = output['choices'][0]['text'].strip()
91
+ if poem_text.startswith("BEGIN POEM:"):
92
+ poem_text = poem_text[len("BEGIN POEM:"):].strip()
93
 
94
+ # --- 2. POET SUGGESTION (ANALYSIS - Simplified Prompt) ---
95
  progress(0.5, desc="Analyzing style for literary match...")
96
 
97
+ # Secondary prompt for the analysis task - Simplified Question
98
  poet_prompt = (
99
+ f"You are a literary analyst. Analyze the poem below. Your analysis should reflect the persona of {selected_voice}. \n"
100
+ f"Identify the single most appropriate REAL, historical poet whose style and themes match the poem. Do NOT suggest fictional characters.\n"
101
+ f"Format your answer as: Poet Name: [Name] - Justification: [Reason].\n\n"
 
 
 
 
 
102
  f"POEM:\n{poem_text}\n\n"
103
+ f"ANALYSIS: " # Simple marker for generation start
104
  )
105
 
106
  output_poet = llm(
107
  prompt=poet_prompt,
108
+ max_tokens=80, # Keep this response short
109
  temperature=0.4, # Lower temperature for analysis/retrieval
110
+ # Simple stop list focused on stopping after the answer
111
+ stop=["POEM:", "ANALYSIS:", "\n\n", "\n"],
112
  echo=False
113
  )
114
 
115
+ # Clean up the output using simple string manipulation
116
  poet_suggestion = output_poet['choices'][0]['text'].strip()
117
+
118
+ # 1. Find and remove the "ANALYSIS:" marker if it appeared in the output
119
+ if poet_suggestion.startswith("ANALYSIS:"):
120
+ poet_suggestion = poet_suggestion[len("ANALYSIS:"):].strip()
121
+
122
+ # 2. Re-format the output cleanly (removing the template names)
123
+ if "Poet Name:" in poet_suggestion:
124
+ poet_suggestion = poet_suggestion.replace("Poet Name:", "").replace(" - Justification:", ":").strip()
125
+ # Handle the case where the LLM just gives the answer directly
126
+ elif ":" in poet_suggestion:
127
+ # Keep the output as is, assuming it's in the Poet: Justification format
128
+ pass
129
+ else:
130
+ # Simple fallback for totally non-compliant output
131
+ poet_suggestion = f"Analysis failed: {poet_suggestion[:50]}..."
132
 
133
 
134
  progress(0.9, desc="Poem and analysis complete!")