Luigi commited on
Commit
6dc4bdb
·
1 Parent(s): 6ff484e

Simplify GeneratorExit handling - let it propagate and rely on finally block

Browse files
Files changed (1) hide show
  1. app.py +6 -20
app.py CHANGED
@@ -657,8 +657,6 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
657
  cancel_btn: gr.update(visible=True),
658
  }
659
 
660
- ui_reset_done = False
661
-
662
  try:
663
  # 2. Call the backend and stream updates
664
  backend_args = [user_msg, chat_history] + list(args)
@@ -667,16 +665,6 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
667
  chat: response_chunk[0],
668
  dbg: response_chunk[1],
669
  }
670
- except GeneratorExit:
671
- # Handle Gradio's cancellation signal gracefully
672
- print("Generation cancelled by user.")
673
- # Immediately reset UI state, then re-raise to properly close generator
674
- yield {
675
- txt: gr.update(interactive=True),
676
- submit_btn: gr.update(interactive=True),
677
- cancel_btn: gr.update(visible=False),
678
- }
679
- raise # Re-raise GeneratorExit to properly close the generator
680
  except Exception as e:
681
  print(f"An error occurred during generation: {e}")
682
  # If an error happens, add it to the chat history to inform the user.
@@ -687,14 +675,12 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
687
  yield {chat: error_history}
688
  finally:
689
  # 3. ALWAYS reset the UI to an "idle" state upon completion, error, or cancellation.
690
- # Skip if GeneratorExit was raised (already handled above)
691
- if not isinstance(sys.exc_info()[1], GeneratorExit):
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."""
 
657
  cancel_btn: gr.update(visible=True),
658
  }
659
 
 
 
660
  try:
661
  # 2. Call the backend and stream updates
662
  backend_args = [user_msg, chat_history] + list(args)
 
665
  chat: response_chunk[0],
666
  dbg: response_chunk[1],
667
  }
 
 
 
 
 
 
 
 
 
 
668
  except Exception as e:
669
  print(f"An error occurred during generation: {e}")
670
  # If an error happens, add it to the chat history to inform the user.
 
675
  yield {chat: error_history}
676
  finally:
677
  # 3. ALWAYS reset the UI to an "idle" state upon completion, error, or cancellation.
678
+ print("Resetting UI state.")
679
+ yield {
680
+ txt: gr.update(interactive=True),
681
+ submit_btn: gr.update(interactive=True),
682
+ cancel_btn: gr.update(visible=False),
683
+ }
 
 
684
 
685
  def set_cancel_flag():
686
  """Called by the cancel button, sets the global event."""