Commit ·
6dc4bdb
1
Parent(s): 6ff484e
Simplify GeneratorExit handling - let it propagate and rely on finally block
Browse files656 unmodified lines6576586596606616606616622 unmodified lines6656666676706716726736746756766776786796686696704 unmodified lines675676677690691692693694695696697678679680681682683684685686656 unmodified lines cancel_btn: gr.update(visible=True), }
ui_reset_done = False
try: # 2. Call the backend and stream updates backend_args = [user_msg, chat_history] + list(args)2 unmodified lines chat: response_chunk[0], dbg: response_chunk[1], } except GeneratorExit: # Handle Gradio's cancellation signal gracefully print("Generation cancelled by user.") # Immediately reset UI state, then re-raise to properly close generator yield { txt: gr.update(interactive=True), submit_btn: gr.update(interactive=True), cancel_btn: gr.update(visible=False), } raise # Re-raise GeneratorExit to properly close the generator except Exception as e: print(f"An error occurred during generation: {e}") # If an error happens, add it to the chat history to inform the user.4 unmodified lines yield {chat: error_history} finally: # 3. ALWAYS reset the UI to an "idle" state upon completion, error, or cancellation. # Skip if GeneratorExit was raised (already handled above) if not isinstance(sys.exc_info()[1], GeneratorExit): print("Resetting UI state.") yield { txt: gr.update(interactive=True), submit_btn: gr.update(interactive=True), cancel_btn: gr.update(visible=False), } print("Resetting UI state.") yield { txt: gr.update(interactive=True), submit_btn: gr.update(interactive=True), cancel_btn: gr.update(visible=False), }
def set_cancel_flag(): """Called by the cancel button, sets the global event."""app.py+6-20