WWMachine commited on
Commit
f618fe2
·
verified ·
1 Parent(s): f8a4093

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -64
app.py CHANGED
@@ -1,70 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
-
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
 
16
  """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
-
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- messages.extend(history)
22
-
23
- messages.append({"role": "user", "content": message})
24
-
25
- response = ""
26
-
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
- )
62
-
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()