Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
-
import re
|
| 2 |
-
import time
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import pandas as pd
|
| 5 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
API_BASE_URL = "https://sale-agent-m179.onrender.com"
|
| 8 |
|
|
@@ -20,9 +21,8 @@ def clear_chat():
|
|
| 20 |
return [], {}, {}, [], ""
|
| 21 |
|
| 22 |
|
| 23 |
-
def respond_to_chat(message, history, state, selected_model):
|
| 24 |
"""Handle chat responses with image support"""
|
| 25 |
-
# print(f"🔄 === DEBUG STATE ===\n Chat request with model: {selected_model}")
|
| 26 |
start = time.perf_counter()
|
| 27 |
# Handle multimodal input - message is a dict with 'text' and 'files' keys
|
| 28 |
if isinstance(message, dict):
|
|
@@ -45,11 +45,11 @@ def respond_to_chat(message, history, state, selected_model):
|
|
| 45 |
if image_path:
|
| 46 |
with open(image_path, 'rb') as f:
|
| 47 |
files = {"image": f}
|
| 48 |
-
data = {"message": user_query}
|
| 49 |
resp = requests.post(
|
| 50 |
f"{API_BASE_URL}/chat_with_image", files=files, data=data, timeout=120)
|
| 51 |
else:
|
| 52 |
-
payload = {"message": user_query}
|
| 53 |
resp = requests.post(f"{API_BASE_URL}/chat",
|
| 54 |
json=payload, timeout=120)
|
| 55 |
|
|
@@ -91,7 +91,8 @@ def show_specs(state, history=None):
|
|
| 91 |
df = pd.DataFrame({}, columns=columns)
|
| 92 |
markdown_table = df.to_markdown(index=False)
|
| 93 |
# Fix: Use proper ChatInterface message format
|
| 94 |
-
history.append(
|
|
|
|
| 95 |
return history
|
| 96 |
|
| 97 |
# print(specs_map)
|
|
@@ -187,7 +188,8 @@ def show_specs(state, history=None):
|
|
| 187 |
|
| 188 |
markdown_table = df.to_markdown(index=False)
|
| 189 |
# Fix: Use proper ChatInterface message format
|
| 190 |
-
history.append(
|
|
|
|
| 191 |
return history
|
| 192 |
|
| 193 |
|
|
@@ -202,7 +204,8 @@ def show_advantages(state, history=None):
|
|
| 202 |
df = pd.DataFrame([["Không có ưu điểm", ""]], columns=columns)
|
| 203 |
markdown_table = df.to_markdown(index=False)
|
| 204 |
# Fix: Use proper ChatInterface message format
|
| 205 |
-
history.append(
|
|
|
|
| 206 |
return history
|
| 207 |
|
| 208 |
for prod_id, data in specs_map.items():
|
|
@@ -226,7 +229,8 @@ def show_advantages(state, history=None):
|
|
| 226 |
|
| 227 |
markdown_table = df.to_markdown(index=False)
|
| 228 |
# Fix: Use proper ChatInterface message format
|
| 229 |
-
history.append(
|
|
|
|
| 230 |
return history
|
| 231 |
|
| 232 |
|
|
@@ -238,7 +242,8 @@ def show_solution_packages(state, history=None):
|
|
| 238 |
|
| 239 |
if not specs_map:
|
| 240 |
# Fix: Use proper ChatInterface message format
|
| 241 |
-
history.append(
|
|
|
|
| 242 |
return history
|
| 243 |
|
| 244 |
# Filter only solution packages
|
|
@@ -249,7 +254,8 @@ def show_solution_packages(state, history=None):
|
|
| 249 |
|
| 250 |
if not solution_packages:
|
| 251 |
# Fix: Use proper ChatInterface message format
|
| 252 |
-
history.append(
|
|
|
|
| 253 |
return history
|
| 254 |
|
| 255 |
# Build markdown content for each package
|
|
@@ -322,7 +328,7 @@ css = """
|
|
| 322 |
.gradio-container {
|
| 323 |
position: relative;
|
| 324 |
}
|
| 325 |
-
.model-dropdown {
|
| 326 |
background: linear-gradient(135deg, #667eea, #764ba2) !important;
|
| 327 |
color: white !important;
|
| 328 |
border: none !important;
|
|
@@ -333,15 +339,15 @@ css = """
|
|
| 333 |
min-height: 32px !important;
|
| 334 |
box-shadow: 0 2px 4px rgba(0,0,0,0.15) !important;
|
| 335 |
}
|
| 336 |
-
.model-dropdown select {
|
| 337 |
color: white !important;
|
| 338 |
background: transparent !important;
|
| 339 |
cursor: pointer !important;
|
| 340 |
}
|
| 341 |
-
.model-dropdown select:focus {
|
| 342 |
outline: none;
|
| 343 |
}
|
| 344 |
-
.model-dropdown select:not(:focus) {
|
| 345 |
pointer-events: auto;
|
| 346 |
}
|
| 347 |
"""
|
|
@@ -367,6 +373,17 @@ with gr.Blocks(fill_height=True, css=css) as demo:
|
|
| 367 |
container=False
|
| 368 |
)
|
| 369 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
with gr.Column(scale=1, min_width=30):
|
| 371 |
clear_btn = gr.Button(
|
| 372 |
"✨ Xoá", variant="secondary", size="sm", elem_classes="custom-clear-btn")
|
|
@@ -384,14 +401,13 @@ with gr.Blocks(fill_height=True, css=css) as demo:
|
|
| 384 |
fn=respond_to_chat,
|
| 385 |
multimodal=True,
|
| 386 |
type="messages",
|
| 387 |
-
cache_examples=False,
|
| 388 |
examples=[
|
| 389 |
["Tìm sản phẩm bình giữ nhiệt dung tích dưới 2 lít."],
|
| 390 |
["Tìm sản phẩm ổ cắm thông minh"],
|
| 391 |
["Tư vấn cho tôi đèn học chống cận cho con gái của tôi học lớp 6"]
|
| 392 |
],
|
| 393 |
title="_",
|
| 394 |
-
additional_inputs=[state, model_dropdown],
|
| 395 |
chatbot=chatbot
|
| 396 |
)
|
| 397 |
|
|
@@ -407,12 +423,12 @@ with gr.Blocks(fill_height=True, css=css) as demo:
|
|
| 407 |
btn_spec.click(fn=show_specs, inputs=[state, chatbot], outputs=[chatbot])
|
| 408 |
btn_adv.click(fn=show_advantages,
|
| 409 |
inputs=[state, chatbot],
|
| 410 |
-
outputs=[chatbot]
|
| 411 |
-
)
|
| 412 |
|
| 413 |
# Gói sản phẩm button shows solution packages in structured format
|
| 414 |
-
btn_pckg.click(fn=show_solution_packages,
|
| 415 |
-
state, chatbot],
|
|
|
|
| 416 |
|
| 417 |
# Model change handler -> call API
|
| 418 |
def _on_model_change(model_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import time
|
| 5 |
+
import re
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
API_BASE_URL = "https://sale-agent-m179.onrender.com"
|
| 9 |
|
|
|
|
| 21 |
return [], {}, {}, [], ""
|
| 22 |
|
| 23 |
|
| 24 |
+
def respond_to_chat(message, history, state, selected_model, debug_mode):
|
| 25 |
"""Handle chat responses with image support"""
|
|
|
|
| 26 |
start = time.perf_counter()
|
| 27 |
# Handle multimodal input - message is a dict with 'text' and 'files' keys
|
| 28 |
if isinstance(message, dict):
|
|
|
|
| 45 |
if image_path:
|
| 46 |
with open(image_path, 'rb') as f:
|
| 47 |
files = {"image": f}
|
| 48 |
+
data = {"message": user_query, "debug": debug_mode}
|
| 49 |
resp = requests.post(
|
| 50 |
f"{API_BASE_URL}/chat_with_image", files=files, data=data, timeout=120)
|
| 51 |
else:
|
| 52 |
+
payload = {"message": user_query, "debug": debug_mode}
|
| 53 |
resp = requests.post(f"{API_BASE_URL}/chat",
|
| 54 |
json=payload, timeout=120)
|
| 55 |
|
|
|
|
| 91 |
df = pd.DataFrame({}, columns=columns)
|
| 92 |
markdown_table = df.to_markdown(index=False)
|
| 93 |
# Fix: Use proper ChatInterface message format
|
| 94 |
+
history.append(
|
| 95 |
+
{"role": "assistant", "content": f"📄 Thông số kỹ thuật\n{markdown_table}"})
|
| 96 |
return history
|
| 97 |
|
| 98 |
# print(specs_map)
|
|
|
|
| 188 |
|
| 189 |
markdown_table = df.to_markdown(index=False)
|
| 190 |
# Fix: Use proper ChatInterface message format
|
| 191 |
+
history.append(
|
| 192 |
+
{"role": "assistant", "content": f"📄 Thông số kỹ thuật\n{markdown_table}"})
|
| 193 |
return history
|
| 194 |
|
| 195 |
|
|
|
|
| 204 |
df = pd.DataFrame([["Không có ưu điểm", ""]], columns=columns)
|
| 205 |
markdown_table = df.to_markdown(index=False)
|
| 206 |
# Fix: Use proper ChatInterface message format
|
| 207 |
+
history.append(
|
| 208 |
+
{"role": "assistant", "content": f"💡 Ưu điểm nổi trội\n{markdown_table}"})
|
| 209 |
return history
|
| 210 |
|
| 211 |
for prod_id, data in specs_map.items():
|
|
|
|
| 229 |
|
| 230 |
markdown_table = df.to_markdown(index=False)
|
| 231 |
# Fix: Use proper ChatInterface message format
|
| 232 |
+
history.append(
|
| 233 |
+
{"role": "assistant", "content": f"💡 Ưu điểm nổi trội\n{markdown_table}"})
|
| 234 |
return history
|
| 235 |
|
| 236 |
|
|
|
|
| 242 |
|
| 243 |
if not specs_map:
|
| 244 |
# Fix: Use proper ChatInterface message format
|
| 245 |
+
history.append(
|
| 246 |
+
{"role": "assistant", "content": "📦 Không có gói sản phẩm nào"})
|
| 247 |
return history
|
| 248 |
|
| 249 |
# Filter only solution packages
|
|
|
|
| 254 |
|
| 255 |
if not solution_packages:
|
| 256 |
# Fix: Use proper ChatInterface message format
|
| 257 |
+
history.append(
|
| 258 |
+
{"role": "assistant", "content": "📦 Không có gói sản phẩm nào"})
|
| 259 |
return history
|
| 260 |
|
| 261 |
# Build markdown content for each package
|
|
|
|
| 328 |
.gradio-container {
|
| 329 |
position: relative;
|
| 330 |
}
|
| 331 |
+
.model-dropdown, .debug-dropdown {
|
| 332 |
background: linear-gradient(135deg, #667eea, #764ba2) !important;
|
| 333 |
color: white !important;
|
| 334 |
border: none !important;
|
|
|
|
| 339 |
min-height: 32px !important;
|
| 340 |
box-shadow: 0 2px 4px rgba(0,0,0,0.15) !important;
|
| 341 |
}
|
| 342 |
+
.model-dropdown select, .debug-dropdown select {
|
| 343 |
color: white !important;
|
| 344 |
background: transparent !important;
|
| 345 |
cursor: pointer !important;
|
| 346 |
}
|
| 347 |
+
.model-dropdown select:focus, .debug-dropdown select:focus {
|
| 348 |
outline: none;
|
| 349 |
}
|
| 350 |
+
.model-dropdown select:not(:focus), .debug-dropdown select:not(:focus) {
|
| 351 |
pointer-events: auto;
|
| 352 |
}
|
| 353 |
"""
|
|
|
|
| 373 |
container=False
|
| 374 |
)
|
| 375 |
|
| 376 |
+
with gr.Column(scale=1, min_width=120):
|
| 377 |
+
debug_dropdown = gr.Dropdown(
|
| 378 |
+
choices=["Normal", "Debug"],
|
| 379 |
+
value="Normal",
|
| 380 |
+
label="Mode",
|
| 381 |
+
show_label=False,
|
| 382 |
+
elem_classes="debug-dropdown",
|
| 383 |
+
interactive=True,
|
| 384 |
+
container=False
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
with gr.Column(scale=1, min_width=30):
|
| 388 |
clear_btn = gr.Button(
|
| 389 |
"✨ Xoá", variant="secondary", size="sm", elem_classes="custom-clear-btn")
|
|
|
|
| 401 |
fn=respond_to_chat,
|
| 402 |
multimodal=True,
|
| 403 |
type="messages",
|
|
|
|
| 404 |
examples=[
|
| 405 |
["Tìm sản phẩm bình giữ nhiệt dung tích dưới 2 lít."],
|
| 406 |
["Tìm sản phẩm ổ cắm thông minh"],
|
| 407 |
["Tư vấn cho tôi đèn học chống cận cho con gái của tôi học lớp 6"]
|
| 408 |
],
|
| 409 |
title="_",
|
| 410 |
+
additional_inputs=[state, model_dropdown, debug_dropdown],
|
| 411 |
chatbot=chatbot
|
| 412 |
)
|
| 413 |
|
|
|
|
| 423 |
btn_spec.click(fn=show_specs, inputs=[state, chatbot], outputs=[chatbot])
|
| 424 |
btn_adv.click(fn=show_advantages,
|
| 425 |
inputs=[state, chatbot],
|
| 426 |
+
outputs=[chatbot])
|
|
|
|
| 427 |
|
| 428 |
# Gói sản phẩm button shows solution packages in structured format
|
| 429 |
+
btn_pckg.click(fn=show_solution_packages,
|
| 430 |
+
inputs=[state, chatbot],
|
| 431 |
+
outputs=[chatbot])
|
| 432 |
|
| 433 |
# Model change handler -> call API
|
| 434 |
def _on_model_change(model_name):
|