Spaces:
Sleeping
Sleeping
Commit ·
1f017cb
1
Parent(s): 029de51
Refresh catalog and harden consumer inference
Browse filesFiles changed (3)
476 unmodified lines4774784794804804814824832 unmodified lines486487488489490489490491492493494212 unmodified lines70770870970971071071171271371471571671771871972072126 unmodified lines748749750746751752753754476 unmodified lines store = product.get("store") or "Amazon" asin = product.get("parent_asin") or "-" detail = ( f'<div class="product-card product-layout">{image_html}<div>' f'<div class="product-card"><div class="product-layout">{image_html}<div>' f'<h3>{_esc(product["name"])}</h3>' f'<p class="muted">{_esc(product["categories"])}</p>' f'<div class="meta-pills"><span>Store: {_esc(store)}</span>'2 unmodified lines f'<span>Price: ${_safe_float(product["price"]):.2f}</span>' f'<span>Rating: {_safe_float(product["average_rating"]):.1f}</span>' f'<span>Reviews: {int(_safe_float(product["rating_number"]))}</span></div>' f'<p class="metadata-summary"><b>Metadata:</b> {_esc(_short_text(product["features"], 190))}</p>' f'{_recommendation_html(result, product["name"])}{_overall_html(result)}</div></div>' f'<p class="metadata-summary"><b>Metadata:</b> {_esc(_short_text(product["features"], 130))}</p>' f'</div></div><div class="decision-layout">{_overall_html(result)}' f'{_recommendation_html(result, product["name"])}</div></div>' ) evidence = f'<h4>Key review evidence</h4>{_highlight_review(product["review"], selected_aspect)}' return detail, _aspect_cards(result, product["review"], selected_aspect), evidence, _aspect_rows(result, product["review"])212 unmodified lines.status.ok { background:#ecfdf5; border-color:#86efac; color:#065f46; }.status.bad { background:#fff1f2; border-color:#fda4af; color:#991b1b; }.product-card, .overall-card, .note-card { border:1px solid var(--line); border-radius:8px; padding:16px; background:white; }.product-layout { display:grid; grid-template-columns:104px 1fr; gap:14px; align-items:start; }.product-img { width:104px; height:132px; object-fit:cover; border-radius:8px; border:1px solid var(--line); background:#f8fafc; }.product-layout { display:grid; grid-template-columns:96px 1fr; gap:12px; align-items:start; }.product-img { width:96px; height:118px; object-fit:cover; border-radius:8px; border:1px solid var(--line); background:#f8fafc; }.product-card h3 { margin:0 0 6px; font-size:18px; line-height:1.25; }.product-card p { margin:7px 0; }.metadata-summary { color:#334155; font-size:13px; line-height:1.45; }.decision-layout { display:grid; grid-template-columns:1fr 1fr; gap:10px; margin-top:10px; align-items:stretch; }.decision-layout .overall-card, .decision-layout .recommendation-card { margin:0; padding:12px; }.decision-layout .recommendation-card .small-label { display:none; }.decision-layout .recommendation-card p { display:none; }.recommendation-card { border:1px solid #fed7aa; border-left:5px solid var(--accent); background:#fff7ed; border-radius:8px; padding:13px 14px; margin:12px 0; }.recommendation-card p { margin:7px 0 0; }.muted { color:var(--muted); }26 unmodified lines.kv-table td { border-bottom:1px solid #e2e8f0; padding:9px 12px; }.kv-table td:first-child { width:220px; color:#334155; font-weight:700; background:#f8fafc; }.compact-note { color:#475569; font-size:13px; margin:4px 0 10px; }@media (max-width:860px) { .aspect-grid, .metric-grid, .product-layout { grid-template-columns:1fr; } .product-img { width:100%; height:180px; } }@media (max-width:860px) { .aspect-grid, .metric-grid, .product-layout, .decision-layout { grid-template-columns:1fr; } .product-img { width:100%; height:180px; } }"""
173 unmodified lines1741751761771781791801811821831841851861871881891901913 unmodified lines19519619718619819920020137 unmodified lines23924024123024224324424527 unmodified lines273274275264276173 unmodified lines checkpoint_dir=checkpoint_dir, device=device, )
def _prepare_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: bert = getattr(self.model, "bert", None) embeddings = getattr(bert, "embeddings", None) word_embeddings = getattr(embeddings, "word_embeddings", None) if word_embeddings is None: return input_ids vocab_size = int(word_embeddings.num_embeddings) if vocab_size <= 0: return input_ids unk_id = int(getattr(self.tokenizer, "unk_token_id", 0) or 0) return torch.where(input_ids >= vocab_size, torch.full_like(input_ids, unk_id), input_ids)
def predict(self, review_text: str, product_meta: Dict, return_attention: bool = True) -> Dict: enc = self.tokenizer(3 unmodified lines padding="max_length", return_tensors="pt", ) input_ids = enc["input_ids"].to(self.device) input_ids = self._prepare_input_ids(enc["input_ids"]).to(self.device) attn_mask = enc["attention_mask"].to(self.device)
meta_df = _meta_dict_to_df(product_meta)37 unmodified lines padding="max_length", return_tensors="pt", ) input_ids = enc["input_ids"].to(self.device) input_ids = self._prepare_input_ids(enc["input_ids"]).to(self.device) attn_mask = enc["attention_mask"].to(self.device)
meta_df = pd.concat(27 unmodified lines item.update(attn_payload) item.update(_attention_insights(attn_payload)) results.append(item) return results return resultsapp.py+11-6
data/product_catalog.json
src/inference.py+15-3