Lavender825 commited on
Commit
4ca69f1
·
1 Parent(s): 8898fac

Add ZeroGPU inference decorators

Browse files
Files changed (2) hide show
  1. app.py +18 -2
  2. requirements.txt +2 -1
app.py CHANGED
@@ -31,6 +31,19 @@ except Exception:
31
 
32
  import gradio as gr
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  from src import config as cfg
35
  from src.inference import AspectPredictor
36
 
@@ -248,6 +261,7 @@ def _aspect_rows(result: Dict[str, Any], review: str) -> List[List[Any]]:
248
  return rows
249
 
250
 
 
251
  def consumer_product_view(product_name: str, selected_aspect: str) -> Tuple[str, str, str, List[List[Any]]]:
252
  product = _get_product(product_name)
253
  try:
@@ -269,6 +283,7 @@ def consumer_product_view(product_name: str, selected_aspect: str) -> Tuple[str,
269
  return detail, _aspect_cards(result, product["review"], selected_aspect), evidence, _aspect_rows(result, product["review"])
270
 
271
 
 
272
  def filter_products(aspect: str, sentiment: str, category: str, tags: List[str], min_rating: float) -> Tuple[List[List[Any]], str]:
273
  rows = []
274
  best = None
@@ -351,6 +366,7 @@ def ablation_rows() -> List[List[Any]]:
351
  return [[labels.get(k, k), _num(ab.get(k, {}).get("mean_macro_f1")), _num(ab.get(k, {}).get("mean_accuracy"))] for k in labels if k in ab]
352
 
353
 
 
354
  def merchant_product_scores(metric: str) -> List[List[Any]]:
355
  rows = []
356
  monitored = _rank_products(PRODUCTS)[:MAX_MONITOR_PRODUCTS]
@@ -382,6 +398,7 @@ def _metadata_risks(features: str, categories: str, price: Any, rating: Any, cou
382
  return risks
383
 
384
 
 
385
  def screen_new_product(features: str, categories: str, price: Any, rating: Any, count: Any, focus: str) -> Tuple[str, List[List[Any]]]:
386
  review = "This is a new clothing item. Customers may comment on fit, fabric, quality, appearance, style, and value."
387
  try:
@@ -404,6 +421,7 @@ def screen_new_product(features: str, categories: str, price: Any, rating: Any,
404
  return f'<div class="note-card"><b>New product risk focus:</b> {_esc(summary)}<br><span class="muted">This is a metadata screening tool, not a replacement for real review evaluation.</span></div>', rows
405
 
406
 
 
407
  def external_review_predict(review: str, features: str, categories: str, price: Any, rating: Any, count: Any, selected_aspect: str) -> Tuple[str, str, List[List[Any]]]:
408
  try:
409
  result = _predict_custom(review, features, categories, price, rating, count)
@@ -605,8 +623,6 @@ def build_app() -> gr.Blocks:
605
  refresh_research = gr.Button("Refresh Research Metrics", variant="primary")
606
  refresh_research.click(lambda: (research_cards_html(), overall_metric_rows(), aspect_metric_rows(), ablation_rows()), outputs=[research_cards, overall_table, aspect_table, ablation_table])
607
  demo.load(consumer_product_view, [product_select, consumer_aspect], [product_detail, aspect_html, evidence_html, consumer_table])
608
- demo.load(merchant_product_scores, merchant_metric, merchant_scores)
609
- demo.load(filter_products, [filter_aspect, filter_sentiment, filter_category, filter_tags, min_rating], [filter_table, filter_summary])
610
  return demo
611
 
612
  demo = build_app()
 
31
 
32
  import gradio as gr
33
 
34
+ try:
35
+ import spaces
36
+ except Exception:
37
+ class _SpacesCompat:
38
+ def GPU(self, *args, **kwargs):
39
+ if args and callable(args[0]) and len(args) == 1 and not kwargs:
40
+ return args[0]
41
+ def decorator(fn):
42
+ return fn
43
+ return decorator
44
+ spaces = _SpacesCompat()
45
+
46
+
47
  from src import config as cfg
48
  from src.inference import AspectPredictor
49
 
 
261
  return rows
262
 
263
 
264
+ @spaces.GPU(duration=120)
265
  def consumer_product_view(product_name: str, selected_aspect: str) -> Tuple[str, str, str, List[List[Any]]]:
266
  product = _get_product(product_name)
267
  try:
 
283
  return detail, _aspect_cards(result, product["review"], selected_aspect), evidence, _aspect_rows(result, product["review"])
284
 
285
 
286
+ @spaces.GPU(duration=120)
287
  def filter_products(aspect: str, sentiment: str, category: str, tags: List[str], min_rating: float) -> Tuple[List[List[Any]], str]:
288
  rows = []
289
  best = None
 
366
  return [[labels.get(k, k), _num(ab.get(k, {}).get("mean_macro_f1")), _num(ab.get(k, {}).get("mean_accuracy"))] for k in labels if k in ab]
367
 
368
 
369
+ @spaces.GPU(duration=120)
370
  def merchant_product_scores(metric: str) -> List[List[Any]]:
371
  rows = []
372
  monitored = _rank_products(PRODUCTS)[:MAX_MONITOR_PRODUCTS]
 
398
  return risks
399
 
400
 
401
+ @spaces.GPU(duration=120)
402
  def screen_new_product(features: str, categories: str, price: Any, rating: Any, count: Any, focus: str) -> Tuple[str, List[List[Any]]]:
403
  review = "This is a new clothing item. Customers may comment on fit, fabric, quality, appearance, style, and value."
404
  try:
 
421
  return f'<div class="note-card"><b>New product risk focus:</b> {_esc(summary)}<br><span class="muted">This is a metadata screening tool, not a replacement for real review evaluation.</span></div>', rows
422
 
423
 
424
+ @spaces.GPU(duration=120)
425
  def external_review_predict(review: str, features: str, categories: str, price: Any, rating: Any, count: Any, selected_aspect: str) -> Tuple[str, str, List[List[Any]]]:
426
  try:
427
  result = _predict_custom(review, features, categories, price, rating, count)
 
623
  refresh_research = gr.Button("Refresh Research Metrics", variant="primary")
624
  refresh_research.click(lambda: (research_cards_html(), overall_metric_rows(), aspect_metric_rows(), ablation_rows()), outputs=[research_cards, overall_table, aspect_table, ablation_table])
625
  demo.load(consumer_product_view, [product_select, consumer_aspect], [product_detail, aspect_html, evidence_html, consumer_table])
 
 
626
  return demo
627
 
628
  demo = build_app()
requirements.txt CHANGED
@@ -4,4 +4,5 @@ transformers>=4.35.0,<5.0.0
4
  huggingface-hub>=0.30.0
5
  numpy>=1.24.0
6
  pandas>=2.0.0
7
- scikit-learn>=1.3.0
 
 
4
  huggingface-hub>=0.30.0
5
  numpy>=1.24.0
6
  pandas>=2.0.0
7
+ scikit-learn>=1.3.0
8
+ spaces>=0.32.0