Luigi commited on
Commit
b7e5000
·
1 Parent(s): c49f312

Fix UI not resetting after cancel - add reset handler

Browse files

- Add reset_ui_after_cancel function to properly reset UI components
- Chain .then() after cancel button click to reset UI state
- Clear cancel_event flag for next generation
- Ensures Submit button becomes interactive again after cancellation

Files changed (1) hide show
  1. app.py +14 -1
app.py CHANGED
@@ -693,6 +693,16 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
693
  """Called by the cancel button, sets the global event."""
694
  cancel_event.set()
695
  print("Cancellation signal sent.")
 
 
 
 
 
 
 
 
 
 
696
 
697
  # Event for submitting text via Enter key or Submit button
698
  submit_event = txt.submit(
@@ -707,10 +717,13 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
707
  )
708
 
709
  # Event for the "Cancel" button.
710
- # It calls the flag-setting function and cancels the long-running submit_event.
711
  cancel_btn.click(
712
  fn=set_cancel_flag,
713
  cancels=[submit_event]
 
 
 
714
  )
715
 
716
  # Listeners for updating the duration estimate
 
693
  """Called by the cancel button, sets the global event."""
694
  cancel_event.set()
695
  print("Cancellation signal sent.")
696
+
697
+ def reset_ui_after_cancel():
698
+ """Reset UI components after cancellation."""
699
+ cancel_event.clear() # Clear the flag for next generation
700
+ print("UI reset after cancellation.")
701
+ return {
702
+ txt: gr.update(interactive=True),
703
+ submit_btn: gr.update(interactive=True),
704
+ cancel_btn: gr.update(visible=False),
705
+ }
706
 
707
  # Event for submitting text via Enter key or Submit button
708
  submit_event = txt.submit(
 
717
  )
718
 
719
  # Event for the "Cancel" button.
720
+ # It sets the cancel flag, cancels the submit event, then resets the UI.
721
  cancel_btn.click(
722
  fn=set_cancel_flag,
723
  cancels=[submit_event]
724
+ ).then(
725
+ fn=reset_ui_after_cancel,
726
+ outputs=ui_components
727
  )
728
 
729
  # Listeners for updating the duration estimate