dafniai commited on
Commit
95f5b8a
·
verified ·
1 Parent(s): cee0457

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, io, requests, time
2
+ from PIL import Image
3
+ import gradio as gr
4
+
5
+ API_URL = "https://api-inference.huggingface.co/tuphamdf/skincare-detection"
6
+ HF_TOKEN = os.environ.get("HF_TOKEN", "")
7
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
8
+
9
+ def analyze(image):
10
+ if image is None:
11
+ return "Δεν δόθηκε εικόνα."
12
+
13
+ # Downscale για ταχύτητα
14
+ image = image.copy()
15
+ image.thumbnail((1024, 1024))
16
+
17
+ buf = io.BytesIO()
18
+ image.save(buf, format="JPEG", quality=90)
19
+ data = buf.getvalue()
20
+
21
+ # Απλό retry αν το μοντέλο “ζεσταίνεται”
22
+ for i in range(3):
23
+ r = requests.post(API_URL, headers=HEADERS, data=data, timeout=60)
24
+ if r.status_code == 503:
25
+ time.sleep(2*(i+1))
26
+ continue
27
+ r.raise_for_status()
28
+ break
29
+
30
+ preds = r.json()
31
+ try:
32
+ top = sorted(preds, key=lambda x: x.get("score", 0), reverse=True)[:3]
33
+ except Exception:
34
+ return f"Μη αναμενόμενη απόκριση API: {preds}"
35
+
36
+ labels = [p["label"].lower() for p in top if "label" in p]
37
+ recos = []
38
+ if any("acne" in l or "pimple" in l for l in labels):
39
+ recos.append("Ήπιος καθαρισμός + BHA 1–2x/εβδ.")
40
+ if any("red" in l or "rosacea" in l or "erythema" in l for l in labels):
41
+ recos.append("Serum με niacinamide/centella (soothing).")
42
+ if any("dry" in l or "xerosis" in l for l in labels):
43
+ recos.append("Ενυδάτωση με ceramides + hyaluronic acid.")
44
+ if not recos:
45
+ recos.append("Βασική ρουτίνα: gentle cleanser, ενυδατική, SPF.")
46
+
47
+ result_lines = [f"{p['label']}: {p['score']:.1%}" for p in top if "label" in p]
48
+ return (
49
+ "Ανάλυση (top-3):\n- " + "\n- ".join(result_lines) +
50
+ "\n\nΠροτάσεις:\n- " + "\n- ".join(recos) +
51
+ "\n\n⚠️ MVP επίδειξης — όχι ιατρική διάγνωση."
52
+ )
53
+
54
+ demo = gr.Interface(
55
+ fn=analyze,
56
+ inputs=gr.Image(type="pil", sources=["upload","webcam"], label="Ανέβασε ή τράβηξε φωτογραφία"),
57
+ outputs=gr.Textbox(label="Αποτέλεσμα"),
58
+ title="AI Skin Analyzer (MVP)",
59
+ description="Ανάλυση δέρματος με Hugging Face Inference API (demo)."
60
+ )
61
+
62
+ if __name__ == "__main__":
63
+ demo.launch()