Spaces:
Runtime error
Runtime error
pixel3user commited on
Commit ·
dad83c8
1
Parent(s): 7b2b305
template
Browse files1 unmodified line2345674 unmodified lines121314151617181 unmodified lines1992002012022032042052062072082092102111 unmodified linePetBull‑7B‑VL demo – ZeroGPU‑ready (Qwen2.5‑VL API)"""import osimport spacesimport torchimport gradio as gr4 unmodified linesfrom qwen_vl_utils import process_vision_info # pip install qwen-vl-utilsfrom tools.retriever import search as product_searchprint("VERSIONS:", transformers.__version__, accelerate.__version__, torch.__version__, np.__version__)os.environ["ACCELERATE_USE_SLOW_RETRIEVAL"] = "true"181 unmodified lines"Safety notes: For broken/infected skin, pregnancy/lactation, infants, ""or if symptoms worsen—seek a qualified dermatologist. Patch-test first.")# Final combined messageif suggestions.strip() != '':product_section = f"Suggested products:\n{suggestions}"else:product_section = ""return f"{base}\n\n{product_section}\n\n{safety}"# ---- UI ----with gr.Blocks(title="DermalCare - Pet & Skincare Assistant") as demo:1 unmodified line23456784 unmodified lines13141516171819202122232425262728293031323334353637383940414243444546474849181 unmodified lines2312322332342352362372382392402412422432442452462472482492502512522532542552561 unmodified linePetBull‑7B‑VL demo – ZeroGPU‑ready (Qwen2.5‑VL API)"""import osimport jsonimport spacesimport torchimport gradio as gr4 unmodified linesfrom qwen_vl_utils import process_vision_info # pip install qwen-vl-utilsfrom tools.retriever import search as product_search# ---- Product JSON block markers ----PRODUCTS_START = "<DERMACARE_PRODUCTS_JSON>"PRODUCTS_END = "</DERMACARE_PRODUCTS_JSON>"def append_products_json_to_text(text_answer: str, products: list[dict]) -> str:"""Append a machine-readable JSON block to the end of the text.The Flutter client will read between the PRODUCTS_START/END tags."""payload = {"version": 1,"products": products or [],}block = PRODUCTS_START + json.dumps(payload, ensure_ascii=False) + PRODUCTS_ENDreturn text_answer.rstrip() + "\n\n" + blockdef to_product_card(meta):return {"id": meta.get("id"),"brand": meta.get("brand_en") or meta.get("brand_zh"),"name": meta.get("product_name_en") or meta.get("product_name_zh"),"category": meta.get("category_en") or meta.get("category_zh"),"price_value": meta.get("price_value"),"price_currency": meta.get("price_currency"),"why": "Matches your query by skin concern/ingredient.","how": "Use as directed on label; patch-test first.","url": meta.get("source_url"),"image_url": meta.get("image_url"),"score": round(float(meta.get("_score", 0.0)), 3),}print("VERSIONS:", transformers.__version__, accelerate.__version__, torch.__version__, np.__version__)os.environ["ACCELERATE_USE_SLOW_RETRIEVAL"] = "true"181 unmodified lines"Safety notes: For broken/infected skin, pregnancy/lactation, infants, ""or if symptoms worsen—seek a qualified dermatologist. Patch-test first.")# Final combined message (human-readable)if suggestions.strip() != '':product_section = f"Suggested products:\n{suggestions}"else:product_section = "No relevant products."human_text = f"{base}\n\n{product_section}\n\n{safety}"# Build deterministic product cards (top-3) for client rendering.# If retrieval/index fails, fall back to empty list so the client gracefully# renders only the text.products_json = []try:top3 = [to_product_card(c) for c in cands[:3]]# Filter out items missing essential fieldsproducts_json = [p for p in top3 if p.get("id") and p.get("brand") and p.get("name")]except Exception:products_json = []return append_products_json_to_text(human_text, products_json)# ---- UI ----with gr.Blocks(title="DermalCare - Pet & Skincare Assistant") as demo:
app.py+49-4