Pro-Coder commited on
Commit
8dfde0d
·
verified ·
1 Parent(s): cd95223

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -205
app.py DELETED
@@ -1,205 +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, LLM-generated explanation.
7
- 2. Chat Assistant — a cautious chatbot that discusses skin-health topics
8
- in general terms.
9
-
10
- IMPORTANT: This is an educational / demo tool only. It is NOT a medical
11
- device and does NOT provide real diagnoses. See DISCLAIMER below.
12
- """
13
-
14
- import os
15
-
16
- import gradio as gr
17
- from transformers import pipeline
18
- from huggingface_hub import InferenceClient
19
-
20
- # ---------------------------------------------------------------------------
21
- # Config — override any of these via Space "Variables and secrets"
22
- # ---------------------------------------------------------------------------
23
- IMAGE_MODEL_ID = os.environ.get("IMAGE_MODEL_ID", "Anwarkh1/Skin_Cancer-Image_Classification")
24
- CHAT_MODEL_ID = os.environ.get("CHAT_MODEL_ID", "HuggingFaceH4/zephyr-7b-beta")
25
- HF_TOKEN = os.environ.get("HF_TOKEN") # add as a Space SECRET to enable the chatbot
26
-
27
- DISCLAIMER = (
28
- "⚠️ **Demo only — not a medical device.** This tool does not provide a real "
29
- "diagnosis. Always consult a licensed dermatologist or doctor about any skin concern, "
30
- "especially a spot that is new, changing, bleeding, or growing."
31
- )
32
-
33
- # Friendly descriptions for the HAM10000-style label set used by the default model
34
- CLASS_INFO = {
35
- "actinic keratoses": "A rough, scaly patch caused by sun damage; can occasionally progress to skin cancer if untreated.",
36
- "basal cell carcinoma": "The most common type of skin cancer; usually slow-growing and rarely spreads.",
37
- "benign keratosis-like lesions": "Non-cancerous growths such as seborrheic keratoses or solar lentigines.",
38
- "dermatofibroma": "A common benign skin nodule, often on the legs; usually harmless.",
39
- "melanocytic nevi": "Ordinary moles; almost always benign.",
40
- "melanoma": "The most serious type of skin cancer; early evaluation matters.",
41
- "vascular lesions": "Blood-vessel-related marks such as angiomas; usually benign.",
42
- }
43
-
44
- SYSTEM_PROMPT = (
45
- "You are a friendly, cautious skin-health assistant inside a DEMO application. "
46
- "You are NOT a doctor and must never claim to diagnose anyone. "
47
- "You can explain general skin-condition concepts, explain what classifier labels mean, "
48
- "and encourage users to see a licensed dermatologist for anything concerning. "
49
- "Never give a definitive diagnosis, never prescribe treatment or dosages. "
50
- "If a user describes an urgent symptom (rapid growth, bleeding, severe pain, sudden "
51
- "changes to a mole), tell them to seek in-person medical care promptly. "
52
- "Keep answers concise — 3 to 6 sentences."
53
- )
54
-
55
- # ---------------------------------------------------------------------------
56
- # Lazy-loaded models (so the Space boots fast and only loads on first use)
57
- # ---------------------------------------------------------------------------
58
- _classifier = None
59
-
60
-
61
- def get_classifier():
62
- global _classifier
63
- if _classifier is None:
64
- _classifier = pipeline("image-classification", model=IMAGE_MODEL_ID)
65
- return _classifier
66
-
67
-
68
- def get_chat_client():
69
- if not HF_TOKEN:
70
- return None
71
- return InferenceClient(model=CHAT_MODEL_ID, token=HF_TOKEN)
72
-
73
-
74
- # ---------------------------------------------------------------------------
75
- # Image diagnosis tab
76
- # ---------------------------------------------------------------------------
77
- def diagnose_image(image):
78
- """Run the image classifier and, if a chat model is configured, ask it
79
- for a short plain-language explanation of the top prediction."""
80
- if image is None:
81
- return "Please upload an image or pick one of the test images first."
82
-
83
- try:
84
- clf = get_classifier()
85
- preds = clf(image, top_k=5)
86
- except Exception as e:
87
- return f"**Model error:** {e}\n\nMake sure `IMAGE_MODEL_ID` is a valid image-classification model."
88
-
89
- lines = ["### 🔬 Classifier prediction (demo only — not a real diagnosis)\n"]
90
- for p in preds:
91
- label = p["label"]
92
- score = p["score"] * 100
93
- info = CLASS_INFO.get(label.lower(), "")
94
- info_txt = f" \n _{info}_" if info else ""
95
- lines.append(f"- **{label}** — {score:.1f}%{info_txt}")
96
-
97
- client = get_chat_client()
98
- if client is not None:
99
- top = preds[0]["label"]
100
- others = ", ".join(p["label"] for p in preds[1:3])
101
- prompt = (
102
- f"An image classifier in a DEMO app predicted the top possible skin-lesion "
103
- f"category as '{top}' ({preds[0]['score']*100:.1f}% confidence), with other "
104
- f"candidates including {others}. In 3-4 short, plain-language sentences, explain "
105
- f"generally what the top category means, make clear this is not a diagnosis, and "
106
- f"suggest seeing a dermatologist, especially if the spot is new, changing, or bleeding."
107
- )
108
- try:
109
- resp = client.chat_completion(
110
- messages=[
111
- {"role": "system", "content": SYSTEM_PROMPT},
112
- {"role": "user", "content": prompt},
113
- ],
114
- max_tokens=250,
115
- )
116
- explanation = resp.choices[0].message.content
117
- lines.append(f"\n### 🤖 Assistant explanation\n{explanation}")
118
- except Exception as e:
119
- lines.append(f"\n_(Chat explanation unavailable: {e})_")
120
- else:
121
- lines.append(
122
- "\n_Add an `HF_TOKEN` secret to this Space to also get a plain-language "
123
- "explanation from the chat model here._"
124
- )
125
-
126
- lines.append(f"\n---\n{DISCLAIMER}")
127
- return "\n".join(lines)
128
-
129
-
130
- # ---------------------------------------------------------------------------
131
- # Chat tab
132
- # ---------------------------------------------------------------------------
133
- def chat_respond(message, history):
134
- client = get_chat_client()
135
- if client is None:
136
- return (
137
- "⚠️ Chat isn't configured yet. Add an `HF_TOKEN` secret to this Space "
138
- "(Settings → Variables and secrets) to enable the chatbot."
139
- )
140
-
141
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
142
- for turn in history:
143
- # supports both the legacy (user, bot) tuple format and the
144
- # newer {"role": ..., "content": ...} messages format
145
- if isinstance(turn, dict):
146
- messages.append(turn)
147
- else:
148
- user_msg, bot_msg = turn
149
- messages.append({"role": "user", "content": user_msg})
150
- if bot_msg:
151
- messages.append({"role": "assistant", "content": bot_msg})
152
- messages.append({"role": "user", "content": message})
153
-
154
- try:
155
- resp = client.chat_completion(messages=messages, max_tokens=400)
156
- return resp.choices[0].message.content
157
- except Exception as e:
158
- return f"Sorry, I hit an error talking to the model: {e}"
159
-
160
-
161
- # ---------------------------------------------------------------------------
162
- # UI
163
- # ---------------------------------------------------------------------------
164
- EXAMPLES_DIR = "examples"
165
- example_images = []
166
- if os.path.isdir(EXAMPLES_DIR):
167
- example_images = [
168
- os.path.join(EXAMPLES_DIR, f)
169
- for f in sorted(os.listdir(EXAMPLES_DIR))
170
- if f.lower().endswith((".jpg", ".jpeg", ".png"))
171
- ]
172
-
173
- with gr.Blocks(title="Skin Disease Detection — Demo") as demo:
174
- gr.Markdown("# 🩺 Skin Disease Detection — Demo")
175
- gr.Markdown(DISCLAIMER)
176
-
177
- with gr.Tab("📷 Image Diagnosis"):
178
- with gr.Row():
179
- with gr.Column():
180
- img_in = gr.Image(type="pil", label="Upload a skin image")
181
- if example_images:
182
- gr.Examples(examples=example_images, inputs=img_in, label="Or try a test image")
183
- else:
184
- gr.Markdown(
185
- "_No bundled test images found. Run `scripts/download_examples.py` "
186
- "before deploying, or just upload your own image._"
187
- )
188
- analyze_btn = gr.Button("Analyze image", variant="primary")
189
- with gr.Column():
190
- result_md = gr.Markdown()
191
- analyze_btn.click(diagnose_image, inputs=img_in, outputs=result_md)
192
-
193
- with gr.Tab("💬 Chat Assistant"):
194
- gr.ChatInterface(
195
- fn=chat_respond,
196
- description="Ask general questions about skin conditions. " + DISCLAIMER,
197
- )
198
-
199
- gr.Markdown(
200
- "---\nBuilt with 🤗 Transformers + Gradio. "
201
- f"Image model: `{IMAGE_MODEL_ID}` · Chat model: `{CHAT_MODEL_ID}`"
202
- )
203
-
204
- if __name__ == "__main__":
205
- demo.launch()