Spaces:
Running
Running
feat(ui): add stop buttons to chat and image tabs
Browse files- [feat] Add `gr.Button` 'chat_stop' and implement its click event for chat generation cancellation (ui_components.py:create_chat_tab:34,82-87)
- [refactor] Assign `chat_submit.click()` and `chat_input.submit()` to variables (ui_components.py:create_chat_tab:70,77)
- [feat] Add `gr.Button` 'stop_generate_btn' and implement its click event for image generation cancellation (ui_components.py:create_image_tab:213,228-233)
- [refactor] Enclose `generate_btn` within `gr.Row()` for layout; assign `generate_btn.click()` to `gen_event` variable (ui_components.py:create_image_tab:205-212,220)
- ui_components.py +29 -10
ui_components.py
CHANGED
|
@@ -33,6 +33,7 @@ def create_chat_tab(handle_chat_submit_fn, handle_chat_retry_fn=None):
|
|
| 33 |
container=False
|
| 34 |
)
|
| 35 |
chat_submit = gr.Button("Send", variant="primary", scale=1)
|
|
|
|
| 36 |
|
| 37 |
# Configuration options below the chat
|
| 38 |
with gr.Row():
|
|
@@ -67,20 +68,28 @@ def create_chat_tab(handle_chat_submit_fn, handle_chat_retry_fn=None):
|
|
| 67 |
create_chat_tips()
|
| 68 |
|
| 69 |
# Connect chat events (streaming auto-detected from generator function)
|
| 70 |
-
chat_submit.click(
|
| 71 |
fn=handle_chat_submit_fn,
|
| 72 |
inputs=[chat_input, chatbot_display, chat_system_message, chat_model_name,
|
| 73 |
chat_max_tokens, chat_temperature, chat_top_p],
|
| 74 |
outputs=[chatbot_display, chat_input]
|
| 75 |
)
|
| 76 |
|
| 77 |
-
chat_input.submit(
|
| 78 |
fn=handle_chat_submit_fn,
|
| 79 |
inputs=[chat_input, chatbot_display, chat_system_message, chat_model_name,
|
| 80 |
chat_max_tokens, chat_temperature, chat_top_p],
|
| 81 |
outputs=[chatbot_display, chat_input]
|
| 82 |
)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
# Enable retry icon and bind handler if provided
|
| 85 |
if handle_chat_retry_fn is not None:
|
| 86 |
chatbot_display.retry(
|
|
@@ -205,13 +214,15 @@ def create_image_tab(handle_image_generation_fn):
|
|
| 205 |
label="Seed", info="-1 for random"
|
| 206 |
)
|
| 207 |
|
| 208 |
-
# Generate
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
|
|
|
|
|
|
| 215 |
|
| 216 |
# Quick model presets
|
| 217 |
create_image_presets(img_model_name, img_provider)
|
|
@@ -220,7 +231,7 @@ def create_image_tab(handle_image_generation_fn):
|
|
| 220 |
create_image_examples(img_prompt)
|
| 221 |
|
| 222 |
# Connect image generation events
|
| 223 |
-
generate_btn.click(
|
| 224 |
fn=handle_image_generation_fn,
|
| 225 |
inputs=[
|
| 226 |
img_prompt, img_model_name, img_provider, img_negative_prompt,
|
|
@@ -229,6 +240,14 @@ def create_image_tab(handle_image_generation_fn):
|
|
| 229 |
outputs=[output_image, status_text]
|
| 230 |
)
|
| 231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
def create_image_presets(img_model_name, img_provider):
|
| 234 |
"""Create quick model presets for image generation."""
|
|
|
|
| 33 |
container=False
|
| 34 |
)
|
| 35 |
chat_submit = gr.Button("Send", variant="primary", scale=1)
|
| 36 |
+
chat_stop = gr.Button("⏹ Stop", variant="secondary", scale=0)
|
| 37 |
|
| 38 |
# Configuration options below the chat
|
| 39 |
with gr.Row():
|
|
|
|
| 68 |
create_chat_tips()
|
| 69 |
|
| 70 |
# Connect chat events (streaming auto-detected from generator function)
|
| 71 |
+
chat_send_event = chat_submit.click(
|
| 72 |
fn=handle_chat_submit_fn,
|
| 73 |
inputs=[chat_input, chatbot_display, chat_system_message, chat_model_name,
|
| 74 |
chat_max_tokens, chat_temperature, chat_top_p],
|
| 75 |
outputs=[chatbot_display, chat_input]
|
| 76 |
)
|
| 77 |
|
| 78 |
+
chat_enter_event = chat_input.submit(
|
| 79 |
fn=handle_chat_submit_fn,
|
| 80 |
inputs=[chat_input, chatbot_display, chat_system_message, chat_model_name,
|
| 81 |
chat_max_tokens, chat_temperature, chat_top_p],
|
| 82 |
outputs=[chatbot_display, chat_input]
|
| 83 |
)
|
| 84 |
|
| 85 |
+
# Stop current chat generation
|
| 86 |
+
chat_stop.click(
|
| 87 |
+
fn=None,
|
| 88 |
+
inputs=None,
|
| 89 |
+
outputs=None,
|
| 90 |
+
cancels=[chat_send_event, chat_enter_event]
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
# Enable retry icon and bind handler if provided
|
| 94 |
if handle_chat_retry_fn is not None:
|
| 95 |
chatbot_display.retry(
|
|
|
|
| 214 |
label="Seed", info="-1 for random"
|
| 215 |
)
|
| 216 |
|
| 217 |
+
# Generate and Stop buttons
|
| 218 |
+
with gr.Row():
|
| 219 |
+
generate_btn = gr.Button(
|
| 220 |
+
"🎨 Generate Image",
|
| 221 |
+
variant="primary",
|
| 222 |
+
size="lg",
|
| 223 |
+
scale=2
|
| 224 |
+
)
|
| 225 |
+
stop_generate_btn = gr.Button("⏹ Stop", variant="secondary")
|
| 226 |
|
| 227 |
# Quick model presets
|
| 228 |
create_image_presets(img_model_name, img_provider)
|
|
|
|
| 231 |
create_image_examples(img_prompt)
|
| 232 |
|
| 233 |
# Connect image generation events
|
| 234 |
+
gen_event = generate_btn.click(
|
| 235 |
fn=handle_image_generation_fn,
|
| 236 |
inputs=[
|
| 237 |
img_prompt, img_model_name, img_provider, img_negative_prompt,
|
|
|
|
| 240 |
outputs=[output_image, status_text]
|
| 241 |
)
|
| 242 |
|
| 243 |
+
# Stop current image generation
|
| 244 |
+
stop_generate_btn.click(
|
| 245 |
+
fn=None,
|
| 246 |
+
inputs=None,
|
| 247 |
+
outputs=None,
|
| 248 |
+
cancels=[gen_event]
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
|
| 252 |
def create_image_presets(img_model_name, img_provider):
|
| 253 |
"""Create quick model presets for image generation."""
|