Update app.py
Browse files
app.py
CHANGED
|
@@ -29,27 +29,23 @@ def load_llm():
|
|
| 29 |
|
| 30 |
load_llm()
|
| 31 |
|
| 32 |
-
# ---
|
| 33 |
-
# UPDATE: Added 'progress=gr.Progress()' here to enable the loading bar
|
| 34 |
def generate_poem(format_type, persona, topic, progress=gr.Progress()):
|
| 35 |
-
"""
|
| 36 |
-
Takes user variables and wraps them in a sophisticated prompt
|
| 37 |
-
that the user never sees.
|
| 38 |
-
"""
|
| 39 |
# 1. VISUAL FEEDBACK: Immediately show the bar
|
|
|
|
| 40 |
progress(0, desc="Consulting the Muse...")
|
| 41 |
-
|
|
|
|
| 42 |
if not llm:
|
| 43 |
return "Error: Model not loaded."
|
| 44 |
|
| 45 |
if not topic:
|
| 46 |
return "Please enter a topic!"
|
| 47 |
|
| 48 |
-
# 2.
|
| 49 |
progress(0.2, desc=f"Summoning {persona}...")
|
| 50 |
-
time.sleep(0.3)
|
| 51 |
|
| 52 |
-
# 1. Define Persona Instructions (The "Voice")
|
| 53 |
persona_map = {
|
| 54 |
"A Grumpy Pirate": "You are a salty, grumpy pirate captain. Use nautical slang, complain about the sea, and say 'Arrr'.",
|
| 55 |
"A Melancholy Philosopher": "You are a deep, existential philosopher. Use complex vocabulary, metaphor, and a somber tone.",
|
|
@@ -57,7 +53,6 @@ def generate_poem(format_type, persona, topic, progress=gr.Progress()):
|
|
| 57 |
"Shakespearean Actor": "You are a dramatic Shakespearean actor. Use Early Modern English (thee, thou), dramatic flair, and iambic rhythm."
|
| 58 |
}
|
| 59 |
|
| 60 |
-
# 2. Define Format Instructions (The "Constraint")
|
| 61 |
format_map = {
|
| 62 |
"Limerick": "Write a Limerick. It must have exactly 5 lines. The rhyme scheme must be A-A-B-B-A. It should be humorous.",
|
| 63 |
"Sonnet": "Write a Sonnet. It must have exactly 14 lines and express a complete thought or feeling.",
|
|
@@ -77,10 +72,9 @@ def generate_poem(format_type, persona, topic, progress=gr.Progress()):
|
|
| 77 |
f"### Response:\n"
|
| 78 |
)
|
| 79 |
|
| 80 |
-
# 3.
|
| 81 |
-
progress(0.4, desc="
|
| 82 |
|
| 83 |
-
# 4. Generate
|
| 84 |
output = llm(
|
| 85 |
prompt=full_prompt,
|
| 86 |
max_tokens=MAX_NEW_TOKENS,
|
|
@@ -89,20 +83,18 @@ def generate_poem(format_type, persona, topic, progress=gr.Progress()):
|
|
| 89 |
echo=False
|
| 90 |
)
|
| 91 |
|
| 92 |
-
# 4.
|
| 93 |
-
progress(0.9, desc="
|
| 94 |
time.sleep(0.2)
|
| 95 |
|
| 96 |
return output['choices'][0]['text'].strip()
|
| 97 |
|
| 98 |
-
# ---
|
| 99 |
with gr.Blocks(title="The Poetry Workshop", theme=gr.themes.Soft()) as demo:
|
| 100 |
|
| 101 |
-
# Header
|
| 102 |
gr.Markdown("# 🖋️ The Poetry Workshop")
|
| 103 |
gr.Markdown("Your own personal muse to help you get started with poetry.")
|
| 104 |
|
| 105 |
-
# The "Sentence" Layout (Mad Libs Style)
|
| 106 |
with gr.Group():
|
| 107 |
with gr.Row(equal_height=True):
|
| 108 |
gr.Markdown("### I want to write a ")
|
|
@@ -124,7 +116,6 @@ with gr.Blocks(title="The Poetry Workshop", theme=gr.themes.Soft()) as demo:
|
|
| 124 |
scale=3
|
| 125 |
)
|
| 126 |
|
| 127 |
-
# Topic Input
|
| 128 |
with gr.Row(equal_height=True):
|
| 129 |
gr.Markdown("### about this topic: ")
|
| 130 |
topic_input = gr.Textbox(
|
|
@@ -135,11 +126,18 @@ with gr.Blocks(title="The Poetry Workshop", theme=gr.themes.Soft()) as demo:
|
|
| 135 |
)
|
| 136 |
generate_btn = gr.Button("✨ Create Masterpiece", variant="primary", scale=1)
|
| 137 |
|
| 138 |
-
# Output Area
|
| 139 |
gr.Markdown("---")
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
# Event Wiring
|
| 143 |
generate_btn.click(
|
| 144 |
fn=generate_poem,
|
| 145 |
inputs=[format_dropdown, persona_dropdown, topic_input],
|
|
|
|
| 29 |
|
| 30 |
load_llm()
|
| 31 |
|
| 32 |
+
# --- Generation Function ---
|
|
|
|
| 33 |
def generate_poem(format_type, persona, topic, progress=gr.Progress()):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# 1. VISUAL FEEDBACK: Immediately show the bar
|
| 35 |
+
# This now works because the Textbox below has a fixed height!
|
| 36 |
progress(0, desc="Consulting the Muse...")
|
| 37 |
+
time.sleep(0.2) # Force a tiny pause so the eye catches the bar
|
| 38 |
+
|
| 39 |
if not llm:
|
| 40 |
return "Error: Model not loaded."
|
| 41 |
|
| 42 |
if not topic:
|
| 43 |
return "Please enter a topic!"
|
| 44 |
|
| 45 |
+
# 2. Progress Update
|
| 46 |
progress(0.2, desc=f"Summoning {persona}...")
|
| 47 |
+
time.sleep(0.3)
|
| 48 |
|
|
|
|
| 49 |
persona_map = {
|
| 50 |
"A Grumpy Pirate": "You are a salty, grumpy pirate captain. Use nautical slang, complain about the sea, and say 'Arrr'.",
|
| 51 |
"A Melancholy Philosopher": "You are a deep, existential philosopher. Use complex vocabulary, metaphor, and a somber tone.",
|
|
|
|
| 53 |
"Shakespearean Actor": "You are a dramatic Shakespearean actor. Use Early Modern English (thee, thou), dramatic flair, and iambic rhythm."
|
| 54 |
}
|
| 55 |
|
|
|
|
| 56 |
format_map = {
|
| 57 |
"Limerick": "Write a Limerick. It must have exactly 5 lines. The rhyme scheme must be A-A-B-B-A. It should be humorous.",
|
| 58 |
"Sonnet": "Write a Sonnet. It must have exactly 14 lines and express a complete thought or feeling.",
|
|
|
|
| 72 |
f"### Response:\n"
|
| 73 |
)
|
| 74 |
|
| 75 |
+
# 3. Progress Update
|
| 76 |
+
progress(0.4, desc="Drafting Masterpiece...")
|
| 77 |
|
|
|
|
| 78 |
output = llm(
|
| 79 |
prompt=full_prompt,
|
| 80 |
max_tokens=MAX_NEW_TOKENS,
|
|
|
|
| 83 |
echo=False
|
| 84 |
)
|
| 85 |
|
| 86 |
+
# 4. Progress Update
|
| 87 |
+
progress(0.9, desc="Polishing rhymes...")
|
| 88 |
time.sleep(0.2)
|
| 89 |
|
| 90 |
return output['choices'][0]['text'].strip()
|
| 91 |
|
| 92 |
+
# --- UI Layout ---
|
| 93 |
with gr.Blocks(title="The Poetry Workshop", theme=gr.themes.Soft()) as demo:
|
| 94 |
|
|
|
|
| 95 |
gr.Markdown("# 🖋️ The Poetry Workshop")
|
| 96 |
gr.Markdown("Your own personal muse to help you get started with poetry.")
|
| 97 |
|
|
|
|
| 98 |
with gr.Group():
|
| 99 |
with gr.Row(equal_height=True):
|
| 100 |
gr.Markdown("### I want to write a ")
|
|
|
|
| 116 |
scale=3
|
| 117 |
)
|
| 118 |
|
|
|
|
| 119 |
with gr.Row(equal_height=True):
|
| 120 |
gr.Markdown("### about this topic: ")
|
| 121 |
topic_input = gr.Textbox(
|
|
|
|
| 126 |
)
|
| 127 |
generate_btn = gr.Button("✨ Create Masterpiece", variant="primary", scale=1)
|
| 128 |
|
|
|
|
| 129 |
gr.Markdown("---")
|
| 130 |
+
|
| 131 |
+
# --- CRITICAL CHANGE HERE ---
|
| 132 |
+
# Swapped gr.Markdown for gr.Textbox.
|
| 133 |
+
# 'lines=10' forces the box to be visible immediately, so the loading bar has a place to live.
|
| 134 |
+
output_display = gr.Textbox(
|
| 135 |
+
label="Your Poem",
|
| 136 |
+
lines=12, # Ensures a big empty box exists on load
|
| 137 |
+
interactive=False, # Read-only
|
| 138 |
+
placeholder="Your masterpiece will appear here..."
|
| 139 |
+
)
|
| 140 |
|
|
|
|
| 141 |
generate_btn.click(
|
| 142 |
fn=generate_poem,
|
| 143 |
inputs=[format_dropdown, persona_dropdown, topic_input],
|