pixel3user commited on
Commit
4b3e157
·
1 Parent(s): 2d88b86
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -47,20 +47,30 @@ model = PeftModel.from_pretrained(
47
  _model_on_gpu = False # once-per-session move
48
 
49
  def format_candidates_for_llm(cands, budget_twd=None):
50
- lines = []
51
- rank = 1
52
  for c in cands:
53
- price = f"{c.get('price_value')} {c.get('price_currency')}" if c.get("price_value") else "N/A"
54
- if budget_twd and c.get("price_currency") == "TWD" and c.get("price_value") and c["price_value"] > budget_twd:
 
 
 
 
55
  continue
56
- lines.append(
57
- f"{rank}. {c.get('brand_en','')}/{c.get('brand_zh','')} — {c.get('product_name_en') or c.get('product_name_zh')}\n"
58
- f" • Category: {c.get('category_en') or c.get('category_zh')}\n"
59
- f" • Price: {price}\n"
60
- f" • Link: {c.get('source_url')}\n"
61
- )
62
- rank += 1
63
- return "\n".join(lines) if lines else "No products matched."
 
 
 
 
 
 
 
64
 
65
  DERMA_SAFETY = (
66
  "Safety notes: For broken/infected skin, pregnancy/lactation, infants, "
@@ -80,7 +90,7 @@ def recommend_products(query_text: str, budget_twd: int | None = None, k: int =
80
  "from the provided list. Include a one-line why-it-helps and a brief how-to-use. "
81
  "Respect budget and do not invent products."
82
  )
83
- user = f"User need: {query_text}\nCandidate products:\n{context}\n{DERMA_SAFETY}"
84
 
85
  # --- if you already have Qwen2-VL loaded as text generator, reuse it.
86
  # Example skeleton (pseudo—replace with your app’s generate() function):
@@ -200,16 +210,22 @@ def pet_answer_with_recs(image, question, temperature=0.7, top_p=0.95, max_token
200
  "content": [
201
  {"type": "text", "text":
202
  "You are DermalCare's assistant.\n"
203
- "Task: Given a user message and a list of candidate skincare products, "
204
- "return a section titled 'Suggested products' with up to 3 items ONLY IF they are relevant to the user's issue. "
205
- "Each item must be strictly chosen from the candidate list; include one-line 'why it helps' and a short 'how to use'. "
206
- "If none are relevant (e.g., purely veterinary emergencies, non-skin issues), output exactly: 'No relevant products.'"}
 
 
 
 
 
 
207
  ]
208
  },{
209
  "role": "user",
210
  "content": [
211
  {"type": "text", "text": f"User message:\n{question}"},
212
- {"type": "text", "text": f"Candidates (do not invent beyond this list):\n{cand_block}"}
213
  ]
214
  }]
215
 
 
47
  _model_on_gpu = False # once-per-session move
48
 
49
  def format_candidates_for_llm(cands, budget_twd=None):
50
+ filtered = []
 
51
  for c in cands:
52
+ if (
53
+ budget_twd
54
+ and c.get("price_currency") == "TWD"
55
+ and c.get("price_value")
56
+ and c["price_value"] > budget_twd
57
+ ):
58
  continue
59
+ filtered.append({
60
+ "id": c.get("id"),
61
+ "brand_en": c.get("brand_en"),
62
+ "brand_zh": c.get("brand_zh"),
63
+ "product_name_en": c.get("product_name_en"),
64
+ "product_name_zh": c.get("product_name_zh"),
65
+ "category_en": c.get("category_en"),
66
+ "category_zh": c.get("category_zh"),
67
+ "price_value": c.get("price_value"),
68
+ "price_currency": c.get("price_currency"),
69
+ "source_url": c.get("source_url"),
70
+ "image_url": c.get("image_url"),
71
+ "score": c.get("score"),
72
+ })
73
+ return json.dumps(filtered, ensure_ascii=False, indent=2)
74
 
75
  DERMA_SAFETY = (
76
  "Safety notes: For broken/infected skin, pregnancy/lactation, infants, "
 
90
  "from the provided list. Include a one-line why-it-helps and a brief how-to-use. "
91
  "Respect budget and do not invent products."
92
  )
93
+ user = f"User need: {query_text}\nCandidate products (JSON array):\n{context}\n{DERMA_SAFETY}"
94
 
95
  # --- if you already have Qwen2-VL loaded as text generator, reuse it.
96
  # Example skeleton (pseudo—replace with your app’s generate() function):
 
210
  "content": [
211
  {"type": "text", "text":
212
  "You are DermalCare's assistant.\n"
213
+ "You will receive (1) a user question and (2) a JSON array named candidate_products containing skincare products.\n"
214
+ "If one or more products genuinely help the user, respond with: \n"
215
+ "### Suggested Products\n"
216
+ "1. **Product name**\n- **Why it helps:** ...\n- **How to use:** ...\n"
217
+ "(repeat up to 3 items)\n"
218
+ "\n<DERMACARE_PRODUCTS_JSON>{\"version\": 1, \"products\": [...]}</DERMACARE_PRODUCTS_JSON>\n"
219
+ "The JSON must list exactly the recommended products with keys: id, brand, name, category, price_value, price_currency, why, how, url, image_url.\n"
220
+ "Use values from candidate_products: use brand_en/brand_zh to compose 'brand', prefer English names but fall back to Chinese when absent.\n"
221
+ "If a field is missing, set it to null. Do NOT invent ids or products outside the provided list.\n"
222
+ "If no products are relevant, output exactly 'No relevant products.' with nothing else."}
223
  ]
224
  },{
225
  "role": "user",
226
  "content": [
227
  {"type": "text", "text": f"User message:\n{question}"},
228
+ {"type": "text", "text": f"candidate_products = {cand_block}"}
229
  ]
230
  }]
231