asimniaz007 commited on
Commit
3d826e8
·
verified ·
1 Parent(s): e9469a3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +225 -0
app.py ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ from transformers import AutoModelForImageClassification, AutoImageProcessor
4
+ from PIL import Image
5
+ import torch
6
+
7
+ # Set the title of the application
8
+ st.title("Dermavision")
9
+ st.write(
10
+ "Upload an image of the affected skin area, and the app will classify the disease and provide analysis."
11
+ )
12
+
13
+ # Cache model and processor loading
14
+ @st.cache_resource
15
+ def load_model():
16
+ repo_name = "Jayanth2002/dinov2-base-finetuned-SkinDisease"
17
+ processor = AutoImageProcessor.from_pretrained(repo_name)
18
+ model = AutoModelForImageClassification.from_pretrained(repo_name)
19
+ return model, processor
20
+
21
+ model, processor = load_model()
22
+
23
+ # Define the class names
24
+ class_names = [
25
+ 'Basal Cell Carcinoma', 'Darier_s Disease', 'Epidermolysis Bullosa Pruriginosa',
26
+ 'Hailey-Hailey Disease', 'Herpes Simplex', 'Impetigo', 'Larva Migrans',
27
+ 'Leprosy Borderline', 'Leprosy Lepromatous', 'Leprosy Tuberculoid', 'Lichen Planus',
28
+ 'Lupus Erythematosus Chronicus Discoides', 'Melanoma', 'Molluscum Contagiosum',
29
+ 'Mycosis Fungoides', 'Neurofibromatosis', 'Papilomatosis Confluentes And Reticulate',
30
+ 'Pediculosis Capitis', 'Pityriasis Rosea', 'Porokeratosis Actinic', 'Psoriasis',
31
+ 'Tinea Corporis', 'Tinea Nigra', 'Tungiasis', 'actinic keratosis', 'dermatofibroma',
32
+ 'nevus', 'pigmented benign keratosis', 'seborrheic keratosis', 'squamous cell carcinoma',
33
+ 'vascular lesion'
34
+ ]
35
+
36
+ # Define reasons, treatments, and home remedies for each disease
37
+ disease_analysis = {
38
+ "Basal Cell Carcinoma": {
39
+ "reason": "Caused by prolonged exposure to ultraviolet (UV) radiation from sunlight or tanning beds, as well as genetic predisposition.",
40
+ "treatment": "Surgical removal, radiation therapy, or topical treatments.",
41
+ "home_remedy": "Apply aloe vera gel to soothe the skin and use green tea extracts for antioxidant benefits."
42
+ },
43
+ "Darier_s Disease": {
44
+ "reason": "A rare genetic disorder caused by mutations in the ATP2A2 gene, leading to issues with skin cell adhesion.",
45
+ "treatment": "Retinoids, moisturizers, and sun protection.",
46
+ "home_remedy": "Use oatmeal baths to relieve irritation and avoid tight clothing to prevent friction."
47
+ },
48
+ "Epidermolysis Bullosa Pruriginosa": {
49
+ "reason": "A rare genetic disorder causing skin fragility and blistering.",
50
+ "treatment": "Wound care, pain management, and avoiding trauma to the skin.",
51
+ "home_remedy": "Apply coconut oil for soothing and keep the skin hydrated with gentle moisturizers."
52
+ },
53
+ "Hailey-Hailey Disease": {
54
+ "reason": "A genetic disorder caused by mutations in the ATP2C1 gene, leading to improper skin cell cohesion.",
55
+ "treatment": "Topical steroids, antibiotics, and avoiding friction or heat.",
56
+ "home_remedy": "Cool compresses and aloe vera gel to relieve discomfort."
57
+ },
58
+ "Herpes Simplex": {
59
+ "reason": "Caused by the herpes simplex virus (HSV), typically transmitted through direct contact or saliva.",
60
+ "treatment": "Antiviral medications like acyclovir or valacyclovir.",
61
+ "home_remedy": "Apply cold compresses or honey to reduce pain and inflammation."
62
+ },
63
+ "Impetigo": {
64
+ "reason": "A bacterial infection caused by Staphylococcus aureus or Streptococcus pyogenes.",
65
+ "treatment": "Topical or oral antibiotics.",
66
+ "home_remedy": "Clean the affected area with diluted vinegar and apply tea tree oil for antimicrobial effects."
67
+ },
68
+ "Larva Migrans": {
69
+ "reason": "Caused by parasitic hookworms that infect the skin, usually through contaminated soil.",
70
+ "treatment": "Anti-parasitic medications like albendazole or ivermectin.",
71
+ "home_remedy": "Soak the affected area in warm water and keep the skin clean."
72
+ },
73
+ "Leprosy Borderline": {
74
+ "reason": "Caused by the bacterium Mycobacterium leprae, typically spread through prolonged close contact.",
75
+ "treatment": "Multi-drug therapy including rifampin, dapsone, and clofazimine.",
76
+ "home_remedy": "Boost immune health with a balanced diet rich in vitamin C and antioxidants."
77
+ },
78
+ "Leprosy Lepromatous": {
79
+ "reason": "A severe form of leprosy caused by Mycobacterium leprae, associated with immune system dysfunction.",
80
+ "treatment": "Long-term multi-drug therapy.",
81
+ "home_remedy": "Include turmeric in the diet for its anti-inflammatory properties."
82
+ },
83
+ "Leprosy Tuberculoid": {
84
+ "reason": "A milder form of leprosy caused by Mycobacterium leprae, with localized skin lesions.",
85
+ "treatment": "Multi-drug therapy including rifampin and dapsone.",
86
+ "home_remedy": "Maintain proper hygiene and support the immune system with vitamin-rich foods."
87
+ },
88
+ "Lichen Planus": {
89
+ "reason": "Thought to be an autoimmune condition triggered by infections, medications, or stress.",
90
+ "treatment": "Topical steroids, antihistamines, and light therapy.",
91
+ "home_remedy": "Apply aloe vera gel to soothe the skin and use turmeric paste for inflammation."
92
+ },
93
+ "Lupus Erythematosus Chronicus Discoides": {
94
+ "reason": "An autoimmune condition triggered by sunlight exposure and genetic factors.",
95
+ "treatment": "Sun protection, topical steroids, and antimalarial drugs.",
96
+ "home_remedy": "Use calendula cream for soothing and avoid sun exposure."
97
+ },
98
+ "Melanoma": {
99
+ "reason": "Caused by mutations in melanocytes, often due to excessive UV radiation exposure and genetic factors.",
100
+ "treatment": "Surgical excision, immunotherapy, or targeted therapy.",
101
+ "home_remedy": "Apply green tea extracts for antioxidant support and avoid sun exposure."
102
+ },
103
+ "Molluscum Contagiosum": {
104
+ "reason": "A viral infection caused by the molluscum contagiosum virus, spread through skin-to-skin contact or contaminated objects.",
105
+ "treatment": "Cryotherapy, topical treatments, or curettage.",
106
+ "home_remedy": "Apply apple cider vinegar as a natural antiseptic."
107
+ },
108
+ "Mycosis Fungoides": {
109
+ "reason": "A type of cutaneous T-cell lymphoma with unknown exact causes but potentially linked to immune dysfunction.",
110
+ "treatment": "Phototherapy, topical treatments, or systemic medications.",
111
+ "home_remedy": "Use coconut oil for hydration and gentle skin care products."
112
+ },
113
+ "Neurofibromatosis": {
114
+ "reason": "A genetic disorder caused by mutations in the NF1 or NF2 genes, leading to benign tumor growth.",
115
+ "treatment": "Surgical removal of tumors and symptom management.",
116
+ "home_remedy": "Maintain a healthy diet and avoid skin irritation."
117
+ },
118
+ "Papilomatosis Confluentes And Reticulate": {
119
+ "reason": "Often associated with genetic factors or chronic irritation.",
120
+ "treatment": "Symptomatic treatment and monitoring.",
121
+ "home_remedy": "Apply aloe vera gel to soothe irritation."
122
+ },
123
+ "Pediculosis Capitis": {
124
+ "reason": "Caused by infestation with head lice (Pediculus humanus capitis), transmitted through close contact.",
125
+ "treatment": "Topical insecticides or manual removal.",
126
+ "home_remedy": "Use a mixture of coconut oil and tea tree oil to remove lice."
127
+ },
128
+ "Pityriasis Rosea": {
129
+ "reason": "Likely caused by viral infections, though the exact virus is unknown.",
130
+ "treatment": "Symptomatic relief with antihistamines or topical treatments.",
131
+ "home_remedy": "Apply calamine lotion for relief and take lukewarm oatmeal baths."
132
+ },
133
+ "Porokeratosis Actinic": {
134
+ "reason": "Caused by prolonged UV exposure or genetic factors, leading to abnormal keratinization.",
135
+ "treatment": "Cryotherapy, topical treatments, or laser therapy.",
136
+ "home_remedy": "Use sunscreen regularly and apply aloe vera for soothing."
137
+ },
138
+ "Psoriasis": {
139
+ "reason": "An autoimmune condition triggered by stress, infections, or genetic predisposition.",
140
+ "treatment": "Topical steroids, phototherapy, or systemic medications.",
141
+ "home_remedy": "Apply coconut oil for moisture and use oatmeal baths for relief."
142
+ },
143
+ "Tinea Corporis": {
144
+ "reason": "A fungal infection caused by dermatophytes, often transmitted through contact with infected individuals or surfaces.",
145
+ "treatment": "Topical or oral antifungal medications.",
146
+ "home_remedy": "Apply tea tree oil to the affected area and keep the skin dry."
147
+ },
148
+ "Tinea Nigra": {
149
+ "reason": "A rare fungal infection caused by Hortaea werneckii, often contracted in tropical regions.",
150
+ "treatment": "Topical antifungal treatments.",
151
+ "home_remedy": "Use apple cider vinegar for cleansing and antifungal effects."
152
+ },
153
+ "Tungiasis": {
154
+ "reason": "Caused by infestation of the skin by the sand flea (Tunga penetrans), often from walking barefoot.",
155
+ "treatment": "Manual removal of fleas and wound care.",
156
+ "home_remedy": "Apply antiseptic and keep the area clean."
157
+ },
158
+ "actinic keratosis": {
159
+ "reason": "Caused by prolonged sun exposure leading to abnormal skin cell changes.",
160
+ "treatment": "Cryotherapy, topical treatments, or laser therapy.",
161
+ "home_remedy": "Use sunscreen and aloe vera for soothing."
162
+ },
163
+ "dermatofibroma": {
164
+ "reason": "Likely caused by minor skin injuries or insect bites, leading to localized fibroblast proliferation.",
165
+ "treatment": "Observation or surgical removal if necessary.",
166
+ "home_remedy": "Apply turmeric paste for natural anti-inflammatory benefits."
167
+ },
168
+ "nevus": {
169
+ "reason": "Usually congenital or caused by genetic mutations in skin cells (melanocytes).",
170
+ "treatment": "Monitoring or surgical removal if changes are observed.",
171
+ "home_remedy": "Apply coconut oil for hydration."
172
+ },
173
+ "pigmented benign keratosis": {
174
+ "reason": "Often caused by aging and prolonged UV exposure.",
175
+ "treatment": "Observation or cryotherapy for cosmetic reasons.",
176
+ "home_remedy": "Use green tea extracts for antioxidant benefits."
177
+ },
178
+ "seborrheic keratosis": {
179
+ "reason": "Caused by aging and genetic factors, with no known environmental triggers.",
180
+ "treatment": "Cryotherapy, curettage, or observation.",
181
+ "home_remedy": "Apply coconut oil for skin hydration."
182
+ },
183
+ "squamous cell carcinoma": {
184
+ "reason": "Caused by prolonged UV exposure, chemical exposure, or chronic skin irritation.",
185
+ "treatment": "Surgical removal, radiation therapy, or topical treatments.",
186
+ "home_remedy": "Use aloe vera for soothing and apply green tea extracts."
187
+ },
188
+ "vascular lesion": {
189
+ "reason": "Caused by abnormal growth or formation of blood vessels, often due to genetic or developmental factors.",
190
+ "treatment": "Laser treatment or surgical intervention.",
191
+ "home_remedy": "Apply cold compresses and use calendula cream for soothing."
192
+ }
193
+ }
194
+
195
+ # Function to classify the image
196
+ def classify_image(image):
197
+ inputs = processor(image.convert("RGB"), return_tensors="pt")
198
+ with torch.no_grad():
199
+ outputs = model(**inputs)
200
+ predicted_class_idx = outputs.logits.argmax(-1).item()
201
+ predicted_label = class_names[predicted_class_idx]
202
+ return predicted_label
203
+
204
+ # File uploader for user image
205
+ uploaded_file = st.file_uploader("Upload a skin image", type=["jpg", "jpeg", "png"])
206
+
207
+ if uploaded_file is not None:
208
+ # Display the uploaded image
209
+ image = Image.open(uploaded_file)
210
+ st.image(image, caption="Uploaded Image", use_column_width=True)
211
+
212
+ # Analyze the image
213
+ with st.spinner("Analyzing the image..."):
214
+ predicted_label = classify_image(image)
215
+ reason = disease_analysis.get(predicted_label, {}).get("reason", "Reason unknown.")
216
+ treatment = disease_analysis.get(predicted_label, {}).get("treatment", "Consult a dermatologist.")
217
+ home_remedy = disease_analysis.get(predicted_label, {}).get("home_remedy", "No specific home remedies available.")
218
+
219
+ # Display the results
220
+ st.success("Analysis Complete!")
221
+ st.write(f"**Classification**: {predicted_label}")
222
+ st.write(f"**Reason**: {reason}")
223
+ st.write(f"**Treatment**: {treatment}")
224
+ st.write(f"**Home Remedy**: {home_remedy}")
225
+ st.write("**Note:** Please consult a doctor for final recommendations and a detailed treatment plan.")