Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -531,8 +531,8 @@ def create_chatbot_demo():
|
|
531 |
"[[Blog](https://hkunlp.github.io/blog/2025/dream/)]" # Note: Link might be hypothetical
|
532 |
)
|
533 |
|
534 |
-
# STATE MANAGEMENT
|
535 |
-
_chat_history_store = gr.State([])
|
536 |
|
537 |
# UI COMPONENTS
|
538 |
with gr.Row():
|
@@ -565,7 +565,7 @@ def create_chatbot_demo():
|
|
565 |
label="Denoising Process Visualization",
|
566 |
combine_adjacent=False,
|
567 |
show_legend=True,
|
568 |
-
interactive=False
|
569 |
)
|
570 |
response_text_display = gr.Textbox(
|
571 |
label="Generated Response",
|
@@ -582,9 +582,8 @@ def create_chatbot_demo():
|
|
582 |
temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.4, step=0.05, label="Temperature (0 = greedy)")
|
583 |
alg_temp = gr.Slider(minimum=0.0, maximum=1.0, value=0.1, step=0.05, label="Remasking Temp (Confidence Algs)")
|
584 |
with gr.Row():
|
585 |
-
|
586 |
-
|
587 |
-
top_k = gr.Slider(minimum=0, maximum=200, value=0, step=5, label="Top-K (>0 to enable)")
|
588 |
with gr.Row():
|
589 |
remasking_strategy = gr.Radio(choices=['origin', 'maskgit_plus', 'topk_margin', 'entropy'], value='entropy', label="Remasking Strategy (Algorithm)")
|
590 |
with gr.Row():
|
@@ -596,78 +595,72 @@ def create_chatbot_demo():
|
|
596 |
# --- Event Handlers ---
|
597 |
|
598 |
def add_user_message_to_history(message: str, history_store: List[List[Optional[str]]]):
|
599 |
-
"""Adds user message
|
600 |
if not message.strip():
|
601 |
gr.Warning("Please enter a message.")
|
602 |
-
# Return unchanged
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
#
|
607 |
-
history_store
|
608 |
-
# Return updated history store, clear input box, clear vis, clear response text
|
609 |
-
# Outputs: _chat_history_store, user_input, output_vis, response_text_display
|
610 |
-
return history_store, "", [], "" # Clear user_input only on success
|
611 |
|
612 |
def clear_conversation():
|
613 |
-
"""Clears the chat history
|
614 |
-
#
|
615 |
-
return [], [], "", [], "" #
|
616 |
-
|
617 |
|
618 |
# --- Connect UI elements ---
|
619 |
|
620 |
-
#
|
621 |
generation_inputs = [
|
622 |
_chat_history_store, gen_length, steps, constraints_input,
|
623 |
temperature, top_p, top_k, remasking_strategy, alg_temp,
|
624 |
visualization_delay
|
625 |
]
|
626 |
-
#
|
627 |
-
|
628 |
-
|
629 |
-
#
|
630 |
-
|
631 |
-
_chat_history_store, # Update state
|
632 |
-
user_input, # Clear input (or return original if invalid)
|
633 |
-
output_vis, # Clear visualization
|
634 |
-
response_text_display # Clear response text
|
635 |
-
]
|
636 |
|
637 |
# Handle Textbox Submission (Enter key)
|
638 |
submit_listener = user_input.submit(
|
639 |
fn=add_user_message_to_history,
|
640 |
inputs=[user_input, _chat_history_store],
|
641 |
-
|
642 |
-
|
643 |
-
)
|
|
|
|
|
644 |
fn=generate_dream_response,
|
645 |
-
inputs=generation_inputs, #
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
)
|
650 |
|
651 |
# Handle Send Button Click
|
652 |
click_listener = send_btn.click(
|
653 |
fn=add_user_message_to_history,
|
654 |
inputs=[user_input, _chat_history_store],
|
655 |
-
outputs=
|
656 |
-
|
657 |
-
|
|
|
658 |
fn=generate_dream_response,
|
659 |
-
inputs=generation_inputs,
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
)
|
664 |
|
665 |
# Clear Button Action
|
666 |
clear_btn.click(
|
667 |
clear_conversation,
|
668 |
inputs=[],
|
669 |
-
|
670 |
-
|
671 |
)
|
672 |
|
673 |
return demo
|
|
|
531 |
"[[Blog](https://hkunlp.github.io/blog/2025/dream/)]" # Note: Link might be hypothetical
|
532 |
)
|
533 |
|
534 |
+
# STATE MANAGEMENT: Use a hidden state variable to store the actual list
|
535 |
+
_chat_history_store = gr.State([])
|
536 |
|
537 |
# UI COMPONENTS
|
538 |
with gr.Row():
|
|
|
565 |
label="Denoising Process Visualization",
|
566 |
combine_adjacent=False,
|
567 |
show_legend=True,
|
568 |
+
interactive=False
|
569 |
)
|
570 |
response_text_display = gr.Textbox(
|
571 |
label="Generated Response",
|
|
|
582 |
temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.4, step=0.05, label="Temperature (0 = greedy)")
|
583 |
alg_temp = gr.Slider(minimum=0.0, maximum=1.0, value=0.1, step=0.05, label="Remasking Temp (Confidence Algs)")
|
584 |
with gr.Row():
|
585 |
+
top_p = gr.Slider(minimum=0.0, maximum=1.0, value=0.95, step=0.05, label="Top-P (0 disables)")
|
586 |
+
top_k = gr.Slider(minimum=0, maximum=200, value=0, step=5, label="Top-K (0 disables)")
|
|
|
587 |
with gr.Row():
|
588 |
remasking_strategy = gr.Radio(choices=['origin', 'maskgit_plus', 'topk_margin', 'entropy'], value='entropy', label="Remasking Strategy (Algorithm)")
|
589 |
with gr.Row():
|
|
|
595 |
# --- Event Handlers ---
|
596 |
|
597 |
def add_user_message_to_history(message: str, history_store: List[List[Optional[str]]]):
|
598 |
+
"""Adds user message, clears input, prepares for bot response."""
|
599 |
if not message.strip():
|
600 |
gr.Warning("Please enter a message.")
|
601 |
+
# Return unchanged history store, history for UI, empty input, empty vis, empty response
|
602 |
+
return history_store, history_store, "", [], ""
|
603 |
+
# Append user message with placeholder for bot response
|
604 |
+
history_store.append([message, None])
|
605 |
+
# Return updated history store for the state, history for the UI, clear other fields
|
606 |
+
return history_store, history_store, "", [], ""
|
|
|
|
|
|
|
607 |
|
608 |
def clear_conversation():
|
609 |
+
"""Clears the chat history, visualization, and response text."""
|
610 |
+
# Return empty values for all relevant outputs
|
611 |
+
return [], [], "", [], "" # History store, chatbot UI, input, vis, response text
|
|
|
612 |
|
613 |
# --- Connect UI elements ---
|
614 |
|
615 |
+
# Define the inputs for the generation function once
|
616 |
generation_inputs = [
|
617 |
_chat_history_store, gen_length, steps, constraints_input,
|
618 |
temperature, top_p, top_k, remasking_strategy, alg_temp,
|
619 |
visualization_delay
|
620 |
]
|
621 |
+
# Define the outputs for the generation function's yield values
|
622 |
+
# Yields: history_list, vis_data, response_text
|
623 |
+
# CRITICAL FIX: Map the first yielded value (history_list) back to _chat_history_store
|
624 |
+
# as well as chatbot_ui.
|
625 |
+
generation_outputs = [_chat_history_store, chatbot_ui, output_vis, response_text_display]
|
|
|
|
|
|
|
|
|
|
|
626 |
|
627 |
# Handle Textbox Submission (Enter key)
|
628 |
submit_listener = user_input.submit(
|
629 |
fn=add_user_message_to_history,
|
630 |
inputs=[user_input, _chat_history_store],
|
631 |
+
# Outputs of step 1: update store, UI, clear inputs/outputs
|
632 |
+
outputs=[_chat_history_store, chatbot_ui, user_input, output_vis, response_text_display]
|
633 |
+
)
|
634 |
+
# Chain the bot response generation after the user message is added
|
635 |
+
submit_listener.then(
|
636 |
fn=generate_dream_response,
|
637 |
+
inputs=generation_inputs, # Passes the updated _chat_history_store
|
638 |
+
# Outputs of step 2: **UPDATE STORE**, UI, vis, response text
|
639 |
+
outputs=generation_outputs, # Maps the final yielded history back to the store
|
640 |
+
show_progress="hidden"
|
641 |
)
|
642 |
|
643 |
# Handle Send Button Click
|
644 |
click_listener = send_btn.click(
|
645 |
fn=add_user_message_to_history,
|
646 |
inputs=[user_input, _chat_history_store],
|
647 |
+
outputs=[_chat_history_store, chatbot_ui, user_input, output_vis, response_text_display]
|
648 |
+
)
|
649 |
+
# Chain the bot response generation after the user message is added
|
650 |
+
click_listener.then(
|
651 |
fn=generate_dream_response,
|
652 |
+
inputs=generation_inputs,
|
653 |
+
# Outputs of step 2: **UPDATE STORE**, UI, vis, response text
|
654 |
+
outputs=generation_outputs, # Map final history back to store here too
|
655 |
+
show_progress="hidden"
|
656 |
)
|
657 |
|
658 |
# Clear Button Action
|
659 |
clear_btn.click(
|
660 |
clear_conversation,
|
661 |
inputs=[],
|
662 |
+
# Ensure clear updates the state variable too
|
663 |
+
outputs=[_chat_history_store, chatbot_ui, user_input, output_vis, response_text_display]
|
664 |
)
|
665 |
|
666 |
return demo
|