Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,18 +12,18 @@ processor = AutoImageProcessor.from_pretrained(model_name)
|
|
12 |
model = AutoModelForImageClassification.from_pretrained(model_name)
|
13 |
label_map = model.config.id2label
|
14 |
|
15 |
-
#
|
16 |
condition_info = {
|
17 |
-
"
|
18 |
-
"
|
19 |
-
"
|
20 |
"dermatofibroma": "Small, firm bump. Usually harmless.",
|
21 |
-
"
|
22 |
"melanoma": "Dangerous skin cancer. Needs fast treatment.",
|
23 |
-
"
|
24 |
}
|
25 |
|
26 |
-
# AI logic
|
27 |
def classify_skin(image: Image.Image):
|
28 |
if image is None:
|
29 |
return pd.DataFrame(), "Please upload or take a photo."
|
@@ -41,17 +41,18 @@ def classify_skin(image: Image.Image):
|
|
41 |
likely_conditions = []
|
42 |
|
43 |
for idx, prob in enumerate(probs):
|
44 |
-
label = label_map[idx]
|
45 |
conf = prob.item()
|
46 |
status = "β
Positive" if conf > threshold else "β Negative"
|
|
|
47 |
data.append({
|
48 |
-
"Condition": label.capitalize(),
|
49 |
"Confidence (%)": f"{conf*100:.2f}",
|
50 |
"Status": status,
|
51 |
-
"What it means":
|
52 |
})
|
53 |
if conf > threshold:
|
54 |
-
likely_conditions.append(label.capitalize())
|
55 |
|
56 |
df = pd.DataFrame(data)
|
57 |
summary_text = (
|
@@ -62,7 +63,7 @@ def classify_skin(image: Image.Image):
|
|
62 |
|
63 |
return df, summary_text
|
64 |
|
65 |
-
# Gradio
|
66 |
demo = gr.Interface(
|
67 |
fn=classify_skin,
|
68 |
inputs=gr.Image(type="pil", label="π· Upload or Capture Skin Image"),
|
@@ -71,7 +72,7 @@ demo = gr.Interface(
|
|
71 |
gr.Markdown()
|
72 |
],
|
73 |
title="AI Skin Condition Classifier",
|
74 |
-
description="Upload a photo of a skin issue. The AI will check 7 common conditions and suggest what
|
75 |
)
|
76 |
|
77 |
if __name__ == "__main__":
|
|
|
12 |
model = AutoModelForImageClassification.from_pretrained(model_name)
|
13 |
label_map = model.config.id2label
|
14 |
|
15 |
+
# Match labels exactly from model
|
16 |
condition_info = {
|
17 |
+
"actinic_keratoses": "Dry, rough patch β sometimes early sign of skin cancer.",
|
18 |
+
"basal_cell_carcinoma": "Slow-growing skin cancer. Common but treatable.",
|
19 |
+
"benign_keratosis-like_lesions": "Non-cancerous growth. Like age spots or warts.",
|
20 |
"dermatofibroma": "Small, firm bump. Usually harmless.",
|
21 |
+
"melanocytic_nevi": "Just a mole. Normal unless changing.",
|
22 |
"melanoma": "Dangerous skin cancer. Needs fast treatment.",
|
23 |
+
"vascular_lesions": "Red or purple patches from blood vessels."
|
24 |
}
|
25 |
|
26 |
+
# AI prediction logic
|
27 |
def classify_skin(image: Image.Image):
|
28 |
if image is None:
|
29 |
return pd.DataFrame(), "Please upload or take a photo."
|
|
|
41 |
likely_conditions = []
|
42 |
|
43 |
for idx, prob in enumerate(probs):
|
44 |
+
label = label_map[idx] # this is the exact model label
|
45 |
conf = prob.item()
|
46 |
status = "β
Positive" if conf > threshold else "β Negative"
|
47 |
+
desc = condition_info.get(label, "No description available.")
|
48 |
data.append({
|
49 |
+
"Condition": label.replace("_", " ").capitalize(),
|
50 |
"Confidence (%)": f"{conf*100:.2f}",
|
51 |
"Status": status,
|
52 |
+
"What it means": desc
|
53 |
})
|
54 |
if conf > threshold:
|
55 |
+
likely_conditions.append(label.replace("_", " ").capitalize())
|
56 |
|
57 |
df = pd.DataFrame(data)
|
58 |
summary_text = (
|
|
|
63 |
|
64 |
return df, summary_text
|
65 |
|
66 |
+
# Gradio UI
|
67 |
demo = gr.Interface(
|
68 |
fn=classify_skin,
|
69 |
inputs=gr.Image(type="pil", label="π· Upload or Capture Skin Image"),
|
|
|
72 |
gr.Markdown()
|
73 |
],
|
74 |
title="AI Skin Condition Classifier",
|
75 |
+
description="Upload a photo of a skin issue. The AI will check 7 common conditions and suggest what's likely. For support only β not a diagnosis."
|
76 |
)
|
77 |
|
78 |
if __name__ == "__main__":
|