Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=hf_token)
|
|
10 |
|
11 |
# Story genres with genre-specific example prompts
|
12 |
GENRE_EXAMPLES = {
|
13 |
-
"
|
14 |
"I follow the shimmer of fairy dust into a hidden forest"
|
15 |
"I meet a talking rabbit who claims to know a secret about the king’s lost crown"
|
16 |
"A tiny dragon appears at my window, asking for help to find its mother"
|
@@ -136,11 +136,11 @@ def respond(
|
|
136 |
chat_history: List[Tuple[str, str]],
|
137 |
genre: Optional[str] = None,
|
138 |
use_full_memory: bool = True
|
139 |
-
) -> List[
|
140 |
"""Generate a response based on the current message and conversation history."""
|
141 |
if not message.strip():
|
142 |
-
return
|
143 |
-
|
144 |
try:
|
145 |
# Format messages for API
|
146 |
api_messages = [{"role": "system", "content": get_enhanced_system_prompt(genre)}]
|
@@ -164,12 +164,18 @@ def respond(
|
|
164 |
top_p=TOP_P
|
165 |
)
|
166 |
|
167 |
-
#
|
168 |
bot_message = response.choices[0].message.content
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
except Exception as e:
|
172 |
-
|
|
|
173 |
|
174 |
def save_story(chat_history):
|
175 |
"""Convert chat history to markdown for download"""
|
@@ -237,50 +243,47 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
237 |
starter_btn4 = gr.Button("Starter 4")
|
238 |
starter_buttons = [starter_btn1, starter_btn2, starter_btn3, starter_btn4]
|
239 |
|
240 |
-
#
|
241 |
def update_starter_buttons(selected_genre):
|
242 |
-
"""Update starter buttons with examples for the selected genre."""
|
243 |
examples = get_examples_for_genre(selected_genre)
|
244 |
results = []
|
245 |
for i in range(4):
|
246 |
if i < len(examples):
|
247 |
-
# Return just the string value instead of a dict
|
248 |
results.append(examples[i])
|
249 |
else:
|
250 |
-
results.append("")
|
251 |
-
return tuple(results)
|
252 |
|
253 |
-
#
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
|
262 |
-
#
|
263 |
for starter_button in starter_buttons:
|
264 |
starter_button.click(
|
265 |
-
|
266 |
-
fn=lambda x: [{"role": "user", "content": str(x)}],
|
267 |
-
inputs=[starter_button],
|
268 |
-
outputs=[chatbot],
|
269 |
-
queue=False
|
270 |
-
).success(
|
271 |
-
# Then process with properly formatted message
|
272 |
-
fn=lambda x, h, g, m: respond(
|
273 |
-
message=x["content"], # Extract content from ChatMessage
|
274 |
-
chat_history=h if h else [],
|
275 |
-
genre=g,
|
276 |
-
use_full_memory=m
|
277 |
-
),
|
278 |
inputs=[starter_button, chatbot, genre, full_memory],
|
279 |
-
outputs=chatbot,
|
280 |
queue=True
|
281 |
)
|
282 |
|
283 |
-
#
|
284 |
genre.change(
|
285 |
fn=update_starter_buttons,
|
286 |
inputs=[genre],
|
@@ -288,8 +291,16 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
288 |
)
|
289 |
|
290 |
# Handler for user input
|
291 |
-
msg.submit(
|
292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
293 |
|
294 |
# Clear the chatbot for a new adventure
|
295 |
clear.click(lambda: [], None, chatbot, queue=False)
|
@@ -309,12 +320,19 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
309 |
queue=False
|
310 |
)
|
311 |
|
312 |
-
#
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
318 |
|
319 |
# Run the app
|
320 |
if __name__ == "__main__":
|
|
|
10 |
|
11 |
# Story genres with genre-specific example prompts
|
12 |
GENRE_EXAMPLES = {
|
13 |
+
"fairy tale": [
|
14 |
"I follow the shimmer of fairy dust into a hidden forest"
|
15 |
"I meet a talking rabbit who claims to know a secret about the king’s lost crown"
|
16 |
"A tiny dragon appears at my window, asking for help to find its mother"
|
|
|
136 |
chat_history: List[Tuple[str, str]],
|
137 |
genre: Optional[str] = None,
|
138 |
use_full_memory: bool = True
|
139 |
+
) -> Tuple[str, List[Tuple[str, str]]]:
|
140 |
"""Generate a response based on the current message and conversation history."""
|
141 |
if not message.strip():
|
142 |
+
return "", chat_history
|
143 |
+
|
144 |
try:
|
145 |
# Format messages for API
|
146 |
api_messages = [{"role": "system", "content": get_enhanced_system_prompt(genre)}]
|
|
|
164 |
top_p=TOP_P
|
165 |
)
|
166 |
|
167 |
+
# Get response content
|
168 |
bot_message = response.choices[0].message.content
|
169 |
+
|
170 |
+
# Update chat history with new exchange
|
171 |
+
updated_history = chat_history + [(message, bot_message)]
|
172 |
+
|
173 |
+
# Return updated components
|
174 |
+
return "", updated_history
|
175 |
|
176 |
except Exception as e:
|
177 |
+
error_msg = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
178 |
+
return "", chat_history + [(message, error_msg)]
|
179 |
|
180 |
def save_story(chat_history):
|
181 |
"""Convert chat history to markdown for download"""
|
|
|
243 |
starter_btn4 = gr.Button("Starter 4")
|
244 |
starter_buttons = [starter_btn1, starter_btn2, starter_btn3, starter_btn4]
|
245 |
|
246 |
+
# Simplified update function
|
247 |
def update_starter_buttons(selected_genre):
|
|
|
248 |
examples = get_examples_for_genre(selected_genre)
|
249 |
results = []
|
250 |
for i in range(4):
|
251 |
if i < len(examples):
|
|
|
252 |
results.append(examples[i])
|
253 |
else:
|
254 |
+
results.append("")
|
255 |
+
return tuple(results)
|
256 |
|
257 |
+
# New direct handler for starter clicks
|
258 |
+
def use_starter(starter_text, history, selected_genre, memory_flag):
|
259 |
+
"""Handle starter button clicks with proper message formatting"""
|
260 |
+
if not starter_text:
|
261 |
+
return "", history
|
262 |
+
|
263 |
+
try:
|
264 |
+
# Use the respond function for consistent handling
|
265 |
+
_, updated_history = respond(
|
266 |
+
message=starter_text,
|
267 |
+
chat_history=history,
|
268 |
+
genre=selected_genre,
|
269 |
+
use_full_memory=memory_flag
|
270 |
+
)
|
271 |
+
return "", updated_history
|
272 |
+
|
273 |
+
except Exception as e:
|
274 |
+
error_msg = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
275 |
+
return "", history + [(starter_text, error_msg)]
|
276 |
|
277 |
+
# Simplified button connections
|
278 |
for starter_button in starter_buttons:
|
279 |
starter_button.click(
|
280 |
+
fn=use_starter,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
281 |
inputs=[starter_button, chatbot, genre, full_memory],
|
282 |
+
outputs=[msg, chatbot], # Now returning both message and chat history
|
283 |
queue=True
|
284 |
)
|
285 |
|
286 |
+
# Update buttons when genre changes
|
287 |
genre.change(
|
288 |
fn=update_starter_buttons,
|
289 |
inputs=[genre],
|
|
|
291 |
)
|
292 |
|
293 |
# Handler for user input
|
294 |
+
msg.submit(
|
295 |
+
fn=respond,
|
296 |
+
inputs=[msg, chatbot, genre, full_memory],
|
297 |
+
outputs=[msg, chatbot] # Now returning both message and chat history
|
298 |
+
)
|
299 |
+
submit.click(
|
300 |
+
fn=respond,
|
301 |
+
inputs=[msg, chatbot, genre, full_memory],
|
302 |
+
outputs=[msg, chatbot] # Now returning both message and chat history
|
303 |
+
)
|
304 |
|
305 |
# Clear the chatbot for a new adventure
|
306 |
clear.click(lambda: [], None, chatbot, queue=False)
|
|
|
320 |
queue=False
|
321 |
)
|
322 |
|
323 |
+
# Initialize buttons with default fantasy genre examples
|
324 |
+
initial_examples = get_examples_for_genre("fantasy")
|
325 |
+
initial_button_data = tuple(
|
326 |
+
initial_examples[i] if i < len(initial_examples) else ""
|
327 |
+
for i in range(4)
|
328 |
+
)
|
329 |
+
|
330 |
+
# Update button text on page load
|
331 |
+
demo.load(
|
332 |
+
fn=lambda: initial_button_data,
|
333 |
+
outputs=starter_buttons,
|
334 |
+
queue=False
|
335 |
+
)
|
336 |
|
337 |
# Run the app
|
338 |
if __name__ == "__main__":
|