Spaces:
Sleeping
Sleeping
Commit ·
71dd534
1
Parent(s): cddf904
Polish merchant spotlight and category inputs
Browse files321 unmodified lines32232332432532632732832933033133233333433533633769 unmodified lines407408409400410411412413345 unmodified lines759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796110 unmodified lines9079089098689109119129131 unmodified line9159169178769189199209215 unmodified lines92792892988893093193293353 unmodified lines9879889899489909919929931 unmodified line995996997956998999100010016 unmodified lines100810091010969101110121013101487 unmodified lines110211031104110511061107110811091110111111121113111411151 unmodified line1117111811191120112111221123112433 unmodified lines115811591160111011611162116310 unmodified lines1174117511761127117711781179118020 unmodified lines12011202120311541204120512061207321 unmodified lines return 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 lines
def _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)} 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 lines
def negative_product_spotlight(limit: int = 6) -> str: cards = [] for aspect in ASPECTS: best = None for product in PRODUCTS: try: result = _predict_product(product["name"]) except Exception: continue d = result.get("aspect_details", {}).get(aspect, {}) if d.get("label") != "Negative": continue conf = _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>' ) continue conf, product, reason = best img = 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", "Clothing > Women > Tops > T-Shirts", "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"], ["features", "category", "price", "average_rating", "rating_number", "focus_aspect"], rows, )
5 unmodified lines rows = [] 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 "" 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 lines rows = [[ "The fabric is soft and the color looks good, but it runs small.", "Cotton Blend, Slim Fit, Zipper Closure", "Clothing > Women > Jackets", "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"], ["review", "features", "category", "price", "average_rating", "rating_number", "highlight_aspect"], rows, )
6 unmodified lines for 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 "" 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 line
def 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 lines gr.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 lines with 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") 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 lines external_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) 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