File size: 17,989 Bytes
8253efc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db3c234
8253efc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""
Skin Disease Detection β€” Demo
------------------------------
A Gradio app for Hugging Face Spaces with two tabs:
  1. Image Diagnosis β€” upload (or pick a test image) and get a classifier
     prediction plus a plain-language, urgency-aware explanation.
  2. Chat Assistant β€” a multi-turn triage chatbot. It can accept an image,
     asks the kind of follow-up questions a real triage intake would, and
     after enough context gives a preliminary assessment with urgency-tiered
     guidance.

IMPORTANT: This remains a research/portfolio project, not a validated
medical device. It has not been clinically evaluated or regulatory
cleared. It should never be the sole basis for a health decision β€” that
framing shows up once, clearly, rather than as a repeated warning block
after every message, but it's still true and still matters.
"""

import os

import gradio as gr
import spaces
import requests
from PIL import Image
from transformers import pipeline

# ---------------------------------------------------------------------------
# Config β€” override any of these via Space "Variables and secrets"
# ---------------------------------------------------------------------------
IMAGE_MODEL_ID = os.environ.get("IMAGE_MODEL_ID", "Anwarkh1/Skin_Cancer-Image_Classification")
# Hugging Face retired the old api-inference.huggingface.co Serverless API in
# favor of the "Inference Providers" router (OpenAI-compatible). Any chat
# model listed at https://huggingface.co/models?inference_provider=hf-inference
# works here.
CHAT_MODEL_ID = os.environ.get("CHAT_MODEL_ID", "Qwen/Qwen2.5-7B-Instruct")
HF_TOKEN = os.environ.get("HF_TOKEN")  # add as a Space SECRET to enable the chatbot
ROUTER_URL = "https://router.huggingface.co/v1/chat/completions"



# Optional second classifier specifically for pigmented-lesion cases (HAM10000-based,
# where melanoma is its own clean label). Useful because broader taxonomies like
# DermNet lump "Melanoma Skin Cancer, Nevi and Moles" into one label β€” this lets a
# mole/nevus-flagged image get a second, more specific opinion. Leave unset to disable.
LESION_MODEL_ID = os.environ.get("LESION_MODEL_ID", "")

# ---------------------------------------------------------------------------
# Urgency tiering β€” keyword-based rather than an exact-match label dict, so it
# works across different classifier taxonomies (the original 7-class HAM10000
# labels, the 23-class DermNet labels, or any future swap) without needing to
# hardcode every exact label string. Checked in priority order.
# ---------------------------------------------------------------------------
URGENT_KEYWORDS = [
    "melanoma", "malignant", "carcinoma", "actinic keratos",
    "bullous", "lupus", "systemic", "vasculitis", "cellulitis",
]
PROMPT_DOCTOR_KEYWORDS = [
    "fungal", "fungus", "candidiasis", "tinea", "ringworm",
    "scabies", "lyme", "infestation", "wart", "molluscum",
    "viral", "herpes", "hpv", "std", "exanthem", "drug eruption",
    "impetigo", "bacterial",
]
# Anything not matching the above falls through to "general_care" β€” typically
# manageable/chronic conditions (acne, eczema, psoriasis, hives, hair loss,
# benign tumors, common moles, etc.) where general skin-care guidance fits.

TIER_LABELS = {
    "urgent": "higher-risk β€” recommend seeing a dermatologist soon",
    "prompt_doctor_visit": "needs a proper diagnosis/prescription β€” recommend seeing a doctor, not urgent-emergency",
    "general_care": "typically manageable β€” general skin-care guidance fits, doctor visit optional",
}


def classify_tier(label):
    """Map a classifier label to an urgency tier via keyword matching."""
    l = label.lower()
    for kw in URGENT_KEYWORDS:
        if kw in l:
            return "urgent"
    for kw in PROMPT_DOCTOR_KEYWORDS:
        if kw in l:
            return "prompt_doctor_visit"
    return "general_care"


# Friendly one-line descriptions for labels we recognize exactly (both the
# original 7-class HAM10000 set and common DermNet-style labels). Purely
# cosmetic β€” if a label isn't here, the tier-based guidance still works fine
# without a description.
KNOWN_DESCRIPTIONS = {
    "actinic keratoses": "A rough, scaly patch caused by sun damage.",
    "basal cell carcinoma": "The most common type of skin cancer.",
    "melanoma": "The most serious common type of skin cancer.",
    "benign keratosis-like lesions": "Non-cancerous growths such as seborrheic keratoses or solar lentigines.",
    "dermatofibroma": "A common benign skin nodule, often on the legs.",
    "melanocytic nevi": "Ordinary moles.",
    "vascular lesions": "Blood-vessel-related marks such as angiomas.",
}

SYSTEM_PROMPT = """You are a warm, knowledgeable skin-health triage assistant. You are not a doctor and cannot diagnose anyone β€” but your job is to be genuinely useful, not to hide behind constant disclaimers.

CONVERSATION STYLE
- Have a real back-and-forth. Ask about one or two things at a time, not a long checklist at once.
- Useful things to learn over the conversation (don't force all of them if the user has already told you, or if it's clearly unnecessary):
  - How long they've had it / when they first noticed it
  - Whether it's changed recently in size, shape, or color
  - Symptoms: itching, bleeding, pain, crusting, oozing, discharge, fever
  - Personal or family history of relevant conditions
  - Relevant exposure history (sun, new products, contacts, travel, bites)
  - Where on the body it is
- If the user shared an image, you'll also receive the image classifier's findings as extra context, including an urgency tier (not shown verbatim to the user) β€” weave that in naturally rather than reading out raw percentages.

GIVING A PRELIMINARY ASSESSMENT
Once you have enough context (often after 2-5 exchanges, sooner if the user just wants a quick read, or immediately if red flags are already obvious), give a clear preliminary assessment using three tiers:
- **URGENT** (cancer-related findings, or things like bullous/autoimmune/systemic conditions, cellulitis, vasculitis) OR any red-flag symptoms (rapid growth, irregular/changing borders, multiple colors, asymmetry, a sore that won't heal, bleeding, spreading redness, fever) β€” be direct: recommend seeing a dermatologist or doctor soon, and explain briefly why those signs matter. Don't soften this into vague "keep an eye on it" language. Note: some categories (like a classifier label that groups "melanoma" together with ordinary moles) genuinely can't be told apart by the model β€” when that ambiguity exists, say so plainly and default to recommending it get checked rather than assuming it's benign.
- **NEEDS A DOCTOR VISIT, NOT URGENT** (infections β€” fungal, viral, bacterial, STD-related, infestations like scabies) β€” explain that these generally need a proper diagnosis and often a prescription to clear up, so a doctor visit is the right next step, but it's not an emergency. For anything STD-related, stay factual and non-judgmental, and emphasize in-person testing rather than guessing from a photo.
- **TYPICALLY MANAGEABLE** (acne, eczema, psoriasis, hives, hair loss, common moles, benign growths, contact dermatitis, etc.) with no red flags β€” explain what that category usually is, and give general skin-care guidance: gentle skincare habits, sun protection, not picking or scratching, and watching for changes. It's fine to mention a dermatologist visit is a reasonable option too, especially if they're unsure, worried, or it's not improving β€” but don't invent a treatment plan or prescribe anything.
- Never give specific medications, dosages, or treatment prescriptions β€” general skin-care habits are fine; treating a self- or AI-identified condition with anything beyond general care is not.
- You don't need to repeat a formal warning block every message. Say once, naturally, as part of your assessment β€” not as a bolted-on disclaimer β€” that this is a preliminary read from a conversation and an image classifier, and that an in-person exam is what actually confirms things. Then move on; don't repeat it every turn.

URGENT SITUATIONS
If the user describes something urgent β€” rapidly growing lesion, uncontrolled bleeding, signs of spreading infection, fever with a skin issue, severe pain β€” tell them clearly to seek in-person or emergency care promptly, regardless of how many turns you've had.

Keep replies conversational length β€” a few sentences for a question, a bit longer for the assessment itself."""


# ---------------------------------------------------------------------------
# Lazy-loaded models (so the Space boots fast and only loads on first use)
# ---------------------------------------------------------------------------
_classifier = None
_lesion_classifier = None


def get_classifier():
    global _classifier
    if _classifier is None:
        import torch

        device = 0 if torch.cuda.is_available() else -1
        _classifier = pipeline("image-classification", model=IMAGE_MODEL_ID, device=device)
    return _classifier


def get_lesion_classifier():
    """Optional second opinion model for pigmented-lesion cases. Returns None if disabled."""
    global _lesion_classifier
    if not LESION_MODEL_ID:
        return None
    if _lesion_classifier is None:
        import torch

        device = 0 if torch.cuda.is_available() else -1
        _lesion_classifier = pipeline("image-classification", model=LESION_MODEL_ID, device=device)
    return _lesion_classifier


def chat_completion(messages, max_tokens=500):
    """Call the Hugging Face Inference Providers router directly (OpenAI-compatible)."""
    if not HF_TOKEN:
        return None
    resp = requests.post(
        ROUTER_URL,
        headers={"Authorization": f"Bearer {HF_TOKEN}"},
        json={"model": CHAT_MODEL_ID, "messages": messages, "max_tokens": max_tokens},
        timeout=60,
    )
    resp.raise_for_status()
    data = resp.json()
    return data["choices"][0]["message"]["content"]


def classify_image(image):
    """Run the primary classifier and return (preds, formatted_lines). If the top
    prediction looks like a mixed/ambiguous pigmented-lesion label and a second
    lesion-specific model is configured, also run that for a second opinion."""
    clf = get_classifier()
    preds = clf(image, top_k=5)
    lines = []
    for p in preds:
        label = p["label"]
        score = p["score"] * 100
        desc = KNOWN_DESCRIPTIONS.get(label.lower(), "")
        desc_txt = f"  \n  _{desc}_" if desc else ""
        lines.append(f"- **{label}** β€” {score:.1f}%{desc_txt}")

    top_label = preds[0]["label"].lower()
    if ("nevi" in top_label or "mole" in top_label) and "melanoma" in top_label:
        lesion_clf = get_lesion_classifier()
        if lesion_clf is not None:
            lesion_preds = lesion_clf(image, top_k=3)
            lines.append("\n_Second opinion from a lesion-specific model (melanoma is a distinct label here):_")
            for p in lesion_preds:
                lines.append(f"- **{p['label']}** β€” {p['score']*100:.1f}%")
            preds = preds + [{"label": f"[lesion-model] {p['label']}", "score": p["score"]} for p in lesion_preds]

    return preds, lines


def build_classifier_context(preds):
    """Turn classifier output into a compact context block for the LLM (not shown raw to the user)."""
    parts = []
    for p in preds[:4]:
        label = p["label"]
        if label.startswith("[lesion-model]"):
            # second-opinion predictions: tier by the underlying label, minus the tag
            bare = label.replace("[lesion-model] ", "")
            tier = classify_tier(bare)
            parts.append(f"{label} ({p['score']*100:.1f}%, second-opinion, {tier})")
            continue
        tier = classify_tier(label)
        desc = KNOWN_DESCRIPTIONS.get(label.lower(), "")
        parts.append(f"{label} ({p['score']*100:.1f}%, {tier}{': ' + desc if desc else ''})")
    return "Image classifier findings for your use (do not read these percentages verbatim): " + "; ".join(parts)


# ---------------------------------------------------------------------------
# Image diagnosis tab (single-shot: upload -> classify -> explain)
# ---------------------------------------------------------------------------
@spaces.GPU
def diagnose_image(image):
    if image is None:
        return "Please upload an image or pick one of the test images first."

    try:
        preds, lines_list = classify_image(image)
    except Exception as e:
        return f"**Model error:** {e}\n\nMake sure `IMAGE_MODEL_ID` is a valid image-classification model."

    lines = ["### πŸ”¬ Classifier prediction\n"] + lines_list

    if HF_TOKEN:
        context = build_classifier_context(preds)
        prompt = (
            f"{context}\n\n"
            "The user just uploaded a single image with no conversation yet. Give a short "
            "preliminary assessment following your instructions: plain-language explanation of "
            "the top finding, urgency-tiered guidance, and a brief natural mention that an "
            "in-person dermatologist exam is what actually confirms things. Keep it to 4-6 sentences."
        )
        try:
            explanation = chat_completion(
                [
                    {"role": "system", "content": SYSTEM_PROMPT},
                    {"role": "user", "content": prompt},
                ],
                max_tokens=300,
            )
            lines.append(f"\n### πŸ€– Assessment\n{explanation}")
        except Exception as e:
            lines.append(f"\n_(Assessment unavailable: {e})_")
    else:
        lines.append(
            "\n_Add an `HF_TOKEN` secret to this Space to also get a plain-language "
            "assessment from the chat model here._"
        )

    lines.append(
        "\n---\n_Tip: use the **Chat Assistant** tab for a fuller triage conversation β€” "
        "it can ask follow-up questions and give a more tailored read._"
    )
    return "\n".join(lines)


# ---------------------------------------------------------------------------
# Chat tab β€” multimodal, multi-turn triage
# ---------------------------------------------------------------------------
def _extract_text_and_image(message):
    """Normalize gr.ChatInterface(multimodal=True) message input into (text, image_path_or_None)."""
    if isinstance(message, dict):
        text = message.get("text", "") or ""
        files = message.get("files") or []
        image_path = files[0] if files else None
        return text, image_path
    return str(message), None


def chat_respond(message, history):
    if not HF_TOKEN:
        return (
            "Chat isn't configured yet β€” add an `HF_TOKEN` secret to this Space "
            "(Settings β†’ Variables and secrets) to enable the chatbot."
        )

    text, image_path = _extract_text_and_image(message)

    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    for turn in history:
        if isinstance(turn, dict):
            role = turn.get("role")
            content = turn.get("content")
            if isinstance(content, str):
                messages.append({"role": role, "content": content})
        else:
            user_msg, bot_msg = turn
            if isinstance(user_msg, str):
                messages.append({"role": "user", "content": user_msg})
            if bot_msg:
                messages.append({"role": "assistant", "content": bot_msg})

    user_content = text
    if image_path:
        try:
            img = Image.open(image_path)
            preds, _ = classify_image(img)
            context = build_classifier_context(preds)
            user_content = f"{context}\n\nUser's message: {text or '(no message, just shared an image)'}"
        except Exception as e:
            user_content = f"(Image analysis failed: {e})\n\nUser's message: {text}"

    messages.append({"role": "user", "content": user_content})

    try:
        return chat_completion(messages, max_tokens=500)
    except Exception as e:
        return f"Sorry, I hit an error talking to the model: {e}"


# ---------------------------------------------------------------------------
# UI
# ---------------------------------------------------------------------------
EXAMPLES_DIR = "examples"
example_images = []
if os.path.isdir(EXAMPLES_DIR):
    example_images = [
        os.path.join(EXAMPLES_DIR, f)
        for f in sorted(os.listdir(EXAMPLES_DIR))
        if f.lower().endswith((".jpg", ".jpeg", ".png"))
    ]

with gr.Blocks(title="Skin Disease Detection β€” Demo") as demo:
    gr.Markdown("# 🩺 Skin Disease Detection")

    with gr.Tab("πŸ“· Quick Image Check"):
        with gr.Row():
            with gr.Column():
                img_in = gr.Image(type="pil", label="Upload a skin image")
                if example_images:
                    gr.Examples(examples=example_images, inputs=img_in, label="Or try a test image")
                else:
                    gr.Markdown(
                        "_No bundled test images found. Run `scripts/download_examples.py` "
                        "before deploying, or just upload your own image._"
                    )
                analyze_btn = gr.Button("Analyze image", variant="primary")
            with gr.Column():
                result_md = gr.Markdown()
        analyze_btn.click(diagnose_image, inputs=img_in, outputs=result_md)

    with gr.Tab("πŸ’¬ Chat Assistant"):
        gr.ChatInterface(
            fn=chat_respond,
            multimodal=True,
            description=(
                "Talk through what you're noticing, and optionally attach a photo (πŸ“Ž). "
                "The assistant will ask a few follow-up questions before giving a preliminary read."
            ),
        )

    gr.Markdown(
        "---\nBuilt with πŸ€— Transformers + Gradio. "
        f"Image model: `{IMAGE_MODEL_ID}`  Β·  Chat model: `{CHAT_MODEL_ID}`"
    )

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