Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
+
This script completely replaces the "Boilerplate Chatbot" with a Generative UI (The "Poetry Workshop").
|
| 2 |
+
|
| 3 |
+
It implements the "Mad Libs" style interface we discussed:
|
| 4 |
+
|
| 5 |
+
Dropdowns for Format and Persona (Hardcoded constraints).
|
| 6 |
+
|
| 7 |
+
Free Text for the Topic (User agency).
|
| 8 |
+
|
| 9 |
+
No Chat History: It focuses on a single, high-quality output.
|
| 10 |
+
|
| 11 |
+
🐍 The "Poetry Workshop" Script
|
| 12 |
+
Python
|
| 13 |
+
|
| 14 |
import gradio as gr
|
| 15 |
+
from llama_cpp import Llama
|
| 16 |
+
from huggingface_hub import hf_hub_download
|
| 17 |
+
|
| 18 |
+
# --- Configuration ---
|
| 19 |
+
MODEL_REPO = "Kezovic/iris-q4gguf-lora-test"
|
| 20 |
+
MODEL_FILE = "Llama-3.2-1B-Instruct.Q4_K_M.gguf"
|
| 21 |
+
CONTEXT_WINDOW = 2048 # Reduced slightly for speed, poetry is short
|
| 22 |
+
MAX_NEW_TOKENS = 256 # Poems rarely need more than this
|
| 23 |
+
TEMPERATURE = 0.8 # Slightly higher for creativity
|
| 24 |
+
|
| 25 |
+
# --- Model Loading ---
|
| 26 |
+
llm = None
|
| 27 |
+
def load_llm():
|
| 28 |
+
global llm
|
| 29 |
+
print("Downloading model...")
|
| 30 |
+
try:
|
| 31 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
| 32 |
+
llm = Llama(
|
| 33 |
+
model_path=model_path,
|
| 34 |
+
n_ctx=CONTEXT_WINDOW,
|
| 35 |
+
n_threads=2,
|
| 36 |
+
verbose=False
|
| 37 |
+
)
|
| 38 |
+
print("Model loaded successfully!")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Error loading model: {e}")
|
| 41 |
+
|
| 42 |
+
load_llm()
|
| 43 |
+
|
| 44 |
+
# --- The "A-Grade" Logic: Hardcoded Prompt Engineering ---
|
| 45 |
+
def generate_poem(format_type, persona, topic):
|
| 46 |
"""
|
| 47 |
+
Takes user variables and wraps them in a sophisticated prompt
|
| 48 |
+
that the user never sees.
|
| 49 |
"""
|
| 50 |
+
if not llm:
|
| 51 |
+
return "Error: Model not loaded."
|
| 52 |
+
|
| 53 |
+
if not topic:
|
| 54 |
+
return "Please enter a topic!"
|
| 55 |
+
|
| 56 |
+
# 1. Define Persona Instructions (The "Voice")
|
| 57 |
+
persona_map = {
|
| 58 |
+
"A Grumpy Pirate": "You are a salty, grumpy pirate captain. Use nautical slang, complain about the sea, and say 'Arrr'.",
|
| 59 |
+
"A Melancholy Philosopher": "You are a deep, existential philosopher. Use complex vocabulary, metaphor, and a somber tone.",
|
| 60 |
+
"A Hyperactive 5-Year-Old": "You are a very excited 5-year-old. Use simple words, lots of exclamation marks, and get distracted easily.",
|
| 61 |
+
"Shakespearean Actor": "You are a dramatic Shakespearean actor. Use Early Modern English (thee, thou), dramatic flair, and iambic rhythm."
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
# 2. Define Format Instructions (The "Constraint")
|
| 65 |
+
# We use structural formats (Rhyme/Lines) rather than syllable counting (Hexameter)
|
| 66 |
+
# to prevent the model from failing at math.
|
| 67 |
+
format_map = {
|
| 68 |
+
"Limerick": "Write a Limerick. It must have exactly 5 lines. The rhyme scheme must be A-A-B-B-A. It should be humorous.",
|
| 69 |
+
"Sonnet": "Write a Sonnet. It must have 14 lines and express a complete thought or feeling.",
|
| 70 |
+
"Haiku": "Write a Haiku. It must have 3 lines. Keep it very brief and nature-focused if possible.",
|
| 71 |
+
"Free Verse": "Write in Free Verse. Do not worry about rhyme or strict structure, focusing instead on imagery and emotion."
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
# 3. Construct the "Super Prompt"
|
| 75 |
+
# This is the invisible work that gets you the "A".
|
| 76 |
+
selected_voice = persona_map.get(persona, "You are a helpful assistant.")
|
| 77 |
+
selected_constraint = format_map.get(format_type, "Write a poem.")
|
| 78 |
+
|
| 79 |
+
full_prompt = (
|
| 80 |
+
f"### Instruction:\n"
|
| 81 |
+
f"{selected_voice}\n"
|
| 82 |
+
f"{selected_constraint}\n"
|
| 83 |
+
f"The subject of the poem is: '{topic}'.\n\n"
|
| 84 |
+
f"### Response:\n"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# 4. Generate
|
| 88 |
+
output = llm(
|
| 89 |
+
prompt=full_prompt,
|
| 90 |
+
max_tokens=MAX_NEW_TOKENS,
|
| 91 |
+
temperature=TEMPERATURE,
|
| 92 |
+
stop=["### Instruction:", "### Human:"],
|
| 93 |
+
echo=False
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
return output['choices'][0]['text'].strip()
|
| 97 |
+
|
| 98 |
+
# --- The Innovative UI Layout ---
|
| 99 |
+
with gr.Blocks(title="The Poetry Workshop", theme=gr.themes.Soft()) as demo:
|
| 100 |
+
|
| 101 |
+
# Header
|
| 102 |
+
gr.Markdown("# 🖋️ The AI Poetry Workshop")
|
| 103 |
+
gr.Markdown("Don't suffer from writer's block. Configure your muse below.")
|
| 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 ")
|
| 109 |
+
format_dropdown = gr.Dropdown(
|
| 110 |
+
choices=["Limerick", "Sonnet", "Haiku", "Free Verse"],
|
| 111 |
+
value="Limerick",
|
| 112 |
+
label="Poem Type",
|
| 113 |
+
show_label=False,
|
| 114 |
+
container=False,
|
| 115 |
+
scale=2
|
| 116 |
+
)
|
| 117 |
+
gr.Markdown("### in the style of ")
|
| 118 |
+
persona_dropdown = gr.Dropdown(
|
| 119 |
+
choices=["A Grumpy Pirate", "A Melancholy Philosopher", "A Hyperactive 5-Year-Old", "Shakespearean Actor"],
|
| 120 |
+
value="A Grumpy Pirate",
|
| 121 |
+
label="Persona",
|
| 122 |
+
show_label=False,
|
| 123 |
+
container=False,
|
| 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(
|
| 131 |
+
placeholder="e.g., my broken laptop, the smell of rain, a lost sock",
|
| 132 |
+
label="Topic",
|
| 133 |
+
show_label=False,
|
| 134 |
+
scale=5
|
| 135 |
+
)
|
| 136 |
+
generate_btn = gr.Button("✨ Create Masterpiece", variant="primary", scale=1)
|
| 137 |
+
|
| 138 |
+
# Output Area
|
| 139 |
+
gr.Markdown("---")
|
| 140 |
+
output_display = gr.Markdown(label="Your Poem")
|
| 141 |
|
| 142 |
+
# Event Wiring
|
| 143 |
+
generate_btn.click(
|
| 144 |
+
fn=generate_poem,
|
| 145 |
+
inputs=[format_dropdown, persona_dropdown, topic_input],
|
| 146 |
+
outputs=[output_display]
|
| 147 |
+
)
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|
| 150 |
+
demo.launch()
|