Pro-Coder commited on
Commit
329d067
·
verified ·
1 Parent(s): 6f4d996

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -318
app.py DELETED
@@ -1,318 +0,0 @@
1
- """
2
- Skin Disease Detection — Demo
3
- ------------------------------
4
- A Gradio app for Hugging Face Spaces with two tabs:
5
- 1. Image Diagnosis — upload (or pick a test image) and get a classifier
6
- prediction plus a plain-language, urgency-aware explanation.
7
- 2. Chat Assistant — a multi-turn triage chatbot. It can accept an image,
8
- asks the kind of follow-up questions a real triage intake would, and
9
- after enough context gives a preliminary assessment with urgency-tiered
10
- guidance.
11
-
12
- IMPORTANT: This remains a research/portfolio project, not a validated
13
- medical device. It has not been clinically evaluated or regulatory
14
- cleared. It should never be the sole basis for a health decision — that
15
- framing shows up once, clearly, rather than as a repeated warning block
16
- after every message, but it's still true and still matters.
17
- """
18
-
19
- import os
20
-
21
- import gradio as gr
22
- import spaces
23
- import requests
24
- from PIL import Image
25
- from transformers import pipeline
26
-
27
- # ---------------------------------------------------------------------------
28
- # Config — override any of these via Space "Variables and secrets"
29
- # ---------------------------------------------------------------------------
30
- IMAGE_MODEL_ID = os.environ.get("IMAGE_MODEL_ID", "Anwarkh1/Skin_Cancer-Image_Classification")
31
- # Hugging Face retired the old api-inference.huggingface.co Serverless API in
32
- # favor of the "Inference Providers" router (OpenAI-compatible). Any chat
33
- # model listed at https://huggingface.co/models?inference_provider=hf-inference
34
- # works here.
35
- CHAT_MODEL_ID = os.environ.get("CHAT_MODEL_ID", "Qwen/Qwen2.5-7B-Instruct")
36
- HF_TOKEN = os.environ.get("HF_TOKEN") # add as a Space SECRET to enable the chatbot
37
- ROUTER_URL = "https://router.huggingface.co/v1/chat/completions"
38
-
39
- PAGE_DISCLAIMER = (
40
- "This tool is a research/portfolio project, not a medical device. It hasn't "
41
- "been clinically validated. Use it to get informed and organize your thoughts "
42
- "before a real appointment — not as a substitute for one."
43
- )
44
-
45
- # Friendly descriptions + rough urgency tier for the HAM10000-style label set
46
- CLASS_INFO = {
47
- "actinic keratoses": {
48
- "desc": "A rough, scaly patch caused by sun damage.",
49
- "tier": "higher-risk",
50
- "why": "can occasionally progress to squamous cell carcinoma if left untreated",
51
- },
52
- "basal cell carcinoma": {
53
- "desc": "The most common type of skin cancer.",
54
- "tier": "higher-risk",
55
- "why": "is a skin cancer — usually slow-growing and rarely spreads, but needs treatment",
56
- },
57
- "melanoma": {
58
- "desc": "The most serious common type of skin cancer.",
59
- "tier": "higher-risk",
60
- "why": "can spread if not caught early, so timing matters a lot here",
61
- },
62
- "benign keratosis-like lesions": {
63
- "desc": "Non-cancerous growths such as seborrheic keratoses or solar lentigines.",
64
- "tier": "typically lower-risk",
65
- "why": "these are usually harmless skin changes related to aging or sun exposure",
66
- },
67
- "dermatofibroma": {
68
- "desc": "A common benign skin nodule, often on the legs.",
69
- "tier": "typically lower-risk",
70
- "why": "these are firm, harmless nodules, often following a minor injury like a bug bite",
71
- },
72
- "melanocytic nevi": {
73
- "desc": "Ordinary moles.",
74
- "tier": "typically lower-risk",
75
- "why": "almost all moles are benign — the main thing to watch for is change over time",
76
- },
77
- "vascular lesions": {
78
- "desc": "Blood-vessel-related marks such as angiomas.",
79
- "tier": "typically lower-risk",
80
- "why": "these are usually harmless clusters or growths of blood vessels",
81
- },
82
- }
83
-
84
- 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.
85
-
86
- CONVERSATION STYLE
87
- - Have a real back-and-forth. Ask about one or two things at a time, not a long checklist at once.
88
- - 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):
89
- - How long they've had it / when they first noticed it
90
- - Whether it's changed recently in size, shape, or color
91
- - Symptoms: itching, bleeding, pain, crusting, oozing
92
- - Personal or family history of skin cancer
93
- - Sun exposure / sunburn history
94
- - Where on the body it is
95
- - If the user shared an image, you'll also receive the image classifier's findings as extra context (not shown verbatim to the user) — weave that in naturally rather than reading out raw percentages.
96
-
97
- GIVING A PRELIMINARY ASSESSMENT
98
- 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:
99
- - State plainly what the pattern is consistent with, in plain language. Use words like "consistent with" or "commonly looks like," never "you have X" — you're not certain, and shouldn't sound certain.
100
- - HIGHER-RISK categories (melanoma, basal cell carcinoma, actinic keratosis) OR any red-flag symptoms the user describes (rapid growth, irregular/changing borders, multiple colors, asymmetry, a sore that won't heal, bleeding, diameter over roughly 6mm and growing) — be direct: recommend seeing a dermatologist soon, and explain briefly why those particular signs matter. Don't soften this into vague "keep an eye on it" language.
101
- - TYPICALLY LOWER-RISK categories (common mole, dermatofibroma, vascular lesion, benign keratosis) with no red flags — say so, explain what that category usually is, and give general skin-care guidance: sun protection, not picking or scratching at it, and watching for the ABCDE change-signs (Asymmetry, Border irregularity, Color variation, Diameter >6mm, Evolving). It's fine to mention that seeing a dermatologist is a reasonable option too, especially if they're unsure or worried — but don't invent a treatment plan or prescribe anything.
102
- - Never give specific medications, dosages, or treatment prescriptions — general skin-care habits (sun protection, moisturizing dry skin, not scratching) are fine; treating a self- or AI-identified condition is not.
103
- - 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 a dermatologist's in-person exam is what actually confirms things. Then move on; don't repeat it every turn.
104
-
105
- URGENT SITUATIONS
106
- If the user describes something urgent — rapidly growing lesion, uncontrolled bleeding, signs of infection (spreading redness, pus, fever), severe pain — tell them clearly to seek in-person or emergency care promptly, regardless of how many turns you've had.
107
-
108
- Keep replies conversational length — a few sentences for a question, a bit longer for the assessment itself."""
109
-
110
-
111
- # ---------------------------------------------------------------------------
112
- # Lazy-loaded models (so the Space boots fast and only loads on first use)
113
- # ---------------------------------------------------------------------------
114
- _classifier = None
115
-
116
-
117
- def get_classifier():
118
- global _classifier
119
- if _classifier is None:
120
- import torch
121
-
122
- device = 0 if torch.cuda.is_available() else -1
123
- _classifier = pipeline("image-classification", model=IMAGE_MODEL_ID, device=device)
124
- return _classifier
125
-
126
-
127
- def chat_completion(messages, max_tokens=500):
128
- """Call the Hugging Face Inference Providers router directly (OpenAI-compatible)."""
129
- if not HF_TOKEN:
130
- return None
131
- resp = requests.post(
132
- ROUTER_URL,
133
- headers={"Authorization": f"Bearer {HF_TOKEN}"},
134
- json={"model": CHAT_MODEL_ID, "messages": messages, "max_tokens": max_tokens},
135
- timeout=60,
136
- )
137
- resp.raise_for_status()
138
- data = resp.json()
139
- return data["choices"][0]["message"]["content"]
140
-
141
-
142
- def classify_image(image):
143
- """Run the classifier and return (preds, formatted_lines)."""
144
- clf = get_classifier()
145
- preds = clf(image, top_k=5)
146
- lines = []
147
- for p in preds:
148
- label = p["label"]
149
- score = p["score"] * 100
150
- info = CLASS_INFO.get(label.lower())
151
- info_txt = f" \n _{info['desc']}_" if info else ""
152
- lines.append(f"- **{label}** — {score:.1f}%{info_txt}")
153
- return preds, lines
154
-
155
-
156
- def build_classifier_context(preds):
157
- """Turn classifier output into a compact context block for the LLM (not shown raw to the user)."""
158
- parts = []
159
- for p in preds[:3]:
160
- label = p["label"].lower()
161
- info = CLASS_INFO.get(label)
162
- tier = info["tier"] if info else "unknown-risk"
163
- why = info["why"] if info else ""
164
- parts.append(f"{p['label']} ({p['score']*100:.1f}%, {tier}: {why})")
165
- return "Image classifier findings for your use (do not read these percentages verbatim): " + "; ".join(parts)
166
-
167
-
168
- # ---------------------------------------------------------------------------
169
- # Image diagnosis tab (single-shot: upload -> classify -> explain)
170
- # ---------------------------------------------------------------------------
171
- @spaces.GPU
172
- def diagnose_image(image):
173
- if image is None:
174
- return "Please upload an image or pick one of the test images first."
175
-
176
- try:
177
- preds, lines_list = classify_image(image)
178
- except Exception as e:
179
- return f"**Model error:** {e}\n\nMake sure `IMAGE_MODEL_ID` is a valid image-classification model."
180
-
181
- lines = ["### 🔬 Classifier prediction\n"] + lines_list
182
-
183
- if HF_TOKEN:
184
- context = build_classifier_context(preds)
185
- prompt = (
186
- f"{context}\n\n"
187
- "The user just uploaded a single image with no conversation yet. Give a short "
188
- "preliminary assessment following your instructions: plain-language explanation of "
189
- "the top finding, urgency-tiered guidance, and a brief natural mention that an "
190
- "in-person dermatologist exam is what actually confirms things. Keep it to 4-6 sentences."
191
- )
192
- try:
193
- explanation = chat_completion(
194
- [
195
- {"role": "system", "content": SYSTEM_PROMPT},
196
- {"role": "user", "content": prompt},
197
- ],
198
- max_tokens=300,
199
- )
200
- lines.append(f"\n### 🤖 Assessment\n{explanation}")
201
- except Exception as e:
202
- lines.append(f"\n_(Assessment unavailable: {e})_")
203
- else:
204
- lines.append(
205
- "\n_Add an `HF_TOKEN` secret to this Space to also get a plain-language "
206
- "assessment from the chat model here._"
207
- )
208
-
209
- lines.append(
210
- "\n---\n_Tip: use the **Chat Assistant** tab for a fuller triage conversation — "
211
- "it can ask follow-up questions and give a more tailored read._"
212
- )
213
- return "\n".join(lines)
214
-
215
-
216
- # ---------------------------------------------------------------------------
217
- # Chat tab — multimodal, multi-turn triage
218
- # ---------------------------------------------------------------------------
219
- def _extract_text_and_image(message):
220
- """Normalize gr.ChatInterface(multimodal=True) message input into (text, image_path_or_None)."""
221
- if isinstance(message, dict):
222
- text = message.get("text", "") or ""
223
- files = message.get("files") or []
224
- image_path = files[0] if files else None
225
- return text, image_path
226
- return str(message), None
227
-
228
-
229
- def chat_respond(message, history):
230
- if not HF_TOKEN:
231
- return (
232
- "Chat isn't configured yet — add an `HF_TOKEN` secret to this Space "
233
- "(Settings → Variables and secrets) to enable the chatbot."
234
- )
235
-
236
- text, image_path = _extract_text_and_image(message)
237
-
238
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
239
- for turn in history:
240
- if isinstance(turn, dict):
241
- role = turn.get("role")
242
- content = turn.get("content")
243
- if isinstance(content, str):
244
- messages.append({"role": role, "content": content})
245
- else:
246
- user_msg, bot_msg = turn
247
- if isinstance(user_msg, str):
248
- messages.append({"role": "user", "content": user_msg})
249
- if bot_msg:
250
- messages.append({"role": "assistant", "content": bot_msg})
251
-
252
- user_content = text
253
- if image_path:
254
- try:
255
- img = Image.open(image_path)
256
- preds, _ = classify_image(img)
257
- context = build_classifier_context(preds)
258
- user_content = f"{context}\n\nUser's message: {text or '(no message, just shared an image)'}"
259
- except Exception as e:
260
- user_content = f"(Image analysis failed: {e})\n\nUser's message: {text}"
261
-
262
- messages.append({"role": "user", "content": user_content})
263
-
264
- try:
265
- return chat_completion(messages, max_tokens=500)
266
- except Exception as e:
267
- return f"Sorry, I hit an error talking to the model: {e}"
268
-
269
-
270
- # ---------------------------------------------------------------------------
271
- # UI
272
- # ---------------------------------------------------------------------------
273
- EXAMPLES_DIR = "examples"
274
- example_images = []
275
- if os.path.isdir(EXAMPLES_DIR):
276
- example_images = [
277
- os.path.join(EXAMPLES_DIR, f)
278
- for f in sorted(os.listdir(EXAMPLES_DIR))
279
- if f.lower().endswith((".jpg", ".jpeg", ".png"))
280
- ]
281
-
282
- with gr.Blocks(title="Skin Disease Detection — Demo") as demo:
283
- gr.Markdown("# 🩺 Skin Disease Detection")
284
- gr.Markdown(f"_{PAGE_DISCLAIMER}_")
285
-
286
- with gr.Tab("📷 Quick Image Check"):
287
- with gr.Row():
288
- with gr.Column():
289
- img_in = gr.Image(type="pil", label="Upload a skin image")
290
- if example_images:
291
- gr.Examples(examples=example_images, inputs=img_in, label="Or try a test image")
292
- else:
293
- gr.Markdown(
294
- "_No bundled test images found. Run `scripts/download_examples.py` "
295
- "before deploying, or just upload your own image._"
296
- )
297
- analyze_btn = gr.Button("Analyze image", variant="primary")
298
- with gr.Column():
299
- result_md = gr.Markdown()
300
- analyze_btn.click(diagnose_image, inputs=img_in, outputs=result_md)
301
-
302
- with gr.Tab("💬 Chat Assistant"):
303
- gr.ChatInterface(
304
- fn=chat_respond,
305
- multimodal=True,
306
- description=(
307
- "Talk through what you're noticing, and optionally attach a photo (📎). "
308
- "The assistant will ask a few follow-up questions before giving a preliminary read."
309
- ),
310
- )
311
-
312
- gr.Markdown(
313
- "---\nBuilt with 🤗 Transformers + Gradio. "
314
- f"Image model: `{IMAGE_MODEL_ID}` · Chat model: `{CHAT_MODEL_ID}`"
315
- )
316
-
317
- if __name__ == "__main__":
318
- demo.launch()