Update app.py
Browse filesAdd review board demo view
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import json
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
|
@@ -279,12 +280,92 @@ def build_summary(data):
|
|
| 279 |
followups = sum(1 for order in orders if missing_list(order))
|
| 280 |
ready = total - followups
|
| 281 |
|
| 282 |
-
return
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
def split_customer_messages(messages):
|
| 290 |
entries = []
|
|
@@ -370,7 +451,7 @@ def extract_single_order(customer, message):
|
|
| 370 |
|
| 371 |
def analyze_messages(messages):
|
| 372 |
if not messages.strip():
|
| 373 |
-
return pd.DataFrame(columns=ORDER_COLUMNS), "
|
| 374 |
|
| 375 |
entries = split_customer_messages(messages)
|
| 376 |
orders_data = [extract_single_order(customer, message) for customer, message in entries]
|
|
@@ -385,10 +466,11 @@ def analyze_messages(messages):
|
|
| 385 |
data["reply_drafts"] = auto_replies
|
| 386 |
|
| 387 |
summary = build_summary(data)
|
|
|
|
| 388 |
prep = format_list("Prep list", auto_prep)
|
| 389 |
replies = format_replies(auto_replies)
|
| 390 |
raw = json.dumps(data, indent=2, ensure_ascii=False)
|
| 391 |
-
return orders_df, summary, prep, replies, raw
|
| 392 |
|
| 393 |
with gr.Blocks(title="DM Order Desk", theme=gr.themes.Soft()) as demo:
|
| 394 |
gr.Markdown(
|
|
@@ -420,10 +502,12 @@ with gr.Blocks(title="DM Order Desk", theme=gr.themes.Soft()) as demo:
|
|
| 420 |
farm_btn = gr.Button("Farmers Market")
|
| 421 |
|
| 422 |
with gr.Column(scale=2):
|
| 423 |
-
summary = gr.
|
| 424 |
|
| 425 |
with gr.Tabs():
|
| 426 |
-
with gr.Tab("
|
|
|
|
|
|
|
| 427 |
orders = gr.Dataframe(label="Orders", headers=ORDER_COLUMNS)
|
| 428 |
with gr.Tab("Prep List"):
|
| 429 |
prep = gr.Markdown()
|
|
@@ -432,13 +516,11 @@ with gr.Blocks(title="DM Order Desk", theme=gr.themes.Soft()) as demo:
|
|
| 432 |
with gr.Tab("Raw JSON"):
|
| 433 |
raw = gr.Code(label="Raw JSON", language="json")
|
| 434 |
|
| 435 |
-
run.click(analyze_messages, inputs=messages, outputs=[orders, summary, prep, replies, raw])
|
| 436 |
|
| 437 |
home_btn.click(lambda: EXAMPLE_INPUT, outputs=messages)
|
| 438 |
food_btn.click(lambda: FOOD_TRUCK_EXAMPLE, outputs=messages)
|
| 439 |
craft_btn.click(lambda: CRAFT_SELLER_EXAMPLE, outputs=messages)
|
| 440 |
farm_btn.click(lambda: FARMERS_MARKET_EXAMPLE, outputs=messages)
|
| 441 |
|
| 442 |
-
demo.launch()
|
| 443 |
-
|
| 444 |
demo.launch()
|
|
|
|
| 1 |
import json
|
| 2 |
+
import html
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
|
|
|
| 280 |
followups = sum(1 for order in orders if missing_list(order))
|
| 281 |
ready = total - followups
|
| 282 |
|
| 283 |
+
return f"""
|
| 284 |
+
<div style="display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:12px;margin:8px 0 18px;">
|
| 285 |
+
<div style="background:#f8fafc;border:1px solid #e2e8f0;border-radius:10px;padding:14px;">
|
| 286 |
+
<div style="font-size:24px;font-weight:700;">{total}</div>
|
| 287 |
+
<div style="color:#475569;">customer messages</div>
|
| 288 |
+
</div>
|
| 289 |
+
<div style="background:#ecfdf5;border:1px solid #bbf7d0;border-radius:10px;padding:14px;">
|
| 290 |
+
<div style="font-size:24px;font-weight:700;">{ready}</div>
|
| 291 |
+
<div style="color:#166534;">ready orders</div>
|
| 292 |
+
</div>
|
| 293 |
+
<div style="background:#fffbeb;border:1px solid #fde68a;border-radius:10px;padding:14px;">
|
| 294 |
+
<div style="font-size:24px;font-weight:700;">{followups}</div>
|
| 295 |
+
<div style="color:#92400e;">follow-ups needed</div>
|
| 296 |
+
</div>
|
| 297 |
+
</div>
|
| 298 |
+
"""
|
| 299 |
+
|
| 300 |
+
def build_review_board(data):
|
| 301 |
+
ready_cards = []
|
| 302 |
+
followup_cards = []
|
| 303 |
+
|
| 304 |
+
for order in data.get("orders", []):
|
| 305 |
+
customer = html.escape(text_value(order.get("customer")) or "Customer")
|
| 306 |
+
item = html.escape(text_value(order.get("item")) or "order")
|
| 307 |
+
quantity = html.escape(text_value(order.get("quantity")) or "quantity to confirm")
|
| 308 |
+
flavor = html.escape(text_value(order.get("flavor")))
|
| 309 |
+
pickup_time = html.escape(text_value(order.get("pickup_time")))
|
| 310 |
+
delivery_address = html.escape(text_value(order.get("delivery_address")))
|
| 311 |
+
payment_status = html.escape(text_value(order.get("payment_status")))
|
| 312 |
+
fields = missing_list(order)
|
| 313 |
+
|
| 314 |
+
title = f"{quantity} {item}"
|
| 315 |
+
if flavor:
|
| 316 |
+
title += f" ({flavor})"
|
| 317 |
+
|
| 318 |
+
details = []
|
| 319 |
+
if pickup_time:
|
| 320 |
+
details.append(f"Time: {pickup_time}")
|
| 321 |
+
if delivery_address:
|
| 322 |
+
details.append(f"Place: {delivery_address}")
|
| 323 |
+
if payment_status:
|
| 324 |
+
details.append(f"Payment: {payment_status}")
|
| 325 |
+
|
| 326 |
+
detail_html = " · ".join(details) if details else "Details still need review."
|
| 327 |
+
|
| 328 |
+
if fields:
|
| 329 |
+
chips = "".join(
|
| 330 |
+
f'<span style="display:inline-block;background:#fef3c7;color:#92400e;border-radius:999px;padding:4px 9px;margin:3px;font-size:12px;">{html.escape(field.replace("_", " "))}</span>'
|
| 331 |
+
for field in fields
|
| 332 |
+
)
|
| 333 |
+
followup_cards.append(
|
| 334 |
+
f"""
|
| 335 |
+
<div style="background:#fff7ed;border:1px solid #fed7aa;border-radius:10px;padding:14px;margin-bottom:10px;">
|
| 336 |
+
<div style="font-weight:700;">{customer}</div>
|
| 337 |
+
<div>{title}</div>
|
| 338 |
+
<div style="color:#64748b;font-size:13px;margin-top:4px;">{detail_html}</div>
|
| 339 |
+
<div style="margin-top:8px;">{chips}</div>
|
| 340 |
+
</div>
|
| 341 |
+
"""
|
| 342 |
+
)
|
| 343 |
+
else:
|
| 344 |
+
ready_cards.append(
|
| 345 |
+
f"""
|
| 346 |
+
<div style="background:#f0fdf4;border:1px solid #bbf7d0;border-radius:10px;padding:14px;margin-bottom:10px;">
|
| 347 |
+
<div style="font-weight:700;">{customer}</div>
|
| 348 |
+
<div>{title}</div>
|
| 349 |
+
<div style="color:#64748b;font-size:13px;margin-top:4px;">{detail_html}</div>
|
| 350 |
+
</div>
|
| 351 |
+
"""
|
| 352 |
+
)
|
| 353 |
+
|
| 354 |
+
ready_html = "".join(ready_cards) or '<div style="color:#64748b;">No ready orders yet.</div>'
|
| 355 |
+
followup_html = "".join(followup_cards) or '<div style="color:#64748b;">No follow-ups needed.</div>'
|
| 356 |
+
|
| 357 |
+
return f"""
|
| 358 |
+
<div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
|
| 359 |
+
<section>
|
| 360 |
+
<h3>Ready to prep</h3>
|
| 361 |
+
{ready_html}
|
| 362 |
+
</section>
|
| 363 |
+
<section>
|
| 364 |
+
<h3>Needs follow-up</h3>
|
| 365 |
+
{followup_html}
|
| 366 |
+
</section>
|
| 367 |
+
</div>
|
| 368 |
+
"""
|
| 369 |
|
| 370 |
def split_customer_messages(messages):
|
| 371 |
entries = []
|
|
|
|
| 451 |
|
| 452 |
def analyze_messages(messages):
|
| 453 |
if not messages.strip():
|
| 454 |
+
return pd.DataFrame(columns=ORDER_COLUMNS), "Choose a scenario and organize the inbox.", "", "", "", ""
|
| 455 |
|
| 456 |
entries = split_customer_messages(messages)
|
| 457 |
orders_data = [extract_single_order(customer, message) for customer, message in entries]
|
|
|
|
| 466 |
data["reply_drafts"] = auto_replies
|
| 467 |
|
| 468 |
summary = build_summary(data)
|
| 469 |
+
review = build_review_board(data)
|
| 470 |
prep = format_list("Prep list", auto_prep)
|
| 471 |
replies = format_replies(auto_replies)
|
| 472 |
raw = json.dumps(data, indent=2, ensure_ascii=False)
|
| 473 |
+
return orders_df, summary, review, prep, replies, raw
|
| 474 |
|
| 475 |
with gr.Blocks(title="DM Order Desk", theme=gr.themes.Soft()) as demo:
|
| 476 |
gr.Markdown(
|
|
|
|
| 502 |
farm_btn = gr.Button("Farmers Market")
|
| 503 |
|
| 504 |
with gr.Column(scale=2):
|
| 505 |
+
summary = gr.HTML("Choose a scenario and organize the inbox.")
|
| 506 |
|
| 507 |
with gr.Tabs():
|
| 508 |
+
with gr.Tab("Review Board"):
|
| 509 |
+
review = gr.HTML()
|
| 510 |
+
with gr.Tab("Data Table"):
|
| 511 |
orders = gr.Dataframe(label="Orders", headers=ORDER_COLUMNS)
|
| 512 |
with gr.Tab("Prep List"):
|
| 513 |
prep = gr.Markdown()
|
|
|
|
| 516 |
with gr.Tab("Raw JSON"):
|
| 517 |
raw = gr.Code(label="Raw JSON", language="json")
|
| 518 |
|
| 519 |
+
run.click(analyze_messages, inputs=messages, outputs=[orders, summary, review, prep, replies, raw])
|
| 520 |
|
| 521 |
home_btn.click(lambda: EXAMPLE_INPUT, outputs=messages)
|
| 522 |
food_btn.click(lambda: FOOD_TRUCK_EXAMPLE, outputs=messages)
|
| 523 |
craft_btn.click(lambda: CRAFT_SELLER_EXAMPLE, outputs=messages)
|
| 524 |
farm_btn.click(lambda: FARMERS_MARKET_EXAMPLE, outputs=messages)
|
| 525 |
|
|
|
|
|
|
|
| 526 |
demo.launch()
|