File size: 20,097 Bytes
898adde 24d4087 898adde 24d4087 c431cd0 898adde 24d4087 898adde 24d4087 898adde 24d4087 898adde 24d4087 898adde 24d4087 898adde 24d4087 898adde 24d4087 898adde 24d4087 898adde 98d2081 898adde 24d4087 898adde 24d4087 898adde 4b60f19 24d4087 898adde 0442bde 66b4b5b 0442bde 66b4b5b 898adde fa091d8 04b0522 66b4b5b 04b0522 6267028 04b0522 0442bde 04b0522 0442bde b94db32 0442bde 04b0522 0442bde 04b0522 0442bde 04b0522 0442bde 04b0522 0442bde 04b0522 b94db32 04b0522 0442bde 04b0522 0442bde 04b0522 0442bde 04b0522 b94db32 04b0522 0442bde 04b0522 0442bde b94db32 04b0522 0442bde 04b0522 b94db32 04b0522 6e9bf39 24d4087 898adde 24d4087 898adde 6e9bf39 24d4087 898adde 24d4087 898adde 6e9bf39 898adde 6e9bf39 898adde ef3f83c 898adde 24d4087 898adde ef3f83c 898adde | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 | import html
import gradio as gr
from agent import ShopSmartAgent
agent = ShopSmartAgent()
CUSTOM_CSS = """
body, .gradio-container {
background: #f8fafc;
color: #111827;
}
.page-wrap {
max-width: 1200px;
margin: 0 auto;
}
.hero {
margin-bottom: 14px;
}
.hero-title {
font-size: 2.2rem;
font-weight: 800;
color: #111827;
margin-bottom: 2px;
}
.hero-subtitle {
color: #6b7280;
font-size: 1.02rem;
}
.how-it-works {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 12px;
margin: 14px 0 18px;
}
.how-card {
background: white;
border: 1px solid #e5e7eb;
border-radius: 14px;
padding: 14px;
box-shadow: 0 4px 12px rgba(0,0,0,0.04);
}
.how-card-title {
font-weight: 700;
color: #111827;
margin-bottom: 6px;
}
.how-card-text {
color: #4b5563;
font-size: 0.94rem;
line-height: 1.45;
}
.filters-wrap {
margin: 6px 0 10px;
}
.results-wrap {
margin-top: 12px;
}
.featured-card, .product-card {
border: 1px solid #e5e7eb;
border-radius: 16px;
padding: 16px;
background: white;
box-shadow: 0 4px 14px rgba(0,0,0,0.06);
}
.featured-card {
border: 3px solid gold;
background: linear-gradient(135deg, #fff9e6 0%, #ffffff 100%);
margin-bottom: 16px;
}
.cards-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
gap: 14px;
}
.product-card:hover, .featured-card:hover {
transform: translateY(-2px);
transition: all 0.18s ease;
box-shadow: 0 8px 22px rgba(0,0,0,0.08);
}
.product-header {
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 10px;
}
.product-image, .emoji-fallback {
width: 72px;
height: 72px;
border-radius: 10px;
object-fit: cover;
background: #f3f4f6;
}
.emoji-fallback {
display: flex;
align-items: center;
justify-content: center;
font-size: 28px;
}
.product-name {
font-size: 1.08rem;
font-weight: 700;
color: #111827;
line-height: 1.25;
}
.product-meta {
color: #4b5563;
font-size: 0.95rem;
margin-top: 2px;
}
.product-seller {
color: #6b7280;
font-size: 0.9rem;
margin-top: 4px;
}
.product-reason {
color: #374151;
margin: 10px 0;
min-height: 20px;
}
.badges-row {
display: flex;
gap: 8px;
flex-wrap: wrap;
margin-bottom: 10px;
}
.badge {
display: inline-block;
padding: 5px 10px;
border-radius: 999px;
font-size: 0.78rem;
font-weight: 700;
white-space: nowrap;
}
.badge-blue {
background: #dbeafe;
color: #1d4ed8;
}
.badge-purple {
background: #ede9fe;
color: #6d28d9;
}
.badge-green {
background: #dcfce7;
color: #166534;
}
.badge-amber {
background: #fef3c7;
color: #92400e;
}
.risk-pill {
display: inline-block;
padding: 5px 10px;
border-radius: 999px;
font-size: 0.82rem;
font-weight: 600;
margin-bottom: 10px;
background: #e8f5e8;
color: #2d5f2d;
}
.risk-pill.medium {
background: #fff3cd;
color: #856404;
}
.risk-pill.high {
background: #f8d7da;
color: #721c24;
}
.product-link {
display: inline-block;
margin-top: 4px;
text-decoration: none;
background: #4f46e5;
color: white !important;
padding: 10px 14px;
border-radius: 10px;
font-weight: 600;
}
.summary-box {
border: 1px solid #dbeafe;
background: #eff6ff;
color: #1e3a8a;
padding: 10px 12px;
border-radius: 12px;
margin: 10px 0 14px;
}
.empty-state {
border: 1px dashed #d1d5db;
border-radius: 14px;
padding: 24px;
text-align: center;
color: #6b7280;
background: #fafafa;
}
.portfolio-note {
margin-top: 18px;
background: white;
border: 1px solid #e5e7eb;
border-radius: 14px;
padding: 14px;
color: #374151;
line-height: 1.5;
font-size: 0.95rem;
}
.portfolio-note b,
.portfolio-note strong {
color: #111827;
}
/* --- Form field visibility fixes --- */
.gradio-container .gr-block,
.gradio-container .gr-box,
.gradio-container .gr-group,
.gradio-container .gr-panel {
background: transparent;
}
.gradio-container textarea,
.gradio-container input,
.gradio-container select {
color: #111827 !important;
background: #ffffff !important;
border: 1px solid #cbd5e1 !important;
border-radius: 12px !important;
box-shadow: none !important;
}
.gradio-container input::placeholder,
.gradio-container textarea::placeholder {
color: #6b7280 !important;
opacity: 1 !important;
}
.gradio-container .gradio-textbox,
.gradio-container .gradio-number,
.gradio-container .gradio-dropdown,
.gradio-container .gradio-slider {
background: transparent !important;
}
.gradio-container .gradio-textbox label,
.gradio-container .gradio-number label,
.gradio-container .gradio-dropdown label,
.gradio-container .gradio-slider label,
.gradio-container label {
color: #111827 !important;
font-weight: 600;
}
.gradio-container [class*="input"],
.gradio-container [class*="textarea"],
.gradio-container [class*="select"],
.gradio-container [class*="wrap"] {
background-color: transparent;
}
.gradio-container button {
border-radius: 12px !important;
}
/* ============================================
MOBILE + TABLET LAYOUT (max-width: 1024px)
============================================ */
@media (max-width: 1024px) {
.gradio-container {
padding: 10px !important;
}
.hero-title {
font-size: 1.8rem;
}
.hero-subtitle {
font-size: 0.95rem;
}
.how-it-works {
grid-template-columns: 1fr;
gap: 10px;
}
.how-card {
padding: 12px;
}
.cards-grid {
grid-template-columns: 1fr;
gap: 12px;
}
.featured-card, .product-card {
padding: 14px;
}
.product-header {
align-items: flex-start;
}
.product-image, .emoji-fallback {
width: 64px;
height: 64px;
flex-shrink: 0;
}
.product-name {
font-size: 1rem;
}
.product-meta, .product-seller, .product-reason {
font-size: 0.92rem;
}
.product-link {
width: 100%;
text-align: center;
box-sizing: border-box;
}
}
/* ============================================
MOBILE + TABLET DARK MODE (max-width: 1024px)
============================================ */
@media (max-width: 1024px) {
body, .gradio-container {
background: #0f172a !important;
color: #f8fafc !important;
}
.how-card,
.product-card,
.portfolio-note,
.summary-box {
background: #1e293b !important;
border: 1px solid #334155 !important;
}
/* Featured card gets gold treatment on mobile/tablet */
.featured-card {
background: linear-gradient(135deg, #2d2500 0%, #1e293b 100%) !important;
border: 3px solid gold !important;
}
.hero-title,
.product-name {
color: #f8fafc !important;
-webkit-text-fill-color: #f8fafc !important;
}
.hero-subtitle,
.how-card-text,
.product-meta,
.product-seller,
.product-reason,
.portfolio-note {
color: #94a3b8 !important;
-webkit-text-fill-color: #94a3b8 !important;
}
.how-card-title {
color: #f1f5f9 !important;
-webkit-text-fill-color: #f1f5f9 !important;
}
.summary-box {
color: #93c5fd !important;
-webkit-text-fill-color: #93c5fd !important;
}
.portfolio-note b,
.portfolio-note strong {
color: #f1f5f9 !important;
-webkit-text-fill-color: #f1f5f9 !important;
}
/* Empty state fix */
.empty-state {
background: #1e293b !important;
border: 1px dashed #475569 !important;
color: #94a3b8 !important;
-webkit-text-fill-color: #94a3b8 !important;
}
.empty-state h3 {
color: #f1f5f9 !important;
-webkit-text-fill-color: #f1f5f9 !important;
}
.empty-state p,
.empty-state b {
color: #94a3b8 !important;
-webkit-text-fill-color: #94a3b8 !important;
}
/* Badge fixes */
.badge {
-webkit-text-fill-color: unset !important;
}
.badge-blue {
background: #1e40af !important;
color: #e0e7ff !important;
-webkit-text-fill-color: #e0e7ff !important;
}
.badge-purple {
background: #6d28d9 !important;
color: #ede9fe !important;
-webkit-text-fill-color: #ede9fe !important;
}
.badge-green {
background: #166534 !important;
color: #dcfce7 !important;
-webkit-text-fill-color: #dcfce7 !important;
}
.badge-amber {
background: #92400e !important;
color: #fef3c7 !important;
-webkit-text-fill-color: #fef3c7 !important;
}
/* Risk pill fixes */
.risk-pill {
background: #14532d !important;
color: #dcfce7 !important;
-webkit-text-fill-color: #dcfce7 !important;
}
.risk-pill.medium {
background: #78350f !important;
color: #fde68a !important;
-webkit-text-fill-color: #fde68a !important;
}
.risk-pill.high {
background: #7f1d1d !important;
color: #fecaca !important;
-webkit-text-fill-color: #fecaca !important;
}
/* Input fixes */
.gradio-container input,
.gradio-container textarea,
.gradio-container select {
background: #0f172a !important;
color: #f8fafc !important;
-webkit-text-fill-color: #f8fafc !important;
border: 1px solid #475569 !important;
}
.gradio-container input::placeholder,
.gradio-container textarea::placeholder {
color: #64748b !important;
-webkit-text-fill-color: #64748b !important;
}
/* Example buttons β indigo */
.gradio-container button,
.product-link {
background: linear-gradient(135deg, #6366f1, #4f46e5) !important;
color: white !important;
-webkit-text-fill-color: white !important;
}
/* Search button β green so it stands out */
.gradio-container button.primary,
.gradio-container button[variant="primary"] {
background: linear-gradient(135deg, #059669, #047857) !important;
color: white !important;
-webkit-text-fill-color: white !important;
}
}
"""
def _emoji_for_name(name: str) -> str:
s = (name or "").lower()
if any(k in s for k in ["iphone", "phone", "pixel", "galaxy s", "oneplus", "smartphone"]):
return "π±"
if any(k in s for k in ["headphone", "earbud", "airpods", "bose", "sony wh"]):
return "π§"
if any(k in s for k in ["watch", "garmin", "fitbit"]):
return "β"
if any(k in s for k in ["laptop", "macbook", "notebook", "chromebook"]):
return "π»"
if any(k in s for k in ["tablet", "ipad"]):
return "π±"
if any(k in s for k in ["mask", "serum", "cleanser", "moisturizer", "tonic"]):
return "π§΄"
return "ποΈ"
def _safe(value, default=""):
return default if value is None else value
def _short_reason(product: dict) -> str:
reason = product.get("recommendation") or product.get("short_reason") or "Strong match for your search"
reason = str(reason).strip().split(".")[0].strip()
return html.escape(reason + ("." if reason and not reason.endswith(".") else ""))
def _risk_info(product: dict):
risk = product.get("risk", {}) or {}
level = str(risk.get("level", "Low")).strip().lower()
if level not in {"low", "medium", "high"}:
level = "low"
label = level.title()
return level, label
def _infer_category(name: str) -> str:
s = (name or "").lower()
if any(k in s for k in ["iphone", "phone", "pixel", "galaxy s", "oneplus", "smartphone"]):
return "Smartphones"
if any(k in s for k in ["headphone", "earbud", "airpods", "bose", "sony wh"]):
return "Audio"
if any(k in s for k in ["watch", "garmin", "fitbit"]):
return "Wearables"
if any(k in s for k in ["laptop", "macbook", "notebook", "chromebook"]):
return "Laptops"
if any(k in s for k in ["mask", "serum", "cleanser", "moisturizer", "tonic", "gel masque"]):
return "Beauty"
return "All"
def _badge_html(text: str, style_class: str) -> str:
return f'<span class="badge {style_class}">{html.escape(text)}</span>'
def _extra_badges(product: dict, featured: bool = False) -> str:
badges = []
price = product.get("price")
rating = product.get("rating", product.get("product_rating", 0))
reviews_count = product.get("reviews_count", 0)
if featured:
badges.append(_badge_html("Best Overall", "badge-blue"))
if isinstance(rating, (int, float)) and rating >= 4.6:
badges.append(_badge_html("Top Rated", "badge-purple"))
if isinstance(price, (int, float)) and price < 300:
badges.append(_badge_html("Best Budget", "badge-green"))
if isinstance(reviews_count, int) and reviews_count >= 1000:
badges.append(_badge_html("Popular Pick", "badge-amber"))
if not badges:
return ""
return f'<div class="badges-row">{" ".join(badges)}</div>'
def _filter_products(products: list, category: str, min_rating: float):
filtered = []
for product in products:
product_category = _infer_category(product.get("name", ""))
rating = product.get("rating", product.get("product_rating", 0)) or 0
if category and category != "All" and product_category != category:
continue
if isinstance(rating, (int, float)) and rating < min_rating:
continue
filtered.append(product)
return filtered
def build_card(product: dict, featured: bool = False) -> str:
name = html.escape(str(_safe(product.get("name"), "Unknown Product")))
price = _safe(product.get("price"), "N/A")
if isinstance(price, (int, float)):
price = f"{price:,.2f}"
rating = _safe(product.get("rating", product.get("product_rating")), "N/A")
if isinstance(rating, (int, float)):
rating = f"{rating:.1f}"
seller = html.escape(str(_safe(product.get("seller", "Unknown Seller"))))
url = html.escape(str(_safe(product.get("url"), "#")))
image_url = str(_safe(product.get("image_url"), "")).strip()
emoji = _emoji_for_name(name)
risk_class, risk_label = _risk_info(product)
reason = _short_reason(product)
badges = _extra_badges(product, featured=featured)
image_html = (
f'<img src="{html.escape(image_url)}" class="product-image" alt="{name}">'
if image_url
else f'<div class="emoji-fallback">{emoji}</div>'
)
card_class = "featured-card" if featured else "product-card"
return f"""
<div class="{card_class}">
<div class="product-header">
{image_html}
<div>
<div class="product-name">{name}</div>
<div class="product-meta">${price} β’ β {rating}</div>
<div class="product-seller">Seller: {seller}</div>
</div>
</div>
{badges}
<div class="product-reason">{reason}</div>
<div class="risk-pill {risk_class}">Risk: {risk_label}</div><br/>
<a href="{url}" class="product-link" target="_blank">View Product</a>
</div>
"""
def build_results(results: dict, category: str, min_rating: float) -> str:
products = results.get("products", []) or []
products = _filter_products(products, category, min_rating)
total_found = len(products)
if not products:
return """
<div class="empty-state">
<h3>No products match your filters</h3>
<p>Try lowering the minimum rating or switching the category filter to <b>All</b>.</p>
</div>
"""
best_products = [p for p in products if p.get("is_best", False)]
featured = best_products[0] if best_products else products[0]
others = [p for p in products if p is not featured][:4]
html_out = [
'<div class="results-wrap">',
f'<div class="summary-box">Found {total_found} products. We filtered out irrelevant options and highlighted the safest, best-value picks.</div>',
build_card(featured, featured=True)
]
if others:
html_out.append('<div class="cards-grid">')
html_out.extend(build_card(p, featured=False) for p in others)
html_out.append('</div>')
html_out.append("""
<div class="portfolio-note">
<b>How this project works:</b> ShopSmart searches live product results across multiple sellers, ranks listings using
price, ratings, review volume, and risk signals, and highlights the best overall option in a clean, card-based interface.
<br/><br/>
<b>Stack:</b> Python, Gradio, SerpApi Google Shopping, and LLM-assisted recommendation logic.
</div>
""")
html_out.append('</div>')
return ''.join(html_out)
def process_request(query: str, max_price: float, category: str, min_rating: float):
if not query or not query.strip():
return """
<div class="empty-state">
<h3>Enter a search</h3>
<p>Try <b>smartphone under 800</b>, <b>headphones noise cancelling</b>, or <b>best cucumber face mask</b>.</p>
</div>
"""
try:
results = agent.process_query(query.strip(), max_price)
for product in results.get("products", []):
if not product.get("recommendation") and hasattr(agent, "generate_recommendation"):
try:
product["recommendation"] = agent.generate_recommendation(
product,
product.get("is_best", False)
)
except Exception:
pass
return build_results(results, category, min_rating)
except Exception as e:
return f'<div class="empty-state"><h3>Something went wrong</h3><p>{html.escape(str(e))}</p></div>'
with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft()) as demo:
gr.HTML("""
<div class="page-wrap">
<div class="hero">
<div class="hero-title">π ShopSmart</div>
<div class="hero-subtitle">Smarter, safer purchase decisions</div>
</div>
<div class="how-it-works">
<div class="how-card">
<div class="how-card-title">1. Search live products</div>
<div class="how-card-text">Pulls live shopping results across multiple sellers using Google Shopping data.</div>
</div>
<div class="how-card">
<div class="how-card-title">2. Rank for value + relevance</div>
<div class="how-card-text">Scores products using price, ratings, reviews, and query relevance.</div>
</div>
<div class="how-card">
<div class="how-card-title">3. Flag risk + recommend</div>
<div class="how-card-text">Highlights the safest, best-value pick and surfaces risk signals clearly.</div>
</div>
</div>
</div>
""")
with gr.Row():
with gr.Column(scale=3):
query_box = gr.Textbox(
label="What are you shopping for?",
placeholder="e.g. smartphone under $800, gaming laptop, wireless headphones",
lines=2,
)
with gr.Column(scale=1):
price_box = gr.Number(label="Max Price ($)", value=1000, minimum=0)
with gr.Row(elem_classes="filters-wrap"):
category_box = gr.Dropdown(
label="Category",
choices=["All", "Smartphones", "Audio", "Wearables", "Laptops", "Beauty"],
value="All"
)
rating_box = gr.Slider(
label="Minimum Rating",
minimum=0,
maximum=5,
step=0.1,
value=0
)
with gr.Row():
search_btn = gr.Button("π Search Products", variant="primary")
ex1 = gr.Button("Smartphone under 800")
ex2 = gr.Button("Best power drill")
ex3 = gr.Button("Best cucumber face mask")
results_html = gr.HTML()
search_btn.click(
fn=process_request,
inputs=[query_box, price_box, category_box, rating_box],
outputs=results_html
)
query_box.submit(
fn=process_request,
inputs=[query_box, price_box, category_box, rating_box],
outputs=results_html
)
ex1.click(lambda: ["smartphone under 800", 800, "Smartphones", 4.0], outputs=[query_box, price_box, category_box, rating_box])
ex2.click(lambda: ["best power drill", 200, "All", 4.0], outputs=[query_box, price_box, category_box, rating_box])
ex3.click(lambda: ["best cucumber face mask", 100, "Beauty", 4.0], outputs=[query_box, price_box, category_box, rating_box])
if __name__ == "__main__":
demo.launch() |