File size: 2,791 Bytes
2138d51
 
 
 
 
2580458
2138d51
 
2580458
2138d51
 
 
35642b7
2138d51
dbee67d
35642b7
dbee67d
 
 
2580458
dbee67d
2580458
dbee67d
35642b7
 
dbee67d
2138d51
 
2580458
2138d51
 
 
 
 
 
 
 
 
35642b7
2580458
 
 
35642b7
dbee67d
2580458
 
dbee67d
2580458
dbee67d
2580458
 
dbee67d
2580458
 
dbee67d
2580458
 
 
 
 
 
 
2138d51
2580458
2138d51
dbee67d
2138d51
 
35642b7
 
2580458
 
35642b7
 
dbee67d
2138d51
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# app.py

import gradio as gr
from PIL import Image
import torch
import pandas as pd
from transformers import AutoImageProcessor, AutoModelForImageClassification

# Load model
model_name = "Anwarkh1/Skin_Cancer-Image_Classification"
processor = AutoImageProcessor.from_pretrained(model_name)
model = AutoModelForImageClassification.from_pretrained(model_name)
label_map = model.config.id2label

# Match labels exactly from model
condition_info = {
    "actinic_keratoses": "Dry, rough patch – sometimes early sign of skin cancer.",
    "basal_cell_carcinoma": "Slow-growing skin cancer. Common but treatable.",
    "benign_keratosis-like_lesions": "Non-cancerous growth. Like age spots or warts.",
    "dermatofibroma": "Small, firm bump. Usually harmless.",
    "melanocytic_nevi": "Just a mole. Normal unless changing.",
    "melanoma": "Dangerous skin cancer. Needs fast treatment.",
    "vascular_lesions": "Red or purple patches from blood vessels."
}

# AI prediction logic
def classify_skin(image: Image.Image):
    if image is None:
        return pd.DataFrame(), "Please upload or take a photo."

    image = image.convert("RGB")
    inputs = processor(images=image, return_tensors="pt")
    with torch.no_grad():
        outputs = model(**inputs)

    logits = outputs.logits
    probs = torch.nn.functional.softmax(logits, dim=1)[0]

    threshold = 0.40
    data = []
    likely_conditions = []

    for idx, prob in enumerate(probs):
        label = label_map[idx]  # this is the exact model label
        conf = prob.item()
        status = "βœ… Positive" if conf > threshold else "❌ Negative"
        desc = condition_info.get(label, "No description available.")
        data.append({
            "Condition": label.replace("_", " ").capitalize(),
            "Confidence (%)": f"{conf*100:.2f}",
            "Status": status,
            "What it means": desc
        })
        if conf > threshold:
            likely_conditions.append(label.replace("_", " ").capitalize())

    df = pd.DataFrame(data)
    summary_text = (
        "🧾 **Summary:** " +
        (", ".join(likely_conditions) if likely_conditions else "No major concern seen by AI.") +
        "\n\nπŸ“’ Please check with a real doctor for correct diagnosis."
    )

    return df, summary_text

# Gradio UI
demo = gr.Interface(
    fn=classify_skin,
    inputs=gr.Image(type="pil", label="πŸ“· Upload or Capture Skin Image"),
    outputs=[
        gr.Dataframe(headers=["Condition", "Confidence (%)", "Status", "What it means"]),
        gr.Markdown()
    ],
    title="AI Skin Condition Classifier",
    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."
)

if __name__ == "__main__":
    demo.launch()