Spaces:
Runtime error
Runtime error
pixel3user
commited on
Commit
·
4b3e157
1
Parent(s):
2d88b86
products
Browse files
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 |
-
|
| 51 |
-
rank = 1
|
| 52 |
for c in cands:
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
continue
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
"
|
| 204 |
-
"
|
| 205 |
-
"
|
| 206 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
]
|
| 208 |
},{
|
| 209 |
"role": "user",
|
| 210 |
"content": [
|
| 211 |
{"type": "text", "text": f"User message:\n{question}"},
|
| 212 |
-
{"type": "text", "text": f"
|
| 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 |
|