Spaces:
Running
Running
Handle GeneratorExit by yielding UI reset and returning cleanly
Browse files
app.py
CHANGED
|
@@ -665,6 +665,15 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
|
|
| 665 |
chat: response_chunk[0],
|
| 666 |
dbg: response_chunk[1],
|
| 667 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
except Exception as e:
|
| 669 |
print(f"An error occurred during generation: {e}")
|
| 670 |
# If an error happens, add it to the chat history to inform the user.
|
|
@@ -674,7 +683,7 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
|
|
| 674 |
]
|
| 675 |
yield {chat: error_history}
|
| 676 |
finally:
|
| 677 |
-
#
|
| 678 |
print("Resetting UI state.")
|
| 679 |
yield {
|
| 680 |
txt: gr.update(interactive=True),
|
|
|
|
| 665 |
chat: response_chunk[0],
|
| 666 |
dbg: response_chunk[1],
|
| 667 |
}
|
| 668 |
+
except GeneratorExit:
|
| 669 |
+
# Handle GeneratorExit by resetting UI and returning cleanly
|
| 670 |
+
print("Generation cancelled by user.")
|
| 671 |
+
yield {
|
| 672 |
+
txt: gr.update(interactive=True),
|
| 673 |
+
submit_btn: gr.update(interactive=True),
|
| 674 |
+
cancel_btn: gr.update(visible=False),
|
| 675 |
+
}
|
| 676 |
+
return # Don't re-raise, just return cleanly
|
| 677 |
except Exception as e:
|
| 678 |
print(f"An error occurred during generation: {e}")
|
| 679 |
# If an error happens, add it to the chat history to inform the user.
|
|
|
|
| 683 |
]
|
| 684 |
yield {chat: error_history}
|
| 685 |
finally:
|
| 686 |
+
# Reset UI for normal completion or errors
|
| 687 |
print("Resetting UI state.")
|
| 688 |
yield {
|
| 689 |
txt: gr.update(interactive=True),
|