pixel3user commited on
Commit
dad83c8
·
1 Parent(s): 7b2b305
Files changed (1) hide show
  1. app.py +49 -4
app.py CHANGED
@@ -2,6 +2,7 @@
2
  PetBull‑7B‑VL demo – ZeroGPU‑ready (Qwen2.5‑VL API)
3
  """
4
  import os
 
5
  import spaces
6
  import torch
7
  import gradio as gr
@@ -12,6 +13,37 @@ from peft import PeftModel
12
  from qwen_vl_utils import process_vision_info # pip install qwen-vl-utils
13
  from tools.retriever import search as product_search
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  print("VERSIONS:", transformers.__version__, accelerate.__version__, torch.__version__, np.__version__)
16
  os.environ["ACCELERATE_USE_SLOW_RETRIEVAL"] = "true"
17
 
@@ -199,13 +231,26 @@ def pet_answer_with_recs(image, question, temperature=0.7, top_p=0.95, max_token
199
  "Safety notes: For broken/infected skin, pregnancy/lactation, infants, "
200
  "or if symptoms worsen—seek a qualified dermatologist. Patch-test first."
201
  )
202
- # Final combined message
203
  if suggestions.strip() != '':
204
  product_section = f"Suggested products:\n{suggestions}"
205
  else:
206
- product_section = ""
207
-
208
- return f"{base}\n\n{product_section}\n\n{safety}"
 
 
 
 
 
 
 
 
 
 
 
 
 
209
 
210
  # ---- UI ----
211
  with gr.Blocks(title="DermalCare - Pet & Skincare Assistant") as demo:
 
2
  PetBull‑7B‑VL demo – ZeroGPU‑ready (Qwen2.5‑VL API)
3
  """
4
  import os
5
+ import json
6
  import spaces
7
  import torch
8
  import gradio as gr
 
13
  from qwen_vl_utils import process_vision_info # pip install qwen-vl-utils
14
  from tools.retriever import search as product_search
15
 
16
+ # ---- Product JSON block markers ----
17
+ PRODUCTS_START = "<DERMACARE_PRODUCTS_JSON>"
18
+ PRODUCTS_END = "</DERMACARE_PRODUCTS_JSON>"
19
+
20
+ def append_products_json_to_text(text_answer: str, products: list[dict]) -> str:
21
+ """
22
+ Append a machine-readable JSON block to the end of the text.
23
+ The Flutter client will read between the PRODUCTS_START/END tags.
24
+ """
25
+ payload = {
26
+ "version": 1,
27
+ "products": products or [],
28
+ }
29
+ block = PRODUCTS_START + json.dumps(payload, ensure_ascii=False) + PRODUCTS_END
30
+ return text_answer.rstrip() + "\n\n" + block
31
+
32
+ def to_product_card(meta):
33
+ return {
34
+ "id": meta.get("id"),
35
+ "brand": meta.get("brand_en") or meta.get("brand_zh"),
36
+ "name": meta.get("product_name_en") or meta.get("product_name_zh"),
37
+ "category": meta.get("category_en") or meta.get("category_zh"),
38
+ "price_value": meta.get("price_value"),
39
+ "price_currency": meta.get("price_currency"),
40
+ "why": "Matches your query by skin concern/ingredient.",
41
+ "how": "Use as directed on label; patch-test first.",
42
+ "url": meta.get("source_url"),
43
+ "image_url": meta.get("image_url"),
44
+ "score": round(float(meta.get("_score", 0.0)), 3),
45
+ }
46
+
47
  print("VERSIONS:", transformers.__version__, accelerate.__version__, torch.__version__, np.__version__)
48
  os.environ["ACCELERATE_USE_SLOW_RETRIEVAL"] = "true"
49
 
 
231
  "Safety notes: For broken/infected skin, pregnancy/lactation, infants, "
232
  "or if symptoms worsen—seek a qualified dermatologist. Patch-test first."
233
  )
234
+ # Final combined message (human-readable)
235
  if suggestions.strip() != '':
236
  product_section = f"Suggested products:\n{suggestions}"
237
  else:
238
+ product_section = "No relevant products."
239
+
240
+ human_text = f"{base}\n\n{product_section}\n\n{safety}"
241
+
242
+ # Build deterministic product cards (top-3) for client rendering.
243
+ # If retrieval/index fails, fall back to empty list so the client gracefully
244
+ # renders only the text.
245
+ products_json = []
246
+ try:
247
+ top3 = [to_product_card(c) for c in cands[:3]]
248
+ # Filter out items missing essential fields
249
+ products_json = [p for p in top3 if p.get("id") and p.get("brand") and p.get("name")]
250
+ except Exception:
251
+ products_json = []
252
+
253
+ return append_products_json_to_text(human_text, products_json)
254
 
255
  # ---- UI ----
256
  with gr.Blocks(title="DermalCare - Pet & Skincare Assistant") as demo: