Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -111,10 +111,9 @@ custom_css = """
|
|
| 111 |
"""
|
| 112 |
|
| 113 |
with gr.Blocks(theme=gr.themes.Soft(), fill_height=True, css=custom_css) as demo:
|
| 114 |
-
#
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
is_generating_state = gr.State(False) # To prevent multiple submissions
|
| 118 |
|
| 119 |
# Model info and CTA section
|
| 120 |
gr.HTML("""
|
|
@@ -155,7 +154,7 @@ Think using bullet points and short sentences to simulate thoughts and emoticons
|
|
| 155 |
bubble_full_width=False,
|
| 156 |
height=500,
|
| 157 |
show_copy_button=True,
|
| 158 |
-
type="messages"
|
| 159 |
)
|
| 160 |
|
| 161 |
with gr.Row():
|
|
@@ -237,19 +236,17 @@ Think using bullet points and short sentences to simulate thoughts and emoticons
|
|
| 237 |
info="Maximum response length"
|
| 238 |
)
|
| 239 |
|
| 240 |
-
def handle_user_message(user_message: str,
|
| 241 |
-
|
| 242 |
-
temp: float, top_p_val: float, top_k_val: int,
|
| 243 |
min_p_val: float, max_tokens: int):
|
| 244 |
"""
|
| 245 |
-
Handles user input, updates
|
| 246 |
"""
|
| 247 |
# Prevent multiple submissions
|
| 248 |
if is_generating or not user_message.strip():
|
| 249 |
return {
|
| 250 |
-
chatbot:
|
| 251 |
-
|
| 252 |
-
model_history_state: model_history,
|
| 253 |
is_generating_state: is_generating,
|
| 254 |
user_input: user_message,
|
| 255 |
submit_btn: gr.update(interactive=not is_generating)
|
|
@@ -258,17 +255,13 @@ Think using bullet points and short sentences to simulate thoughts and emoticons
|
|
| 258 |
# Set generating state
|
| 259 |
is_generating = True
|
| 260 |
|
| 261 |
-
#
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
# Update display history (for Gradio chatbot)
|
| 265 |
-
display_history.append([user_message.strip(), None])
|
| 266 |
|
| 267 |
# Yield intermediate state to show user message and disable input
|
| 268 |
yield {
|
| 269 |
-
chatbot:
|
| 270 |
-
|
| 271 |
-
model_history_state: model_history,
|
| 272 |
is_generating_state: is_generating,
|
| 273 |
user_input: "",
|
| 274 |
submit_btn: gr.update(interactive=False, value="π Generating...")
|
|
@@ -278,7 +271,9 @@ Think using bullet points and short sentences to simulate thoughts and emoticons
|
|
| 278 |
messages_for_model = []
|
| 279 |
if system_prompt_text.strip():
|
| 280 |
messages_for_model.append({"role": "system", "content": system_prompt_text.strip()})
|
| 281 |
-
|
|
|
|
|
|
|
| 282 |
|
| 283 |
try:
|
| 284 |
# Generate response with hyperparameters
|
|
@@ -295,83 +290,83 @@ Think using bullet points and short sentences to simulate thoughts and emoticons
|
|
| 295 |
if thinking and thinking.strip():
|
| 296 |
formatted_response = f"""<details>
|
| 297 |
<summary><b>π€ Show Reasoning Process</b></summary>
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
{answer}"""
|
| 300 |
else:
|
| 301 |
formatted_response = answer
|
| 302 |
|
| 303 |
-
#
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
# Update display history with formatted response
|
| 307 |
-
display_history[-1][1] = formatted_response
|
| 308 |
|
| 309 |
except Exception as e:
|
| 310 |
error_msg = f"β Error generating response: {str(e)}"
|
| 311 |
-
|
| 312 |
-
# Don't add error to model history to avoid confusing the model
|
| 313 |
|
| 314 |
# Reset generating state
|
| 315 |
is_generating = False
|
| 316 |
|
| 317 |
# Final yield with complete response
|
| 318 |
yield {
|
| 319 |
-
chatbot:
|
| 320 |
-
|
| 321 |
-
model_history_state: model_history,
|
| 322 |
is_generating_state: is_generating,
|
| 323 |
user_input: "",
|
| 324 |
submit_btn: gr.update(interactive=True, value="Send")
|
| 325 |
}
|
| 326 |
|
| 327 |
def clear_history():
|
| 328 |
-
"""Clear
|
| 329 |
return {
|
| 330 |
chatbot: [],
|
| 331 |
-
|
| 332 |
-
model_history_state: [],
|
| 333 |
is_generating_state: False,
|
| 334 |
user_input: "",
|
| 335 |
submit_btn: gr.update(interactive=True, value="Send")
|
| 336 |
}
|
| 337 |
|
| 338 |
-
def retry_last(
|
| 339 |
temp: float, top_p_val: float, top_k_val: int,
|
| 340 |
min_p_val: float, max_tokens: int):
|
| 341 |
"""
|
| 342 |
-
Retry the last user message
|
| 343 |
"""
|
| 344 |
-
# Safety check: ensure there is a
|
| 345 |
-
if not
|
| 346 |
# If nothing to retry, yield the current state and stop
|
| 347 |
yield {
|
| 348 |
-
chatbot:
|
| 349 |
-
|
| 350 |
-
model_history_state: model_history,
|
| 351 |
is_generating_state: False
|
| 352 |
}
|
| 353 |
return
|
| 354 |
|
| 355 |
-
#
|
| 356 |
-
|
| 357 |
-
display_history.pop() # Remove assistant's message from display history
|
| 358 |
-
|
| 359 |
-
# Get the last user message to resubmit it, then remove it
|
| 360 |
-
last_user_entry = model_history.pop()
|
| 361 |
-
last_user_msg = last_user_entry["content"]
|
| 362 |
|
| 363 |
-
#
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
|
| 368 |
# Use 'yield from' to properly call the generator and pass its updates
|
| 369 |
yield from handle_user_message(
|
| 370 |
-
last_user_msg,
|
| 371 |
-
|
| 372 |
)
|
| 373 |
|
| 374 |
-
|
| 375 |
def on_input_change(text, is_generating):
|
| 376 |
"""Handle input text changes"""
|
| 377 |
return gr.update(interactive=not is_generating and bool(text.strip()))
|
|
@@ -379,35 +374,31 @@ Think using bullet points and short sentences to simulate thoughts and emoticons
|
|
| 379 |
# Event listeners
|
| 380 |
submit_event = submit_btn.click(
|
| 381 |
handle_user_message,
|
| 382 |
-
inputs=[user_input,
|
| 383 |
-
|
| 384 |
-
outputs=[chatbot,
|
| 385 |
-
user_input, submit_btn],
|
| 386 |
show_progress=True
|
| 387 |
)
|
| 388 |
|
| 389 |
submit_event_enter = user_input.submit(
|
| 390 |
handle_user_message,
|
| 391 |
-
inputs=[user_input,
|
| 392 |
-
|
| 393 |
-
outputs=[chatbot,
|
| 394 |
-
user_input, submit_btn],
|
| 395 |
show_progress=True
|
| 396 |
)
|
| 397 |
|
| 398 |
# Clear button event
|
| 399 |
clear_btn.click(
|
| 400 |
clear_history,
|
| 401 |
-
outputs=[chatbot,
|
| 402 |
-
user_input, submit_btn]
|
| 403 |
)
|
| 404 |
|
| 405 |
# Retry button event
|
| 406 |
retry_btn.click(
|
| 407 |
retry_last,
|
| 408 |
-
inputs=[
|
| 409 |
-
|
| 410 |
-
outputs=[chatbot, display_history_state, model_history_state, is_generating_state],
|
| 411 |
show_progress=True
|
| 412 |
)
|
| 413 |
|
|
@@ -419,5 +410,4 @@ Think using bullet points and short sentences to simulate thoughts and emoticons
|
|
| 419 |
)
|
| 420 |
|
| 421 |
if __name__ == "__main__":
|
| 422 |
-
demo.launch(debug=True, share=False)
|
| 423 |
-
|
|
|
|
| 111 |
"""
|
| 112 |
|
| 113 |
with gr.Blocks(theme=gr.themes.Soft(), fill_height=True, css=custom_css) as demo:
|
| 114 |
+
# State for conversation history and generation status
|
| 115 |
+
conversation_state = gr.State([]) # List of message dictionaries with role/content
|
| 116 |
+
is_generating_state = gr.State(False) # To prevent multiple submissions
|
|
|
|
| 117 |
|
| 118 |
# Model info and CTA section
|
| 119 |
gr.HTML("""
|
|
|
|
| 154 |
bubble_full_width=False,
|
| 155 |
height=500,
|
| 156 |
show_copy_button=True,
|
| 157 |
+
type="messages" # This requires proper message format
|
| 158 |
)
|
| 159 |
|
| 160 |
with gr.Row():
|
|
|
|
| 236 |
info="Maximum response length"
|
| 237 |
)
|
| 238 |
|
| 239 |
+
def handle_user_message(user_message: str, conversation: list, system_prompt_text: str,
|
| 240 |
+
is_generating: bool, temp: float, top_p_val: float, top_k_val: int,
|
|
|
|
| 241 |
min_p_val: float, max_tokens: int):
|
| 242 |
"""
|
| 243 |
+
Handles user input, updates conversation, and generates the model's response.
|
| 244 |
"""
|
| 245 |
# Prevent multiple submissions
|
| 246 |
if is_generating or not user_message.strip():
|
| 247 |
return {
|
| 248 |
+
chatbot: conversation,
|
| 249 |
+
conversation_state: conversation,
|
|
|
|
| 250 |
is_generating_state: is_generating,
|
| 251 |
user_input: user_message,
|
| 252 |
submit_btn: gr.update(interactive=not is_generating)
|
|
|
|
| 255 |
# Set generating state
|
| 256 |
is_generating = True
|
| 257 |
|
| 258 |
+
# Add user message to conversation
|
| 259 |
+
conversation.append({"role": "user", "content": user_message.strip()})
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
# Yield intermediate state to show user message and disable input
|
| 262 |
yield {
|
| 263 |
+
chatbot: conversation,
|
| 264 |
+
conversation_state: conversation,
|
|
|
|
| 265 |
is_generating_state: is_generating,
|
| 266 |
user_input: "",
|
| 267 |
submit_btn: gr.update(interactive=False, value="π Generating...")
|
|
|
|
| 271 |
messages_for_model = []
|
| 272 |
if system_prompt_text.strip():
|
| 273 |
messages_for_model.append({"role": "system", "content": system_prompt_text.strip()})
|
| 274 |
+
|
| 275 |
+
# Add conversation history (excluding system messages for model input)
|
| 276 |
+
messages_for_model.extend([msg for msg in conversation if msg["role"] != "system"])
|
| 277 |
|
| 278 |
try:
|
| 279 |
# Generate response with hyperparameters
|
|
|
|
| 290 |
if thinking and thinking.strip():
|
| 291 |
formatted_response = f"""<details>
|
| 292 |
<summary><b>π€ Show Reasoning Process</b></summary>
|
| 293 |
+
|
| 294 |
+
{thinking}
|
| 295 |
+
|
| 296 |
+
</details>
|
| 297 |
+
|
| 298 |
{answer}"""
|
| 299 |
else:
|
| 300 |
formatted_response = answer
|
| 301 |
|
| 302 |
+
# Add assistant response to conversation
|
| 303 |
+
conversation.append({"role": "assistant", "content": formatted_response})
|
|
|
|
|
|
|
|
|
|
| 304 |
|
| 305 |
except Exception as e:
|
| 306 |
error_msg = f"β Error generating response: {str(e)}"
|
| 307 |
+
conversation.append({"role": "assistant", "content": error_msg})
|
|
|
|
| 308 |
|
| 309 |
# Reset generating state
|
| 310 |
is_generating = False
|
| 311 |
|
| 312 |
# Final yield with complete response
|
| 313 |
yield {
|
| 314 |
+
chatbot: conversation,
|
| 315 |
+
conversation_state: conversation,
|
|
|
|
| 316 |
is_generating_state: is_generating,
|
| 317 |
user_input: "",
|
| 318 |
submit_btn: gr.update(interactive=True, value="Send")
|
| 319 |
}
|
| 320 |
|
| 321 |
def clear_history():
|
| 322 |
+
"""Clear conversation history"""
|
| 323 |
return {
|
| 324 |
chatbot: [],
|
| 325 |
+
conversation_state: [],
|
|
|
|
| 326 |
is_generating_state: False,
|
| 327 |
user_input: "",
|
| 328 |
submit_btn: gr.update(interactive=True, value="Send")
|
| 329 |
}
|
| 330 |
|
| 331 |
+
def retry_last(conversation: list, system_prompt_text: str,
|
| 332 |
temp: float, top_p_val: float, top_k_val: int,
|
| 333 |
min_p_val: float, max_tokens: int):
|
| 334 |
"""
|
| 335 |
+
Retry the last user message.
|
| 336 |
"""
|
| 337 |
+
# Safety check: ensure there is a conversation and the last message was from the assistant
|
| 338 |
+
if not conversation or conversation[-1]["role"] != "assistant":
|
| 339 |
# If nothing to retry, yield the current state and stop
|
| 340 |
yield {
|
| 341 |
+
chatbot: conversation,
|
| 342 |
+
conversation_state: conversation,
|
|
|
|
| 343 |
is_generating_state: False
|
| 344 |
}
|
| 345 |
return
|
| 346 |
|
| 347 |
+
# Remove the last assistant message
|
| 348 |
+
conversation.pop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
|
| 350 |
+
# Get the last user message
|
| 351 |
+
if conversation and conversation[-1]["role"] == "user":
|
| 352 |
+
last_user_msg = conversation[-1]["content"]
|
| 353 |
+
# Remove the user message too, as handle_user_message will add it back
|
| 354 |
+
conversation.pop()
|
| 355 |
+
else:
|
| 356 |
+
# If no user message found, just return current state
|
| 357 |
+
yield {
|
| 358 |
+
chatbot: conversation,
|
| 359 |
+
conversation_state: conversation,
|
| 360 |
+
is_generating_state: False
|
| 361 |
+
}
|
| 362 |
+
return
|
| 363 |
|
| 364 |
# Use 'yield from' to properly call the generator and pass its updates
|
| 365 |
yield from handle_user_message(
|
| 366 |
+
last_user_msg, conversation, system_prompt_text, False,
|
| 367 |
+
temp, top_p_val, top_k_val, min_p_val, max_tokens
|
| 368 |
)
|
| 369 |
|
|
|
|
| 370 |
def on_input_change(text, is_generating):
|
| 371 |
"""Handle input text changes"""
|
| 372 |
return gr.update(interactive=not is_generating and bool(text.strip()))
|
|
|
|
| 374 |
# Event listeners
|
| 375 |
submit_event = submit_btn.click(
|
| 376 |
handle_user_message,
|
| 377 |
+
inputs=[user_input, conversation_state, system_prompt, is_generating_state,
|
| 378 |
+
temperature, top_p, top_k, min_p, max_new_tokens],
|
| 379 |
+
outputs=[chatbot, conversation_state, is_generating_state, user_input, submit_btn],
|
|
|
|
| 380 |
show_progress=True
|
| 381 |
)
|
| 382 |
|
| 383 |
submit_event_enter = user_input.submit(
|
| 384 |
handle_user_message,
|
| 385 |
+
inputs=[user_input, conversation_state, system_prompt, is_generating_state,
|
| 386 |
+
temperature, top_p, top_k, min_p, max_new_tokens],
|
| 387 |
+
outputs=[chatbot, conversation_state, is_generating_state, user_input, submit_btn],
|
|
|
|
| 388 |
show_progress=True
|
| 389 |
)
|
| 390 |
|
| 391 |
# Clear button event
|
| 392 |
clear_btn.click(
|
| 393 |
clear_history,
|
| 394 |
+
outputs=[chatbot, conversation_state, is_generating_state, user_input, submit_btn]
|
|
|
|
| 395 |
)
|
| 396 |
|
| 397 |
# Retry button event
|
| 398 |
retry_btn.click(
|
| 399 |
retry_last,
|
| 400 |
+
inputs=[conversation_state, system_prompt, temperature, top_p, top_k, min_p, max_new_tokens],
|
| 401 |
+
outputs=[chatbot, conversation_state, is_generating_state],
|
|
|
|
| 402 |
show_progress=True
|
| 403 |
)
|
| 404 |
|
|
|
|
| 410 |
)
|
| 411 |
|
| 412 |
if __name__ == "__main__":
|
| 413 |
+
demo.launch(debug=True, share=False)
|
|
|