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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -1,7 +1,7 @@
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"
@@ -30,14 +30,14 @@ def load_llm():
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."
@@ -45,10 +45,11 @@ def generate_poem(format_type, persona, topic, progress=gr.Progress()):
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,6 +57,7 @@ def generate_poem(format_type, persona, topic, progress=gr.Progress()):
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.",
@@ -75,8 +77,8 @@ def generate_poem(format_type, persona, topic, progress=gr.Progress()):
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(
@@ -87,9 +89,9 @@ def generate_poem(format_type, persona, topic, progress=gr.Progress()):
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
 
 
1
  import gradio as gr
2
  from llama_cpp import Llama
3
  from huggingface_hub import hf_hub_download
4
+ import time
5
 
6
  # --- Configuration ---
7
  MODEL_REPO = "Kezovic/iris-q4gguf-lora-test"
 
30
  load_llm()
31
 
32
  # --- The "A-Grade" Logic: Hardcoded Prompt Engineering ---
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."
 
45
  if not topic:
46
  return "Please enter a topic!"
47
 
48
+ # 2. VISUAL FEEDBACK: Update status while we build the prompt
49
  progress(0.2, desc=f"Summoning {persona}...")
50
+ time.sleep(0.3) # Small pause so the user actually sees the message
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
  "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
  f"### Response:\n"
78
  )
79
 
80
+ # 3. VISUAL FEEDBACK: Indicate the heavy lifting is starting
81
+ progress(0.4, desc="Writing your masterpiece (this may take a moment)...")
82
 
83
  # 4. Generate
84
  output = llm(
 
89
  echo=False
90
  )
91
 
92
+ # 4. VISUAL FEEDBACK: Done
93
+ progress(0.9, desc="Final polish...")
94
+ time.sleep(0.2)
95
 
96
  return output['choices'][0]['text'].strip()
97