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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
from tensorflow.keras.applications.efficientnet_v2 import preprocess_input
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Class names for the 5 skin conditions
|
| 10 |
+
CLASS_NAMES = [
|
| 11 |
+
'Atopic Dermatitis',
|
| 12 |
+
'Eczema',
|
| 13 |
+
'Psoriasis',
|
| 14 |
+
'Seborrheic Keratoses',
|
| 15 |
+
'Tinea Ringworm Candidiasis'
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
# Class descriptions
|
| 19 |
+
CLASS_DESCRIPTIONS = {
|
| 20 |
+
'Atopic Dermatitis': 'A chronic inflammatory skin condition causing dry, itchy patches',
|
| 21 |
+
'Eczema': 'Inflammatory skin condition causing red, itchy, and inflamed patches',
|
| 22 |
+
'Psoriasis': 'Autoimmune condition causing thick, scaly patches on the skin',
|
| 23 |
+
'Seborrheic Keratoses': 'Common benign (non-cancerous) skin growths',
|
| 24 |
+
'Tinea Ringworm Candidiasis': 'Fungal skin infections causing circular, scaly patches'
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
@gr.utils.require_minimum_gradio_version("3.0.0")
|
| 28 |
+
class DermaAIModel:
|
| 29 |
+
def __init__(self):
|
| 30 |
+
self.model = None
|
| 31 |
+
self.load_model()
|
| 32 |
+
|
| 33 |
+
def load_model(self):
|
| 34 |
+
"""Load the DermaAI model from Hugging Face Hub"""
|
| 35 |
+
try:
|
| 36 |
+
print("π Loading DermaAI model from Hugging Face...")
|
| 37 |
+
model_path = hf_hub_download(
|
| 38 |
+
repo_id="Siraja704/DermaAI",
|
| 39 |
+
filename="DermaAI.keras"
|
| 40 |
+
)
|
| 41 |
+
self.model = tf.keras.models.load_model(model_path)
|
| 42 |
+
print("β
Model loaded successfully!")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"β Error loading model: {e}")
|
| 45 |
+
raise e
|
| 46 |
+
|
| 47 |
+
def predict(self, image):
|
| 48 |
+
"""Make prediction on the input image"""
|
| 49 |
+
if self.model is None:
|
| 50 |
+
return {"error": "Model not loaded"}
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
# Preprocess image
|
| 54 |
+
if image is None:
|
| 55 |
+
return {"error": "No image provided"}
|
| 56 |
+
|
| 57 |
+
# Convert to RGB if necessary
|
| 58 |
+
if image.mode != 'RGB':
|
| 59 |
+
image = image.convert('RGB')
|
| 60 |
+
|
| 61 |
+
# Resize to model input size
|
| 62 |
+
image_resized = image.resize((224, 224))
|
| 63 |
+
|
| 64 |
+
# Convert to numpy array and preprocess
|
| 65 |
+
image_array = np.array(image_resized)
|
| 66 |
+
image_array = preprocess_input(image_array)
|
| 67 |
+
image_array = np.expand_dims(image_array, axis=0)
|
| 68 |
+
|
| 69 |
+
# Make prediction
|
| 70 |
+
predictions = self.model.predict(image_array, verbose=0)
|
| 71 |
+
|
| 72 |
+
# Get results
|
| 73 |
+
predicted_class_idx = np.argmax(predictions[0])
|
| 74 |
+
confidence = float(predictions[0][predicted_class_idx])
|
| 75 |
+
|
| 76 |
+
# Prepare results dictionary for Gradio
|
| 77 |
+
results = {}
|
| 78 |
+
for i, class_name in enumerate(CLASS_NAMES):
|
| 79 |
+
results[class_name] = float(predictions[0][i])
|
| 80 |
+
|
| 81 |
+
return results
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"β Error during prediction: {e}")
|
| 85 |
+
return {"error": f"Prediction failed: {str(e)}"}
|
| 86 |
+
|
| 87 |
+
# Initialize model
|
| 88 |
+
print("π Initializing DermaAI...")
|
| 89 |
+
derma_model = DermaAIModel()
|
| 90 |
+
|
| 91 |
+
def predict_skin_condition(image):
|
| 92 |
+
"""Wrapper function for Gradio interface"""
|
| 93 |
+
if image is None:
|
| 94 |
+
return {"error": "Please upload an image"}
|
| 95 |
+
|
| 96 |
+
return derma_model.predict(image)
|
| 97 |
+
|
| 98 |
+
def get_medical_advice(image):
|
| 99 |
+
"""Provide medical advice based on prediction"""
|
| 100 |
+
if image is None:
|
| 101 |
+
return "Please upload an image first."
|
| 102 |
+
|
| 103 |
+
results = derma_model.predict(image)
|
| 104 |
+
|
| 105 |
+
if "error" in results:
|
| 106 |
+
return results["error"]
|
| 107 |
+
|
| 108 |
+
# Find the top prediction
|
| 109 |
+
top_prediction = max(results, key=results.get)
|
| 110 |
+
confidence = results[top_prediction] * 100
|
| 111 |
+
|
| 112 |
+
# Generate advice based on confidence
|
| 113 |
+
advice = f"**Predicted Condition:** {top_prediction}\n\n"
|
| 114 |
+
advice += f"**Confidence:** {confidence:.1f}%\n\n"
|
| 115 |
+
advice += f"**Description:** {CLASS_DESCRIPTIONS.get(top_prediction, 'No description available')}\n\n"
|
| 116 |
+
|
| 117 |
+
if confidence < 30:
|
| 118 |
+
advice += "β οΈ **Low Confidence Warning:** The AI model has low confidence in this prediction. Please retake the image with better lighting and focus, or consult a healthcare professional."
|
| 119 |
+
elif confidence < 60:
|
| 120 |
+
advice += "π **Moderate Confidence:** This is a preliminary assessment. Consider consulting with a healthcare professional for accurate diagnosis."
|
| 121 |
+
else:
|
| 122 |
+
advice += "β
**High Confidence:** The model shows high confidence, but this is still a preliminary assessment."
|
| 123 |
+
|
| 124 |
+
advice += "\n\nπ₯ **Important Medical Disclaimer:** This AI tool is for educational purposes only and should not replace professional medical diagnosis. Always consult qualified healthcare professionals for proper medical evaluation and treatment."
|
| 125 |
+
|
| 126 |
+
return advice
|
| 127 |
+
|
| 128 |
+
# Custom CSS for better styling
|
| 129 |
+
custom_css = """
|
| 130 |
+
.gradio-container {
|
| 131 |
+
font-family: 'Arial', sans-serif;
|
| 132 |
+
max-width: 1200px;
|
| 133 |
+
margin: 0 auto;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
.medical-disclaimer {
|
| 137 |
+
background-color: #fff3cd;
|
| 138 |
+
border: 1px solid #ffeaa7;
|
| 139 |
+
border-radius: 8px;
|
| 140 |
+
padding: 15px;
|
| 141 |
+
margin: 10px 0;
|
| 142 |
+
color: #856404;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
.prediction-box {
|
| 146 |
+
background-color: #f8f9fa;
|
| 147 |
+
border-radius: 8px;
|
| 148 |
+
padding: 15px;
|
| 149 |
+
margin: 10px 0;
|
| 150 |
+
}
|
| 151 |
+
"""
|
| 152 |
+
|
| 153 |
+
# Create Gradio interface
|
| 154 |
+
with gr.Blocks(
|
| 155 |
+
css=custom_css,
|
| 156 |
+
title="DermaAI - Skin Disease Classification",
|
| 157 |
+
theme=gr.themes.Soft()
|
| 158 |
+
) as demo:
|
| 159 |
+
|
| 160 |
+
gr.HTML("""
|
| 161 |
+
<div style="text-align: center; margin-bottom: 20px;">
|
| 162 |
+
<h1>π₯ DermaAI - Skin Disease Classification</h1>
|
| 163 |
+
<p style="font-size: 18px; color: #666;">
|
| 164 |
+
AI-powered skin condition analysis using deep learning
|
| 165 |
+
</p>
|
| 166 |
+
</div>
|
| 167 |
+
""")
|
| 168 |
+
|
| 169 |
+
gr.HTML("""
|
| 170 |
+
<div class="medical-disclaimer">
|
| 171 |
+
<h3>βοΈ Important Medical Disclaimer</h3>
|
| 172 |
+
<p><strong>This AI tool is for educational and research purposes only.</strong>
|
| 173 |
+
It should not be used as a substitute for professional medical diagnosis or treatment.
|
| 174 |
+
Always consult with qualified healthcare professionals for proper medical evaluation.</p>
|
| 175 |
+
</div>
|
| 176 |
+
""")
|
| 177 |
+
|
| 178 |
+
with gr.Row():
|
| 179 |
+
with gr.Column(scale=1):
|
| 180 |
+
gr.HTML("<h3>πΈ Upload Skin Image</h3>")
|
| 181 |
+
input_image = gr.Image(
|
| 182 |
+
type="pil",
|
| 183 |
+
label="Upload a clear image of the skin condition",
|
| 184 |
+
height=400
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
gr.HTML("""
|
| 188 |
+
<div style="margin-top: 15px; padding: 10px; background-color: #e3f2fd; border-radius: 5px;">
|
| 189 |
+
<h4>π Image Guidelines:</h4>
|
| 190 |
+
<ul>
|
| 191 |
+
<li>Use good lighting and focus</li>
|
| 192 |
+
<li>Ensure the affected area is clearly visible</li>
|
| 193 |
+
<li>Avoid blurry or dark images</li>
|
| 194 |
+
<li>JPG, PNG formats supported</li>
|
| 195 |
+
</ul>
|
| 196 |
+
</div>
|
| 197 |
+
""")
|
| 198 |
+
|
| 199 |
+
with gr.Column(scale=1):
|
| 200 |
+
gr.HTML("<h3>π Analysis Results</h3>")
|
| 201 |
+
|
| 202 |
+
prediction_output = gr.Label(
|
| 203 |
+
label="Prediction Confidence Scores",
|
| 204 |
+
num_top_classes=5
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
medical_advice = gr.Markdown(
|
| 208 |
+
label="Medical Assessment",
|
| 209 |
+
value="Upload an image to see the analysis..."
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
gr.HTML("""
|
| 213 |
+
<div style="margin-top: 20px; padding: 15px; background-color: #f0f8ff; border-radius: 8px;">
|
| 214 |
+
<h3>π©Ί Supported Skin Conditions</h3>
|
| 215 |
+
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 15px; margin-top: 10px;">
|
| 216 |
+
<div><strong>Atopic Dermatitis:</strong> Chronic inflammatory skin condition</div>
|
| 217 |
+
<div><strong>Eczema:</strong> Red, itchy, inflamed skin patches</div>
|
| 218 |
+
<div><strong>Psoriasis:</strong> Thick, scaly skin patches</div>
|
| 219 |
+
<div><strong>Seborrheic Keratoses:</strong> Benign skin growths</div>
|
| 220 |
+
<div><strong>Tinea Ringworm Candidiasis:</strong> Fungal skin infections</div>
|
| 221 |
+
</div>
|
| 222 |
+
</div>
|
| 223 |
+
""")
|
| 224 |
+
|
| 225 |
+
# Set up the interface interactions
|
| 226 |
+
input_image.change(
|
| 227 |
+
fn=predict_skin_condition,
|
| 228 |
+
inputs=input_image,
|
| 229 |
+
outputs=prediction_output
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
input_image.change(
|
| 233 |
+
fn=get_medical_advice,
|
| 234 |
+
inputs=input_image,
|
| 235 |
+
outputs=medical_advice
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
gr.HTML("""
|
| 239 |
+
<div style="text-align: center; margin-top: 30px; padding: 20px; background-color: #f8f9fa; border-radius: 8px;">
|
| 240 |
+
<h3>π About DermaAI</h3>
|
| 241 |
+
<p>DermaAI is built using EfficientNetV2 architecture and trained on dermatological images.
|
| 242 |
+
The model analyzes skin images and provides confidence scores for 5 different skin conditions.</p>
|
| 243 |
+
<p><strong>Model:</strong> <a href="https://huggingface.co/Siraja704/DermaAI" target="_blank">Siraja704/DermaAI</a></p>
|
| 244 |
+
<p><strong>Framework:</strong> TensorFlow/Keras | <strong>Architecture:</strong> EfficientNetV2</p>
|
| 245 |
+
</div>
|
| 246 |
+
""")
|
| 247 |
+
|
| 248 |
+
# Launch the interface
|
| 249 |
+
if __name__ == "__main__":
|
| 250 |
+
demo.launch(
|
| 251 |
+
server_name="0.0.0.0",
|
| 252 |
+
server_port=7860,
|
| 253 |
+
share=False
|
| 254 |
+
)
|