Fix UI state reset after generation cancellation
Browse files
app.py
CHANGED
|
@@ -656,6 +656,8 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
|
|
| 656 |
cancel_btn: gr.update(visible=True),
|
| 657 |
}
|
| 658 |
|
|
|
|
|
|
|
| 659 |
try:
|
| 660 |
# 2. Call the backend and stream updates
|
| 661 |
backend_args = [user_msg, chat_history] + list(args)
|
|
@@ -667,7 +669,13 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
|
|
| 667 |
except GeneratorExit:
|
| 668 |
# Handle Gradio's cancellation signal gracefully
|
| 669 |
print("Generation cancelled by user.")
|
| 670 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 671 |
return
|
| 672 |
except Exception as e:
|
| 673 |
print(f"An error occurred during generation: {e}")
|
|
@@ -679,12 +687,14 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
|
|
| 679 |
yield {chat: error_history}
|
| 680 |
finally:
|
| 681 |
# 3. ALWAYS reset the UI to an "idle" state upon completion, error, or cancellation.
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
|
|
|
|
|
|
| 688 |
|
| 689 |
def set_cancel_flag():
|
| 690 |
"""Called by the cancel button, sets the global event."""
|
|
|
|
| 656 |
cancel_btn: gr.update(visible=True),
|
| 657 |
}
|
| 658 |
|
| 659 |
+
ui_reset_done = False
|
| 660 |
+
|
| 661 |
try:
|
| 662 |
# 2. Call the backend and stream updates
|
| 663 |
backend_args = [user_msg, chat_history] + list(args)
|
|
|
|
| 669 |
except GeneratorExit:
|
| 670 |
# Handle Gradio's cancellation signal gracefully
|
| 671 |
print("Generation cancelled by user.")
|
| 672 |
+
# Immediately reset UI state before returning
|
| 673 |
+
yield {
|
| 674 |
+
txt: gr.update(interactive=True),
|
| 675 |
+
submit_btn: gr.update(interactive=True),
|
| 676 |
+
cancel_btn: gr.update(visible=False),
|
| 677 |
+
}
|
| 678 |
+
ui_reset_done = True
|
| 679 |
return
|
| 680 |
except Exception as e:
|
| 681 |
print(f"An error occurred during generation: {e}")
|
|
|
|
| 687 |
yield {chat: error_history}
|
| 688 |
finally:
|
| 689 |
# 3. ALWAYS reset the UI to an "idle" state upon completion, error, or cancellation.
|
| 690 |
+
# Only do this if we haven't already reset in the except block
|
| 691 |
+
if not ui_reset_done:
|
| 692 |
+
print("Resetting UI state.")
|
| 693 |
+
yield {
|
| 694 |
+
txt: gr.update(interactive=True),
|
| 695 |
+
submit_btn: gr.update(interactive=True),
|
| 696 |
+
cancel_btn: gr.update(visible=False),
|
| 697 |
+
}
|
| 698 |
|
| 699 |
def set_cancel_flag():
|
| 700 |
"""Called by the cancel button, sets the global event."""
|