WWMachine commited on
Commit
edf06e3
·
verified ·
1 Parent(s): b1c4e49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -1,15 +1,14 @@
1
-
2
-
3
  import gradio as gr
4
  from llama_cpp import Llama
5
  from huggingface_hub import hf_hub_download
 
6
 
7
  # --- Configuration ---
8
  MODEL_REPO = "Kezovic/iris-q4gguf-lora-test"
9
  MODEL_FILE = "Llama-3.2-1B-Instruct.Q4_K_M.gguf"
10
- CONTEXT_WINDOW = 2048 # Reduced slightly for speed, poetry is short
11
- MAX_NEW_TOKENS = 512 # Poems rarely need more than this
12
- TEMPERATURE = 0.9 # Slightly higher for creativity
13
 
14
  # --- Model Loading ---
15
  llm = None
@@ -31,18 +30,25 @@ def load_llm():
31
  load_llm()
32
 
33
  # --- The "A-Grade" Logic: Hardcoded Prompt Engineering ---
34
- def generate_poem(format_type, persona, topic):
 
35
  """
36
  Takes user variables and wraps them in a sophisticated prompt
37
  that the user never sees.
38
  """
 
 
 
39
  if not llm:
40
  return "Error: Model not loaded."
41
 
42
  if not topic:
43
  return "Please enter a topic!"
44
 
45
- # 1. Define Persona Instructions (The "Voice")
 
 
 
46
  persona_map = {
47
  "A Grumpy Pirate": "You are a salty, grumpy pirate captain. Use nautical slang, complain about the sea, and say 'Arrr'.",
48
  "A Melancholy Philosopher": "You are a deep, existential philosopher. Use complex vocabulary, metaphor, and a somber tone.",
@@ -50,9 +56,6 @@ def generate_poem(format_type, persona, topic):
50
  "Shakespearean Actor": "You are a dramatic Shakespearean actor. Use Early Modern English (thee, thou), dramatic flair, and iambic rhythm."
51
  }
52
 
53
- # 2. Define Format Instructions (The "Constraint")
54
- # We use structural formats (Rhyme/Lines) rather than syllable counting (Hexameter)
55
- # to prevent the model from failing at math.
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.",
@@ -61,8 +64,6 @@ def generate_poem(format_type, persona, topic):
61
  "Ballad": "Write a Ballad. It must tell a narrative story with a clear beginning, middle, and end. Use four-line stanzas (quatrains) and a simple rhyme scheme like A-B-C-B. Keep the language simple and musical."
62
  }
63
 
64
- # 3. Construct the "Super Prompt"
65
- # This is the invisible work that gets you the "A".
66
  selected_voice = persona_map.get(persona, "You are a helpful assistant.")
67
  selected_constraint = format_map.get(format_type, "Write a poem.")
68
 
@@ -74,6 +75,9 @@ def generate_poem(format_type, persona, topic):
74
  f"### Response:\n"
75
  )
76
 
 
 
 
77
  # 4. Generate
78
  output = llm(
79
  prompt=full_prompt,
@@ -83,14 +87,18 @@ def generate_poem(format_type, persona, topic):
83
  echo=False
84
  )
85
 
 
 
 
 
86
  return output['choices'][0]['text'].strip()
87
 
88
  # --- The Innovative UI Layout ---
89
  with gr.Blocks(title="The Poetry Workshop", theme=gr.themes.Soft()) as demo:
90
 
91
  # Header
92
- gr.Markdown("# 🖋️ The AI Poetry Workshop")
93
- gr.Markdown("Don't suffer from writer's block. Configure your muse below.")
94
 
95
  # The "Sentence" Layout (Mad Libs Style)
96
  with gr.Group():
 
 
 
1
  import gradio as gr
2
  from llama_cpp import Llama
3
  from huggingface_hub import hf_hub_download
4
+ import time # Imported to add small pauses for effect
5
 
6
  # --- Configuration ---
7
  MODEL_REPO = "Kezovic/iris-q4gguf-lora-test"
8
  MODEL_FILE = "Llama-3.2-1B-Instruct.Q4_K_M.gguf"
9
+ CONTEXT_WINDOW = 2048
10
+ MAX_NEW_TOKENS = 400
11
+ TEMPERATURE = 0.8
12
 
13
  # --- Model Loading ---
14
  llm = None
 
30
  load_llm()
31
 
32
  # --- The "A-Grade" Logic: Hardcoded Prompt Engineering ---
33
+ # UPDATE: Added 'progress' argument to the function signature
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. Update Progress: Start
40
+ progress(0, desc="Initializing 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. Update Progress: Constructing Prompt
49
+ progress(0.2, desc=f"Summoning {persona}...")
50
+ time.sleep(0.5) # Artificial pause so user sees the message (optional)
51
+
52
  persona_map = {
53
  "A Grumpy Pirate": "You are a salty, grumpy pirate captain. Use nautical slang, complain about the sea, and say 'Arrr'.",
54
  "A Melancholy Philosopher": "You are a deep, existential philosopher. Use complex vocabulary, metaphor, and a somber tone.",
 
56
  "Shakespearean Actor": "You are a dramatic Shakespearean actor. Use Early Modern English (thee, thou), dramatic flair, and iambic rhythm."
57
  }
58
 
 
 
 
59
  format_map = {
60
  "Limerick": "Write a Limerick. It must have exactly 5 lines. The rhyme scheme must be A-A-B-B-A. It should be humorous.",
61
  "Sonnet": "Write a Sonnet. It must have exactly 14 lines and express a complete thought or feeling.",
 
64
  "Ballad": "Write a Ballad. It must tell a narrative story with a clear beginning, middle, and end. Use four-line stanzas (quatrains) and a simple rhyme scheme like A-B-C-B. Keep the language simple and musical."
65
  }
66
 
 
 
67
  selected_voice = persona_map.get(persona, "You are a helpful assistant.")
68
  selected_constraint = format_map.get(format_type, "Write a poem.")
69
 
 
75
  f"### Response:\n"
76
  )
77
 
78
+ # 3. Update Progress: Generation Started
79
+ progress(0.5, desc="Drafting Masterpiece (This may take a moment)...")
80
+
81
  # 4. Generate
82
  output = llm(
83
  prompt=full_prompt,
 
87
  echo=False
88
  )
89
 
90
+ # 5. Update Progress: Done
91
+ progress(0.9, desc="Polishing rhymes...")
92
+ time.sleep(0.5)
93
+
94
  return output['choices'][0]['text'].strip()
95
 
96
  # --- The Innovative UI Layout ---
97
  with gr.Blocks(title="The Poetry Workshop", theme=gr.themes.Soft()) as demo:
98
 
99
  # Header
100
+ gr.Markdown("# 🖋️ The Poetry Workshop")
101
+ gr.Markdown("Your own personal muse to help you get started with poetry.")
102
 
103
  # The "Sentence" Layout (Mad Libs Style)
104
  with gr.Group():