Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ ChatGPT-Premium-like open-source Gradio app with:
|
|
| 8 |
- Caching of processed images & embeddings
|
| 9 |
- Simple in-process queue & streaming text output
|
| 10 |
- Rate-limiting per-client (token-bucket)
|
| 11 |
-
|
| 12 |
NOTES:
|
| 13 |
- Replace model IDs with ones that match your hardware/quotas.
|
| 14 |
- For production, swap the in-process queue with Redis/Celery and use S3/MinIO for storage.
|
|
@@ -423,6 +423,9 @@ def _stream_llm_response_generator(prompt: str, client_id: str):
|
|
| 423 |
# final append
|
| 424 |
session["history"].append(("Assistant", partial))
|
| 425 |
|
|
|
|
|
|
|
|
|
|
| 426 |
# ---------------------------
|
| 427 |
# GRADIO UI
|
| 428 |
# ---------------------------
|
|
@@ -435,47 +438,55 @@ with gr.Blocks(css="""
|
|
| 435 |
|
| 436 |
with gr.Row():
|
| 437 |
with gr.Column(scale=8, elem_classes="chat-column"):
|
| 438 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
with gr.Row():
|
| 440 |
-
txt = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
submit = gr.Button("Send")
|
| 442 |
with gr.Row():
|
| 443 |
-
img_in = gr.File(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
clear_btn = gr.Button("New Chat")
|
| 445 |
-
client_id_state = gr.State(str(uuid.uuid4())) # simple per-window client id
|
| 446 |
|
|
|
|
|
|
|
|
|
|
| 447 |
def handle_send(message, client_state, files):
|
| 448 |
client_id = client_state or str(uuid.uuid4())
|
| 449 |
-
# process_request yields a generator; Gradio supports returning generator -> progressive updates
|
| 450 |
-
# We return a generator that yields strings; then the front-end should append them to chat.
|
| 451 |
gen = process_request(client_id, files, message, stream=True)
|
| 452 |
-
# We'll wrap generator so Gradio can consume it; we will return a tuple (new user msg textbox, new history)
|
| 453 |
-
# But Gradio expects the function to return: (textbox_clear, new_chat_history)
|
| 454 |
-
# We'll implement a simple approach: produce a list of chunks and finally return them as a single assistant message.
|
| 455 |
collected = ""
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
#
|
| 464 |
-
yield "", [
|
|
|
|
|
|
|
|
|
|
| 465 |
|
| 466 |
# Connect send button and textbox
|
| 467 |
submit.click(handle_send, inputs=[txt, client_id_state, img_in], outputs=[txt, chatbot])
|
| 468 |
txt.submit(handle_send, inputs=[txt, client_id_state, img_in], outputs=[txt, chatbot])
|
| 469 |
|
|
|
|
| 470 |
def clear_chat():
|
| 471 |
client_id_state.value = str(uuid.uuid4())
|
| 472 |
return [], ""
|
| 473 |
-
clear_btn.click(
|
| 474 |
-
|
| 475 |
-
# initialize heavy models in background to avoid blocking Gradio start
|
| 476 |
-
def bg_init():
|
| 477 |
-
init_visual_model()
|
| 478 |
-
init_math_model()
|
| 479 |
-
threading.Thread(target=bg_init, daemon=True).start()
|
| 480 |
-
|
| 481 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
| 8 |
- Caching of processed images & embeddings
|
| 9 |
- Simple in-process queue & streaming text output
|
| 10 |
- Rate-limiting per-client (token-bucket)
|
| 11 |
+
h
|
| 12 |
NOTES:
|
| 13 |
- Replace model IDs with ones that match your hardware/quotas.
|
| 14 |
- For production, swap the in-process queue with Redis/Celery and use S3/MinIO for storage.
|
|
|
|
| 423 |
# final append
|
| 424 |
session["history"].append(("Assistant", partial))
|
| 425 |
|
| 426 |
+
# ---------------------------
|
| 427 |
+
# GRADIO UI
|
| 428 |
+
# ---------------------------
|
| 429 |
# ---------------------------
|
| 430 |
# GRADIO UI
|
| 431 |
# ---------------------------
|
|
|
|
| 438 |
|
| 439 |
with gr.Row():
|
| 440 |
with gr.Column(scale=8, elem_classes="chat-column"):
|
| 441 |
+
chatbot = gr.Chatbot(
|
| 442 |
+
label="Assistant",
|
| 443 |
+
elem_id="chatbot",
|
| 444 |
+
show_label=False,
|
| 445 |
+
type="messages",
|
| 446 |
+
height=600
|
| 447 |
+
)
|
| 448 |
with gr.Row():
|
| 449 |
+
txt = gr.Textbox(
|
| 450 |
+
label="Type a message...",
|
| 451 |
+
placeholder="Ask a question or upload images",
|
| 452 |
+
show_label=False
|
| 453 |
+
)
|
| 454 |
submit = gr.Button("Send")
|
| 455 |
with gr.Row():
|
| 456 |
+
img_in = gr.File(
|
| 457 |
+
label="Upload images (multiple)",
|
| 458 |
+
file_count="multiple",
|
| 459 |
+
file_types=["image"]
|
| 460 |
+
)
|
| 461 |
clear_btn = gr.Button("New Chat")
|
| 462 |
+
client_id_state = gr.State(str(uuid.uuid4())) # simple per-window client id
|
| 463 |
|
| 464 |
+
# ---------------------------
|
| 465 |
+
# handle send
|
| 466 |
+
# ---------------------------
|
| 467 |
def handle_send(message, client_state, files):
|
| 468 |
client_id = client_state or str(uuid.uuid4())
|
|
|
|
|
|
|
| 469 |
gen = process_request(client_id, files, message, stream=True)
|
|
|
|
|
|
|
|
|
|
| 470 |
collected = ""
|
| 471 |
+
for part in gen:
|
| 472 |
+
collected = part
|
| 473 |
+
# Return in new type="messages" format
|
| 474 |
+
yield "", [
|
| 475 |
+
{"role": "user", "content": message},
|
| 476 |
+
{"role": "assistant", "content": collected}
|
| 477 |
+
]
|
| 478 |
+
# Final guarantee
|
| 479 |
+
yield "", [
|
| 480 |
+
{"role": "user", "content": message},
|
| 481 |
+
{"role": "assistant", "content": collected}
|
| 482 |
+
]
|
| 483 |
|
| 484 |
# Connect send button and textbox
|
| 485 |
submit.click(handle_send, inputs=[txt, client_id_state, img_in], outputs=[txt, chatbot])
|
| 486 |
txt.submit(handle_send, inputs=[txt, client_id_state, img_in], outputs=[txt, chatbot])
|
| 487 |
|
| 488 |
+
# Clear chat button
|
| 489 |
def clear_chat():
|
| 490 |
client_id_state.value = str(uuid.uuid4())
|
| 491 |
return [], ""
|
| 492 |
+
clear_btn.click(clear_chat, None, [chatbot, txt])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|