Commit ·
fcb2aaa
1
Parent(s): 4bc617b
Fix UI state reset after generation cancellation
Browse files655 unmodified lines6566576586596606615 unmodified lines6676686696706716726735 unmodified lines679680681682683684685686687688689690655 unmodified linescancel_btn: gr.update(visible=True),}try:# 2. Call the backend and stream updatesbackend_args = [user_msg, chat_history] + list(args)5 unmodified linesexcept GeneratorExit:# Handle Gradio's cancellation signal gracefullyprint("Generation cancelled by user.")# Don't yield anything here - let the finally block handle UI resetreturnexcept Exception as e:print(f"An error occurred during generation: {e}")5 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."""655 unmodified lines6566576586596606616626635 unmodified lines6696706716726736746756766776786796806815 unmodified lines687688689690691692693694695696697698699700655 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)5 unmodified linesexcept GeneratorExit:# Handle Gradio's cancellation signal gracefullyprint("Generation cancelled by user.")# Immediately reset UI state before returningyield {txt: gr.update(interactive=True),submit_btn: gr.update(interactive=True),cancel_btn: gr.update(visible=False),}ui_reset_done = Truereturnexcept Exception as e:print(f"An error occurred during generation: {e}")5 unmodified linesyield {chat: error_history}finally:# 3. ALWAYS reset the UI to an "idle" state upon completion, error, or cancellation.# Only do this if we haven't already reset in the except blockif not ui_reset_done: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+17-7