Update app.py
Browse files
app.py
CHANGED
|
@@ -63,8 +63,7 @@ format_map = {
|
|
| 63 |
"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."
|
| 64 |
}
|
| 65 |
|
| 66 |
-
# --- Refinement Instruction Map
|
| 67 |
-
# Maps the button label to the strict instruction given to the model
|
| 68 |
REFINEMENT_INSTRUCTIONS = {
|
| 69 |
"Make it Shorter": "Rewrite the poem to be significantly shorter, reducing the line count by at least 3 lines, while keeping the core meaning.",
|
| 70 |
"Use Flowery Language": "Rewrite the poem using highly elaborate, decorative, and verbose language with many adjectives and metaphors.",
|
|
@@ -130,7 +129,7 @@ def analyze_poem(poem_text, progress=gr.Progress()):
|
|
| 130 |
output_poet = llm_analyst(
|
| 131 |
prompt=poet_prompt,
|
| 132 |
max_tokens=200,
|
| 133 |
-
temperature=0.4,
|
| 134 |
echo=False
|
| 135 |
)
|
| 136 |
|
|
@@ -138,7 +137,8 @@ def analyze_poem(poem_text, progress=gr.Progress()):
|
|
| 138 |
progress(1.0, desc="Analysis complete!")
|
| 139 |
return poet_suggestion
|
| 140 |
|
| 141 |
-
# --- LLM Function: Step 3 (Refine Poem -
|
|
|
|
| 142 |
def refine_poem(poem_text, refinement_instruction, progress=gr.Progress()):
|
| 143 |
if not llm_generator:
|
| 144 |
return "Error: Generator model not loaded."
|
|
@@ -233,15 +233,17 @@ with gr.Blocks(title="The Poetry Workshop", theme=gr.themes.Soft()) as demo:
|
|
| 233 |
|
| 234 |
gr.Markdown("---")
|
| 235 |
|
| 236 |
-
## ⚙️ Poem Refinement Section
|
| 237 |
with gr.Group():
|
| 238 |
gr.Markdown("### Refine Poem")
|
| 239 |
gr.Markdown("Click a button below to automatically revise the current poem in the **'Your Poem'** box.")
|
|
|
|
|
|
|
|
|
|
| 240 |
with gr.Row():
|
| 241 |
-
# Dynamically create buttons based on the map
|
| 242 |
-
buttons = []
|
| 243 |
for label in REFINEMENT_INSTRUCTIONS.keys():
|
| 244 |
-
|
|
|
|
| 245 |
|
| 246 |
# --- Event Handling ---
|
| 247 |
|
|
@@ -259,14 +261,44 @@ with gr.Blocks(title="The Poetry Workshop", theme=gr.themes.Soft()) as demo:
|
|
| 259 |
outputs=poet_suggestion_display
|
| 260 |
)
|
| 261 |
|
| 262 |
-
# 3. Refinement Events (
|
| 263 |
-
|
| 264 |
-
for
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
if __name__ == "__main__":
|
| 272 |
demo.launch()
|
|
|
|
| 63 |
"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."
|
| 64 |
}
|
| 65 |
|
| 66 |
+
# --- Refinement Instruction Map ---
|
|
|
|
| 67 |
REFINEMENT_INSTRUCTIONS = {
|
| 68 |
"Make it Shorter": "Rewrite the poem to be significantly shorter, reducing the line count by at least 3 lines, while keeping the core meaning.",
|
| 69 |
"Use Flowery Language": "Rewrite the poem using highly elaborate, decorative, and verbose language with many adjectives and metaphors.",
|
|
|
|
| 129 |
output_poet = llm_analyst(
|
| 130 |
prompt=poet_prompt,
|
| 131 |
max_tokens=200,
|
| 132 |
+
temperature=0.4,
|
| 133 |
echo=False
|
| 134 |
)
|
| 135 |
|
|
|
|
| 137 |
progress(1.0, desc="Analysis complete!")
|
| 138 |
return poet_suggestion
|
| 139 |
|
| 140 |
+
# --- LLM Function: Step 3 (Refine Poem - MODIFIED SIGNATURE FOR GRADIO) ---
|
| 141 |
+
# NOTE: This function now accepts an *explicit* instruction string, which Gradio will bind.
|
| 142 |
def refine_poem(poem_text, refinement_instruction, progress=gr.Progress()):
|
| 143 |
if not llm_generator:
|
| 144 |
return "Error: Generator model not loaded."
|
|
|
|
| 233 |
|
| 234 |
gr.Markdown("---")
|
| 235 |
|
| 236 |
+
## ⚙️ Poem Refinement Section
|
| 237 |
with gr.Group():
|
| 238 |
gr.Markdown("### Refine Poem")
|
| 239 |
gr.Markdown("Click a button below to automatically revise the current poem in the **'Your Poem'** box.")
|
| 240 |
+
|
| 241 |
+
# NOTE: Buttons are created but we will reference them explicitly below
|
| 242 |
+
buttons_refinement = {}
|
| 243 |
with gr.Row():
|
|
|
|
|
|
|
| 244 |
for label in REFINEMENT_INSTRUCTIONS.keys():
|
| 245 |
+
# Store the button object in the dictionary for later reference
|
| 246 |
+
buttons_refinement[label] = gr.Button(label, scale=1, variant="light")
|
| 247 |
|
| 248 |
# --- Event Handling ---
|
| 249 |
|
|
|
|
| 261 |
outputs=poet_suggestion_display
|
| 262 |
)
|
| 263 |
|
| 264 |
+
# 3. Refinement Events (Rewritten to remove lambda)
|
| 265 |
+
|
| 266 |
+
# Create explicit functions for each button click, passing the desired instruction
|
| 267 |
+
|
| 268 |
+
# Make it Shorter
|
| 269 |
+
buttons_refinement["Make it Shorter"].click(
|
| 270 |
+
fn=refine_poem,
|
| 271 |
+
inputs=[output_display, gr.State(REFINEMENT_INSTRUCTIONS["Make it Shorter"])],
|
| 272 |
+
outputs=[output_display]
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
# Use Flowery Language
|
| 276 |
+
buttons_refinement["Use Flowery Language"].click(
|
| 277 |
+
fn=refine_poem,
|
| 278 |
+
inputs=[output_display, gr.State(REFINEMENT_INSTRUCTIONS["Use Flowery Language"])],
|
| 279 |
+
outputs=[output_display]
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
+
# Make it More Intense
|
| 283 |
+
buttons_refinement["Make it More Intense"].click(
|
| 284 |
+
fn=refine_poem,
|
| 285 |
+
inputs=[output_display, gr.State(REFINEMENT_INSTRUCTIONS["Make it More Intense"])],
|
| 286 |
+
outputs=[output_display]
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
# Add a Title
|
| 290 |
+
buttons_refinement["Add a Title"].click(
|
| 291 |
+
fn=refine_poem,
|
| 292 |
+
inputs=[output_display, gr.State(REFINEMENT_INSTRUCTIONS["Add a Title"])],
|
| 293 |
+
outputs=[output_display]
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
# Be More Humorous
|
| 297 |
+
buttons_refinement["Be More Humorous"].click(
|
| 298 |
+
fn=refine_poem,
|
| 299 |
+
inputs=[output_display, gr.State(REFINEMENT_INSTRUCTIONS["Be More Humorous"])],
|
| 300 |
+
outputs=[output_display]
|
| 301 |
+
)
|
| 302 |
|
| 303 |
if __name__ == "__main__":
|
| 304 |
demo.launch()
|