Luigi commited on
Commit
4bc617b
·
1 Parent(s): fd70375

Fix GeneratorExit exception in cancel generation by adding proper exception handling

Browse files
Files changed (1) hide show
  1. app.py +10 -0
app.py CHANGED
@@ -567,6 +567,11 @@ def chat_response(user_msg, chat_history, system_prompt,
567
 
568
  gen_thread.join()
569
  yield history, debug + prompt_debug
 
 
 
 
 
570
  except Exception as e:
571
  history.append({'role': 'assistant', 'content': f"Error: {e}"})
572
  yield history, debug
@@ -659,6 +664,11 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
659
  chat: response_chunk[0],
660
  dbg: response_chunk[1],
661
  }
 
 
 
 
 
662
  except Exception as e:
663
  print(f"An error occurred during generation: {e}")
664
  # If an error happens, add it to the chat history to inform the user.
 
567
 
568
  gen_thread.join()
569
  yield history, debug + prompt_debug
570
+ except GeneratorExit:
571
+ # Handle cancellation gracefully
572
+ print("Chat response cancelled.")
573
+ # Don't yield anything - let the cancellation propagate
574
+ return
575
  except Exception as e:
576
  history.append({'role': 'assistant', 'content': f"Error: {e}"})
577
  yield history, debug
 
664
  chat: response_chunk[0],
665
  dbg: response_chunk[1],
666
  }
667
+ except GeneratorExit:
668
+ # Handle Gradio's cancellation signal gracefully
669
+ print("Generation cancelled by user.")
670
+ # Don't yield anything here - let the finally block handle UI reset
671
+ return
672
  except Exception as e:
673
  print(f"An error occurred during generation: {e}")
674
  # If an error happens, add it to the chat history to inform the user.