Commit ·
fcb2aaa
1
Parent(s): 4bc617b
Fix UI state reset after generation cancellation
Browse files655 unmodified lines6566576586596606616626635 unmodified lines6696706716706726736746756766776786796806815 unmodified lines687688689690691682683684685686687692693694695696697698699700655 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)5 unmodified lines except GeneratorExit: # Handle Gradio's cancellation signal gracefully print("Generation cancelled by user.") # Don't yield anything here - let the finally block handle UI reset # Immediately reset UI state before returning yield { txt: gr.update(interactive=True), submit_btn: gr.update(interactive=True), cancel_btn: gr.update(visible=False), } ui_reset_done = True return except Exception as e: print(f"An error occurred during generation: {e}")5 unmodified lines yield {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 block if 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), } 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