Spaces:
Sleeping
Sleeping
Commit ·
6dc4bdb
1
Parent(s): 6ff484e
Simplify GeneratorExit handling - let it propagate and rely on finally block
Browse files656 unmodified lines6576586596606616626636642 unmodified lines6676686696706716726736746756766776786796806816824 unmodified lines687688689690691692693694695696697698699700656 unmodified linescancel_btn: gr.update(visible=True),}ui_reset_done = Falsetry:# 2. Call the backend and stream updatesbackend_args = [user_msg, chat_history] + list(args)2 unmodified lineschat: response_chunk[0],dbg: response_chunk[1],}except GeneratorExit:# Handle Gradio's cancellation signal gracefullyprint("Generation cancelled by user.")# Immediately reset UI state, then re-raise to properly close generatoryield {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 generatorexcept 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 linesyield {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),}def set_cancel_flag():"""Called by the cancel button, sets the global event."""656 unmodified lines6576586596606616622 unmodified lines6656666676686696704 unmodified lines675676677678679680681682683684685686656 unmodified linescancel_btn: gr.update(visible=True),}try:# 2. Call the backend and stream updatesbackend_args = [user_msg, chat_history] + list(args)2 unmodified lineschat: response_chunk[0],dbg: response_chunk[1],}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 linesyield {chat: error_history}finally:# 3. ALWAYS reset the UI to an "idle" state upon completion, error, or cancellation.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