import os, io, requests, time from PIL import Image import gradio as gr API_URL = "https://api-inference.huggingface.co/tuphamdf/skincare-detection" HF_TOKEN = os.environ.get("HF_TOKEN", "") HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {} def analyze(image): if image is None: return "Δεν δόθηκε εικόνα." # Downscale για ταχύτητα image = image.copy() image.thumbnail((1024, 1024)) buf = io.BytesIO() image.save(buf, format="JPEG", quality=90) data = buf.getvalue() # Απλό retry αν το μοντέλο “ζεσταίνεται” for i in range(3): r = requests.post(API_URL, headers=HEADERS, data=data, timeout=60) if r.status_code == 503: time.sleep(2*(i+1)) continue r.raise_for_status() break preds = r.json() try: top = sorted(preds, key=lambda x: x.get("score", 0), reverse=True)[:3] except Exception: return f"Μη αναμενόμενη απόκριση API: {preds}" labels = [p["label"].lower() for p in top if "label" in p] recos = [] if any("acne" in l or "pimple" in l for l in labels): recos.append("Ήπιος καθαρισμός + BHA 1–2x/εβδ.") if any("red" in l or "rosacea" in l or "erythema" in l for l in labels): recos.append("Serum με niacinamide/centella (soothing).") if any("dry" in l or "xerosis" in l for l in labels): recos.append("Ενυδάτωση με ceramides + hyaluronic acid.") if not recos: recos.append("Βασική ρουτίνα: gentle cleanser, ενυδατική, SPF.") result_lines = [f"{p['label']}: {p['score']:.1%}" for p in top if "label" in p] return ( "Ανάλυση (top-3):\n- " + "\n- ".join(result_lines) + "\n\nΠροτάσεις:\n- " + "\n- ".join(recos) + "\n\n⚠️ MVP επίδειξης — όχι ιατρική διάγνωση." ) demo = gr.Interface( fn=analyze, inputs=gr.Image(type="pil", sources=["upload","webcam"], label="Ανέβασε ή τράβηξε φωτογραφία"), outputs=gr.Textbox(label="Αποτέλεσμα"), title="AI Skin Analyzer (MVP)", description="Ανάλυση δέρματος με Hugging Face Inference API (demo)." ) if __name__ == "__main__": demo.launch()