Spaces:
Sleeping
Sleeping
Commit ·
71dd534
1
Parent(s): cddf904
Polish merchant spotlight and category inputs
Browse files321 unmodified lines32232332432532632769 unmodified lines397398399400401402403345 unmodified lines749750751752753754110 unmodified lines8658668678688698708711 unmodified line8738748758768778788795 unmodified lines88588688788888989089153 unmodified lines9459469479489499509511 unmodified line9539549559569579589596 unmodified lines96696796896997097197287 unmodified lines10601061106210631064106510661 unmodified line10681069107010711072107333 unmodified lines110711081109111011111112111310 unmodified lines112411251126112711281129113020 unmodified lines1151115211531154115511561157321 unmodified linesreturn sorted({tag for p in products for tag in p.get("tags", [])})def update_product_choices(category: str):names = _product_names_for_category(category)return gr.update(choices=names, value=names[0] if names else None)69 unmodified linesdef _predict_custom(review: str, features: str, categories: str, price: Any, rating: Any, count: Any) -> Dict[str, Any]:meta = {"features_text": features or "", "categories_text": categories or "", "price": _safe_float(price), "average_rating": _safe_float(rating), "rating_number": _safe_float(count)}return _predictor().predict(review or "No review text provided.", meta)345 unmodified linesdef negative_product_spotlight(limit: int = 6) -> str:items = []for product in PRODUCTS:try:110 unmodified linesdef download_new_product_template():rows = [["Cotton Polyester Blend, Slim Fit, Graphic Print, Machine Wash","Clothing > Women > Tops > T-Shirts",29.99,4.1,35,1 unmodified line]]return _write_csv_download("new_product_metadata_template.csv",["features", "categories", "price", "average_rating", "rating_number", "focus_aspect"],rows,)5 unmodified linesrows = []for i, data in enumerate(records, 1):features = data.get("features") or data.get("features_text") or ""categories = data.get("categories") or data.get("categories_text") or ""price = _safe_float(data.get("price"), 0.0)rating = _safe_float(data.get("average_rating") or data.get("rating"), 4.0)count = _safe_float(data.get("rating_number") or data.get("rating_count"), 0.0)53 unmodified linesrows = [["The fabric is soft and the color looks good, but it runs small.","Cotton Blend, Slim Fit, Zipper Closure","Clothing > Women > Jackets",39.99,4.2,312,1 unmodified line]]return _write_csv_download("external_review_template.csv",["review", "features", "categories", "price", "average_rating", "rating_number", "highlight_aspect"],rows,)6 unmodified linesfor i, data in enumerate(records, 1):review = data.get("review") or data.get("review_text") or ""features = data.get("features") or data.get("features_text") or ""categories = data.get("categories") or data.get("categories_text") or ""price = _safe_float(data.get("price"), 0.0)rating = _safe_float(data.get("average_rating") or data.get("rating"), 4.0)count = _safe_float(data.get("rating_number") or data.get("rating_count"), 0.0)87 unmodified lines.negative-grid { display:grid; grid-template-columns:repeat(6, minmax(0, 1fr)); gap:10px; margin:8px 0 14px; }.negative-card { display:grid; grid-template-columns:64px 1fr; gap:10px; border:1px solid #fecaca; border-left:4px solid #ef4444; border-radius:8px; padding:10px; background:#fffafa; }.negative-card p { margin:5px 0 0; color:#334155; font-size:13px; }.mini-product-img { width:64px; height:76px; object-fit:cover; border-radius:6px; border:1px solid var(--line); background:#f8fafc; }@media (max-width:1100px) { .negative-grid { grid-template-columns:repeat(3, minmax(0, 1fr)); } }@media (max-width:860px) { .aspect-grid, .metric-grid, .product-layout, .decision-layout, .negative-grid { grid-template-columns:1fr; } .product-img { width:100%; height:180px; } }"""1 unmodified linedef build_app() -> gr.Blocks:categories = ["All"] + sorted({p["category"] for p in PRODUCTS})tags = sorted({tag for p in PRODUCTS for tag in p.get("tags", [])})with gr.Blocks(css=CSS, title="Clothing Sentiment Analysis") as demo:gr.HTML('<div id="hero"><div class="eyebrow">BERT + Metadata Cross-Attention</div><h1>Clothing Review Sentiment Intelligence App</h1><p>Explore overall sentiment, six aspect-level opinions, metadata-driven risks, and updated 10W experiment reports in a compact customer decision-support prototype.</p><div class="hero-pills"><span>Consumer decision support</span><span>Merchant diagnostics</span><span>Research dashboard</span></div></div>')33 unmodified linesgr.HTML('<div class="module-head"><h3>Product Score Monitor</h3><span class="info-tip">i<span class="tip-content">' + model_info_html() + '</span></span></div>')gr.HTML('<div class="small-label">Most negative products across the six aspects</div>')negative_spotlight = gr.HTML('<div class="note-card">Loading most negative products...</div>')aspect_overview = gr.Dataframe(headers=["Aspect", "Negative Count", "Most Negative Product", "Confidence", "Reason"], value=merchant_aspect_overview(), datatype=["str", "number", "str", "number", "str"], interactive=False, label="Six-aspect negative overview")with gr.Accordion("Score filters and export", open=True):with gr.Row():merchant_metric = gr.Dropdown(["Overall"] + ASPECTS, value="Overall", label="Aspect")10 unmodified lineswith gr.Column(scale=1):with gr.Group(visible=True) as risk_single_group:new_features = gr.Textbox("Cotton Polyester Blend, Slim Fit, Graphic Print, Machine Wash", label="New product features", lines=4)new_categories = gr.Textbox("Clothing > Women > Tops > T-Shirts", label="New product categories")with gr.Row():new_price = gr.Number(29.99, label="Price")new_rating = gr.Number(4.1, label="Expected or early average rating")20 unmodified linesexternal_review = gr.Textbox("The fabric is soft and the color looks good, but it runs small and the zipper feels weak.", label="External customer review", lines=5)with gr.Row():ext_features = gr.Textbox("Cotton Blend, Slim Fit, Zipper Closure", label="Product features", lines=3)ext_categories = gr.Textbox("Clothing > Women > Jackets", label="Product categories", lines=3)with gr.Row():ext_price = gr.Number(39.99, label="Price")ext_rating = gr.Number(4.2, label="Average rating")321 unmodified lines32232332432532632732832933033133233333433533633769 unmodified lines407408409410411412413345 unmodified lines759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796110 unmodified lines9079089099109119129131 unmodified line9159169179189199209215 unmodified lines92792892993093193293353 unmodified lines9879889899909919929931 unmodified line995996997998999100010016 unmodified lines100810091010101110121013101487 unmodified lines110211031104110511061107110811091110111111121113111411151 unmodified line1117111811191120112111221123112433 unmodified lines11581159116011611162116310 unmodified lines117411751176117711781179118020 unmodified lines1201120212031204120512061207321 unmodified linesreturn sorted({tag for p in products for tag in p.get("tags", [])})def _category_to_metadata_text(category: str) -> str:category = str(category or "").strip()if not category:return "Clothing"for product in PRODUCTS:if product.get("category") == category and product.get("categories"):return product["categories"]return f"Clothing > {category}"def update_product_choices(category: str):names = _product_names_for_category(category)return gr.update(choices=names, value=names[0] if names else None)69 unmodified linesdef _predict_custom(review: str, features: str, categories: str, price: Any, rating: Any, count: Any) -> Dict[str, Any]:meta = {"features_text": features or "", "categories_text": _category_to_metadata_text(categories), "price": _safe_float(price), "average_rating": _safe_float(rating), "rating_number": _safe_float(count)}return _predictor().predict(review or "No review text provided.", meta)345 unmodified linesdef negative_product_spotlight(limit: int = 6) -> str:cards = []for aspect in ASPECTS:best = Nonefor product in PRODUCTS:try:result = _predict_product(product["name"])except Exception:continued = result.get("aspect_details", {}).get(aspect, {})if d.get("label") != "Negative":continueconf = _safe_float(d.get("confidence"))hits = _keyword_hits(product.get("review", ""), aspect)reason = ", ".join(hits[:3]) if hits else _short_text(product.get("review") or product.get("features"), 48)if best is None or conf > best[0]:best = (conf, product, reason)if best is None:cards.append(f'<div class="negative-card compact-negative"><h4>{aspect}</h4>''<div class="empty-negative">No strong negative sample</div></div>')continueconf, product, reason = bestimg = f'<img class="mini-product-img" src="{_esc(_product_image_url(product))}" alt="{_esc(product["name"])}">'cards.append(f'<div class="negative-card compact-negative"><h4>{aspect}</h4>'f'<div class="negative-body">{img}<div><b>{_esc(_short_text(product["name"], 42))}</b>'f'<div class="small-label">{_esc(product["category"])} | conf {conf:.2f}</div>'f'<p>{_esc(_short_text(reason, 56))}</p></div></div></div>')return '<div class="negative-grid">' + "".join(cards[:limit]) + '</div>'items = []for product in PRODUCTS:try:110 unmodified linesdef download_new_product_template():rows = [["Cotton Polyester Blend, Slim Fit, Graphic Print, Machine Wash","T-Shirts",29.99,4.1,35,1 unmodified line]]return _write_csv_download("new_product_metadata_template.csv",["features", "category", "price", "average_rating", "rating_number", "focus_aspect"],rows,)5 unmodified linesrows = []for i, data in enumerate(records, 1):features = data.get("features") or data.get("features_text") or ""categories = data.get("category") or data.get("categories") or data.get("categories_text") or ""price = _safe_float(data.get("price"), 0.0)rating = _safe_float(data.get("average_rating") or data.get("rating"), 4.0)count = _safe_float(data.get("rating_number") or data.get("rating_count"), 0.0)53 unmodified linesrows = [["The fabric is soft and the color looks good, but it runs small.","Cotton Blend, Slim Fit, Zipper Closure","Jackets",39.99,4.2,312,1 unmodified line]]return _write_csv_download("external_review_template.csv",["review", "features", "category", "price", "average_rating", "rating_number", "highlight_aspect"],rows,)6 unmodified linesfor i, data in enumerate(records, 1):review = data.get("review") or data.get("review_text") or ""features = data.get("features") or data.get("features_text") or ""categories = data.get("category") or data.get("categories") or data.get("categories_text") or ""price = _safe_float(data.get("price"), 0.0)rating = _safe_float(data.get("average_rating") or data.get("rating"), 4.0)count = _safe_float(data.get("rating_number") or data.get("rating_count"), 0.0)87 unmodified lines.negative-grid { display:grid; grid-template-columns:repeat(6, minmax(0, 1fr)); gap:10px; margin:8px 0 14px; }.negative-card { display:grid; grid-template-columns:64px 1fr; gap:10px; border:1px solid #fecaca; border-left:4px solid #ef4444; border-radius:8px; padding:10px; background:#fffafa; }.negative-card p { margin:5px 0 0; color:#334155; font-size:13px; }.compact-negative { display:block; min-height:178px; }.compact-negative h4 { margin:0 0 8px; color:#b91c1c; font-size:14px; letter-spacing:.02em; }.negative-body { display:grid; grid-template-columns:54px 1fr; gap:8px; align-items:start; }.negative-body b { display:block; font-size:13px; line-height:1.25; }.negative-body p { font-size:12px; line-height:1.35; }.empty-negative { color:#64748b; font-size:12px; border:1px dashed #fecaca; border-radius:8px; padding:14px 8px; background:white; }.mini-product-img { width:64px; height:76px; object-fit:cover; border-radius:6px; border:1px solid var(--line); background:#f8fafc; }.negative-body .mini-product-img { width:54px; height:64px; }@media (max-width:1100px) { .negative-grid { grid-template-columns:repeat(3, minmax(0, 1fr)); } }@media (max-width:860px) { .aspect-grid, .metric-grid, .product-layout, .decision-layout, .negative-grid { grid-template-columns:1fr; } .product-img { width:100%; height:180px; } }"""1 unmodified linedef build_app() -> gr.Blocks:categories = ["All"] + sorted({p["category"] for p in PRODUCTS})merchant_categories = categories[1:] or ["Clothing"]default_merchant_category = merchant_categories[0]tags = sorted({tag for p in PRODUCTS for tag in p.get("tags", [])})with gr.Blocks(css=CSS, title="Clothing Sentiment Analysis") as demo:gr.HTML('<div id="hero"><div class="eyebrow">BERT + Metadata Cross-Attention</div><h1>Clothing Review Sentiment Intelligence App</h1><p>Explore overall sentiment, six aspect-level opinions, metadata-driven risks, and updated 10W experiment reports in a compact customer decision-support prototype.</p><div class="hero-pills"><span>Consumer decision support</span><span>Merchant diagnostics</span><span>Research dashboard</span></div></div>')33 unmodified linesgr.HTML('<div class="module-head"><h3>Product Score Monitor</h3><span class="info-tip">i<span class="tip-content">' + model_info_html() + '</span></span></div>')gr.HTML('<div class="small-label">Most negative products across the six aspects</div>')negative_spotlight = gr.HTML('<div class="note-card">Loading most negative products...</div>')with gr.Accordion("Score filters and export", open=True):with gr.Row():merchant_metric = gr.Dropdown(["Overall"] + ASPECTS, value="Overall", label="Aspect")10 unmodified lineswith gr.Column(scale=1):with gr.Group(visible=True) as risk_single_group:new_features = gr.Textbox("Cotton Polyester Blend, Slim Fit, Graphic Print, Machine Wash", label="New product features", lines=4)new_categories = gr.Dropdown(merchant_categories, value=default_merchant_category, label="New product category")with gr.Row():new_price = gr.Number(29.99, label="Price")new_rating = gr.Number(4.1, label="Expected or early average rating")20 unmodified linesexternal_review = gr.Textbox("The fabric is soft and the color looks good, but it runs small and the zipper feels weak.", label="External customer review", lines=5)with gr.Row():ext_features = gr.Textbox("Cotton Blend, Slim Fit, Zipper Closure", label="Product features", lines=3)ext_categories = gr.Dropdown(merchant_categories, value=default_merchant_category, label="Product category")with gr.Row():ext_price = gr.Number(39.99, label="Price")ext_rating = gr.Number(4.2, label="Average rating")
app.py+60-10