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|
+
Advanced Gradio Interface - 4 Disease Classes
|
| 4 |
+
|
| 5 |
+
Features:
|
| 6 |
+
- Real-time detection: Normal, TB, Pneumonia, COVID-19
|
| 7 |
+
- Grad-CAM visualization (explainable AI)
|
| 8 |
+
- Improved specificity - distinguishes TB from pneumonia
|
| 9 |
+
- Confidence scores with visual indicators
|
| 10 |
+
- Clinical interpretation and recommendations
|
| 11 |
+
- Mobile-responsive design
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import gradio as gr
|
| 15 |
+
import torch
|
| 16 |
+
import torch.nn as nn
|
| 17 |
+
from torchvision import models, transforms
|
| 18 |
+
from PIL import Image
|
| 19 |
+
import numpy as np
|
| 20 |
+
import cv2
|
| 21 |
+
import matplotlib.pyplot as plt
|
| 22 |
+
from pathlib import Path
|
| 23 |
+
import io
|
| 24 |
+
|
| 25 |
+
# ============================================================================
|
| 26 |
+
# Model Setup
|
| 27 |
+
# ============================================================================
|
| 28 |
+
|
| 29 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 30 |
+
|
| 31 |
+
# Load model
|
| 32 |
+
model = models.efficientnet_b0(weights=None)
|
| 33 |
+
model.classifier[1] = nn.Linear(model.classifier[1].in_features, 4) # 4 classes
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
model.load_state_dict(torch.load('checkpoints/best_multiclass.pt', map_location=device))
|
| 37 |
+
print("β
Multi-class model loaded successfully!")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"β οΈ Error loading model: {e}")
|
| 40 |
+
|
| 41 |
+
model = model.to(device)
|
| 42 |
+
model.eval()
|
| 43 |
+
|
| 44 |
+
# Classes
|
| 45 |
+
CLASSES = ['Normal', 'Tuberculosis', 'Pneumonia', 'COVID-19']
|
| 46 |
+
CLASS_COLORS = {
|
| 47 |
+
'Normal': '#2ecc71', # Green
|
| 48 |
+
'Tuberculosis': '#e74c3c', # Red
|
| 49 |
+
'Pneumonia': '#f39c12', # Orange
|
| 50 |
+
'COVID-19': '#9b59b6' # Purple
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# Image preprocessing
|
| 54 |
+
transform = transforms.Compose([
|
| 55 |
+
transforms.Resize(256),
|
| 56 |
+
transforms.CenterCrop(224),
|
| 57 |
+
transforms.ToTensor(),
|
| 58 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 59 |
+
])
|
| 60 |
+
|
| 61 |
+
# ============================================================================
|
| 62 |
+
# Grad-CAM Implementation
|
| 63 |
+
# ============================================================================
|
| 64 |
+
|
| 65 |
+
class GradCAM:
|
| 66 |
+
def __init__(self, model, target_layer):
|
| 67 |
+
self.model = model
|
| 68 |
+
self.target_layer = target_layer
|
| 69 |
+
self.gradients = None
|
| 70 |
+
self.activations = None
|
| 71 |
+
|
| 72 |
+
def save_gradient(grad):
|
| 73 |
+
self.gradients = grad
|
| 74 |
+
|
| 75 |
+
def save_activation(module, input, output):
|
| 76 |
+
self.activations = output.detach()
|
| 77 |
+
|
| 78 |
+
target_layer.register_forward_hook(save_activation)
|
| 79 |
+
target_layer.register_full_backward_hook(lambda m, gi, go: save_gradient(go[0]))
|
| 80 |
+
|
| 81 |
+
def generate(self, input_image, target_class=None):
|
| 82 |
+
output = self.model(input_image)
|
| 83 |
+
|
| 84 |
+
if target_class is None:
|
| 85 |
+
target_class = output.argmax(dim=1)
|
| 86 |
+
|
| 87 |
+
self.model.zero_grad()
|
| 88 |
+
one_hot = torch.zeros_like(output)
|
| 89 |
+
one_hot[0][target_class] = 1
|
| 90 |
+
output.backward(gradient=one_hot, retain_graph=True)
|
| 91 |
+
|
| 92 |
+
if self.gradients is None:
|
| 93 |
+
return None, output
|
| 94 |
+
|
| 95 |
+
weights = self.gradients.mean(dim=(2, 3), keepdim=True)
|
| 96 |
+
cam = (weights * self.activations).sum(dim=1, keepdim=True)
|
| 97 |
+
cam = torch.relu(cam)
|
| 98 |
+
cam = cam.squeeze().cpu().numpy()
|
| 99 |
+
cam = (cam - cam.min()) / (cam.max() - cam.min() + 1e-8)
|
| 100 |
+
|
| 101 |
+
return cam, output
|
| 102 |
+
|
| 103 |
+
# Setup Grad-CAM
|
| 104 |
+
target_layer = model.features[-1]
|
| 105 |
+
grad_cam = GradCAM(model, target_layer)
|
| 106 |
+
|
| 107 |
+
# ============================================================================
|
| 108 |
+
# Prediction Functions
|
| 109 |
+
# ============================================================================
|
| 110 |
+
|
| 111 |
+
def predict_chest_xray(image, show_gradcam=True):
|
| 112 |
+
"""
|
| 113 |
+
Predict disease class from chest X-ray with Grad-CAM visualization
|
| 114 |
+
"""
|
| 115 |
+
if image is None:
|
| 116 |
+
return None, None, None, None
|
| 117 |
+
|
| 118 |
+
# Convert to PIL if needed
|
| 119 |
+
if isinstance(image, np.ndarray):
|
| 120 |
+
image = Image.fromarray(image).convert('RGB')
|
| 121 |
+
else:
|
| 122 |
+
image = image.convert('RGB')
|
| 123 |
+
|
| 124 |
+
# Store original for display
|
| 125 |
+
original_img = image.copy()
|
| 126 |
+
|
| 127 |
+
# Preprocess
|
| 128 |
+
input_tensor = transform(image).unsqueeze(0).to(device)
|
| 129 |
+
|
| 130 |
+
# Get prediction with Grad-CAM
|
| 131 |
+
with torch.set_grad_enabled(show_gradcam):
|
| 132 |
+
if show_gradcam:
|
| 133 |
+
cam, output = grad_cam.generate(input_tensor)
|
| 134 |
+
else:
|
| 135 |
+
output = model(input_tensor)
|
| 136 |
+
cam = None
|
| 137 |
+
|
| 138 |
+
# Get probabilities
|
| 139 |
+
probs = torch.softmax(output, dim=1)[0].cpu().detach().numpy()
|
| 140 |
+
pred_class = int(output.argmax(dim=1).item())
|
| 141 |
+
pred_label = CLASSES[pred_class]
|
| 142 |
+
confidence = float(probs[pred_class]) * 100
|
| 143 |
+
|
| 144 |
+
# Create results
|
| 145 |
+
results = {
|
| 146 |
+
CLASSES[i]: float(probs[i] * 100) for i in range(len(CLASSES))
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
# Generate visualizations
|
| 150 |
+
original_pil = create_original_display(original_img, pred_label, confidence)
|
| 151 |
+
|
| 152 |
+
if cam is not None and show_gradcam:
|
| 153 |
+
gradcam_viz = create_gradcam_visualization(original_img, cam, pred_label, confidence)
|
| 154 |
+
overlay_viz = create_overlay_visualization(original_img, cam)
|
| 155 |
+
else:
|
| 156 |
+
gradcam_viz = None
|
| 157 |
+
overlay_viz = None
|
| 158 |
+
|
| 159 |
+
# Create interpretation text
|
| 160 |
+
interpretation = create_interpretation(pred_label, confidence, results)
|
| 161 |
+
|
| 162 |
+
return results, original_pil, gradcam_viz, overlay_viz, interpretation
|
| 163 |
+
|
| 164 |
+
def create_original_display(image, pred_label, confidence):
|
| 165 |
+
"""Create annotated original image"""
|
| 166 |
+
fig, ax = plt.subplots(figsize=(8, 8))
|
| 167 |
+
ax.imshow(image)
|
| 168 |
+
ax.axis('off')
|
| 169 |
+
|
| 170 |
+
# Add prediction box
|
| 171 |
+
color = CLASS_COLORS[pred_label]
|
| 172 |
+
title = f'Prediction: {pred_label}\nConfidence: {confidence:.1f}%'
|
| 173 |
+
ax.set_title(title, fontsize=16, fontweight='bold', color=color, pad=20)
|
| 174 |
+
|
| 175 |
+
plt.tight_layout()
|
| 176 |
+
|
| 177 |
+
# Convert to PIL
|
| 178 |
+
buf = io.BytesIO()
|
| 179 |
+
plt.savefig(buf, format='png', dpi=150, bbox_inches='tight', facecolor='white')
|
| 180 |
+
plt.close()
|
| 181 |
+
buf.seek(0)
|
| 182 |
+
|
| 183 |
+
return Image.open(buf)
|
| 184 |
+
|
| 185 |
+
def create_gradcam_visualization(image, cam, pred_label, confidence):
|
| 186 |
+
"""Create Grad-CAM heatmap"""
|
| 187 |
+
# Resize CAM to image size
|
| 188 |
+
img_array = np.array(image.resize((224, 224)))
|
| 189 |
+
cam_resized = cv2.resize(cam, (224, 224))
|
| 190 |
+
|
| 191 |
+
# Create heatmap
|
| 192 |
+
heatmap = cv2.applyColorMap(np.uint8(255 * cam_resized), cv2.COLORMAP_JET)
|
| 193 |
+
heatmap = cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB)
|
| 194 |
+
|
| 195 |
+
fig, ax = plt.subplots(figsize=(8, 8))
|
| 196 |
+
ax.imshow(heatmap)
|
| 197 |
+
ax.axis('off')
|
| 198 |
+
ax.set_title('Attention Heatmap\n(Areas the model focuses on)',
|
| 199 |
+
fontsize=14, fontweight='bold', pad=20)
|
| 200 |
+
|
| 201 |
+
plt.tight_layout()
|
| 202 |
+
|
| 203 |
+
buf = io.BytesIO()
|
| 204 |
+
plt.savefig(buf, format='png', dpi=150, bbox_inches='tight', facecolor='white')
|
| 205 |
+
plt.close()
|
| 206 |
+
buf.seek(0)
|
| 207 |
+
|
| 208 |
+
return Image.open(buf)
|
| 209 |
+
|
| 210 |
+
def create_overlay_visualization(image, cam):
|
| 211 |
+
"""Create overlay of image and heatmap"""
|
| 212 |
+
img_array = np.array(image.resize((224, 224))) / 255.0
|
| 213 |
+
cam_resized = cv2.resize(cam, (224, 224))
|
| 214 |
+
|
| 215 |
+
# Create heatmap
|
| 216 |
+
heatmap = cv2.applyColorMap(np.uint8(255 * cam_resized), cv2.COLORMAP_JET)
|
| 217 |
+
heatmap = cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB) / 255.0
|
| 218 |
+
|
| 219 |
+
# Overlay
|
| 220 |
+
overlay = img_array * 0.5 + heatmap * 0.5
|
| 221 |
+
overlay = np.clip(overlay, 0, 1)
|
| 222 |
+
|
| 223 |
+
fig, ax = plt.subplots(figsize=(8, 8))
|
| 224 |
+
ax.imshow(overlay)
|
| 225 |
+
ax.axis('off')
|
| 226 |
+
ax.set_title('Explainable AI Visualization\n(Original + Heatmap)',
|
| 227 |
+
fontsize=14, fontweight='bold', pad=20)
|
| 228 |
+
|
| 229 |
+
plt.tight_layout()
|
| 230 |
+
|
| 231 |
+
buf = io.BytesIO()
|
| 232 |
+
plt.savefig(buf, format='png', dpi=150, bbox_inches='tight', facecolor='white')
|
| 233 |
+
plt.close()
|
| 234 |
+
buf.seek(0)
|
| 235 |
+
|
| 236 |
+
return Image.open(buf)
|
| 237 |
+
|
| 238 |
+
def create_interpretation(pred_label, confidence, results):
|
| 239 |
+
"""Create interpretation text with improved medical disclaimers"""
|
| 240 |
+
|
| 241 |
+
interpretation = f"""
|
| 242 |
+
## π¬ Analysis Results
|
| 243 |
+
|
| 244 |
+
### Prediction: **{pred_label}**
|
| 245 |
+
- Confidence: **{confidence:.1f}%**
|
| 246 |
+
|
| 247 |
+
### Probability Breakdown:
|
| 248 |
+
- π’ Normal: **{results['Normal']:.1f}%**
|
| 249 |
+
- π΄ Tuberculosis: **{results['Tuberculosis']:.1f}%**
|
| 250 |
+
- π Pneumonia: **{results['Pneumonia']:.1f}%**
|
| 251 |
+
- π£ COVID-19: **{results['COVID-19']:.1f}%**
|
| 252 |
+
|
| 253 |
+
---
|
| 254 |
+
|
| 255 |
+
"""
|
| 256 |
+
|
| 257 |
+
# Disease-specific interpretations
|
| 258 |
+
if pred_label == 'Tuberculosis':
|
| 259 |
+
if confidence >= 85:
|
| 260 |
+
interpretation += """
|
| 261 |
+
**β οΈ High Confidence TB Detection**
|
| 262 |
+
|
| 263 |
+
The model has detected features highly consistent with tuberculosis infection.
|
| 264 |
+
|
| 265 |
+
**CRITICAL - Immediate Actions Required:**
|
| 266 |
+
1. β
**Immediate consultation** with a healthcare provider
|
| 267 |
+
2. β
**Confirmatory sputum test** (AFB smear or GeneXpert MTB/RIF)
|
| 268 |
+
3. β
**Clinical correlation** with symptoms:
|
| 269 |
+
- Persistent cough (>2 weeks)
|
| 270 |
+
- Fever, especially night sweats
|
| 271 |
+
- Unexplained weight loss
|
| 272 |
+
- Hemoptysis (coughing blood)
|
| 273 |
+
4. β
**Isolation** and contact tracing if confirmed
|
| 274 |
+
5. β
**Chest CT scan** if needed for further evaluation
|
| 275 |
+
|
| 276 |
+
**β οΈ IMPORTANT**: This is a SCREENING tool, not a diagnostic tool.
|
| 277 |
+
Clinical diagnosis of TB requires laboratory confirmation (sputum test).
|
| 278 |
+
"""
|
| 279 |
+
else:
|
| 280 |
+
interpretation += """
|
| 281 |
+
**β οΈ Possible TB Detection**
|
| 282 |
+
|
| 283 |
+
The model has detected features suggestive of tuberculosis, but confidence is moderate.
|
| 284 |
+
|
| 285 |
+
**Recommended Actions:**
|
| 286 |
+
1. Consult healthcare provider for clinical evaluation
|
| 287 |
+
2. Consider confirmatory sputum testing
|
| 288 |
+
3. Evaluate clinical symptoms
|
| 289 |
+
4. Follow-up imaging may be recommended
|
| 290 |
+
|
| 291 |
+
**Note**: Moderate confidence requires professional medical evaluation.
|
| 292 |
+
"""
|
| 293 |
+
|
| 294 |
+
elif pred_label == 'Pneumonia':
|
| 295 |
+
if confidence >= 85:
|
| 296 |
+
interpretation += """
|
| 297 |
+
**β οΈ High Confidence Pneumonia Detection**
|
| 298 |
+
|
| 299 |
+
The model has detected features consistent with pneumonia (bacterial or viral).
|
| 300 |
+
|
| 301 |
+
**Recommended Actions:**
|
| 302 |
+
1. β
**Medical evaluation** for pneumonia diagnosis
|
| 303 |
+
2. β
**Possible confirmatory tests**:
|
| 304 |
+
- Sputum culture
|
| 305 |
+
- Blood tests (WBC count, CRP)
|
| 306 |
+
- Additional chest imaging if needed
|
| 307 |
+
3. β
**Clinical correlation** with symptoms:
|
| 308 |
+
- Cough with sputum production
|
| 309 |
+
- Fever and chills
|
| 310 |
+
- Shortness of breath
|
| 311 |
+
- Chest pain with breathing
|
| 312 |
+
4. β
**Treatment**: Antibiotics (bacterial) or supportive care (viral)
|
| 313 |
+
|
| 314 |
+
**Note**: Pneumonia can present similarly to other lung diseases.
|
| 315 |
+
Professional diagnosis is essential for appropriate treatment.
|
| 316 |
+
"""
|
| 317 |
+
else:
|
| 318 |
+
interpretation += """
|
| 319 |
+
**β οΈ Possible Pneumonia**
|
| 320 |
+
|
| 321 |
+
Features suggest possible pneumonia, but further evaluation is needed.
|
| 322 |
+
|
| 323 |
+
**Recommended Actions:**
|
| 324 |
+
1. Seek medical evaluation
|
| 325 |
+
2. Clinical symptom assessment
|
| 326 |
+
3. Consider additional diagnostic tests
|
| 327 |
+
|
| 328 |
+
**Note**: Requires professional medical evaluation for confirmation.
|
| 329 |
+
"""
|
| 330 |
+
|
| 331 |
+
elif pred_label == 'COVID-19':
|
| 332 |
+
if confidence >= 85:
|
| 333 |
+
interpretation += """
|
| 334 |
+
**β οΈ High Confidence COVID-19 Detection**
|
| 335 |
+
|
| 336 |
+
The model has detected features consistent with COVID-19 pneumonia.
|
| 337 |
+
|
| 338 |
+
**URGENT - Immediate Actions:**
|
| 339 |
+
1. β
**COVID-19 RT-PCR test** for confirmation
|
| 340 |
+
2. β
**Isolation** to prevent transmission
|
| 341 |
+
3. β
**Monitor oxygen saturation** (SpO2 levels)
|
| 342 |
+
4. β
**Seek immediate medical care** if:
|
| 343 |
+
- Difficulty breathing
|
| 344 |
+
- SpO2 < 94%
|
| 345 |
+
- Persistent chest pain
|
| 346 |
+
- Confusion or inability to stay awake
|
| 347 |
+
5. β
**Contact tracing** if positive
|
| 348 |
+
|
| 349 |
+
**Clinical Symptoms to Monitor:**
|
| 350 |
+
- Fever, cough, shortness of breath
|
| 351 |
+
- Loss of taste/smell
|
| 352 |
+
- Fatigue, body aches
|
| 353 |
+
- Gastrointestinal symptoms
|
| 354 |
+
|
| 355 |
+
**β οΈ IMPORTANT**: Imaging findings alone cannot confirm COVID-19.
|
| 356 |
+
RT-PCR or antigen testing is required for diagnosis.
|
| 357 |
+
"""
|
| 358 |
+
else:
|
| 359 |
+
interpretation += """
|
| 360 |
+
**β οΈ Possible COVID-19**
|
| 361 |
+
|
| 362 |
+
Features suggest possible COVID-19, but confirmation testing is essential.
|
| 363 |
+
|
| 364 |
+
**Recommended Actions:**
|
| 365 |
+
1. Get RT-PCR or rapid antigen test
|
| 366 |
+
2. Self-isolate pending test results
|
| 367 |
+
3. Monitor symptoms
|
| 368 |
+
4. Seek medical care if symptoms worsen
|
| 369 |
+
|
| 370 |
+
**Note**: COVID-19 diagnosis requires laboratory confirmation.
|
| 371 |
+
"""
|
| 372 |
+
|
| 373 |
+
else: # Normal
|
| 374 |
+
if confidence >= 85:
|
| 375 |
+
interpretation += """
|
| 376 |
+
**β
High Confidence Normal Result**
|
| 377 |
+
|
| 378 |
+
The model has not detected significant abnormalities consistent with TB, pneumonia, or COVID-19.
|
| 379 |
+
|
| 380 |
+
**Interpretation:**
|
| 381 |
+
- Chest X-ray appears within normal limits
|
| 382 |
+
- No features of active tuberculosis detected
|
| 383 |
+
- No signs of pneumonia or COVID-19
|
| 384 |
+
|
| 385 |
+
**Important Notes:**
|
| 386 |
+
- This does NOT rule out all lung diseases
|
| 387 |
+
- Early-stage diseases may not show on X-ray
|
| 388 |
+
- If you have symptoms, seek medical evaluation
|
| 389 |
+
- Regular health screenings are recommended
|
| 390 |
+
|
| 391 |
+
**When to still see a doctor:**
|
| 392 |
+
- Persistent cough, fever, or respiratory symptoms
|
| 393 |
+
- Unexplained weight loss or night sweats
|
| 394 |
+
- Shortness of breath or chest pain
|
| 395 |
+
- Known exposure to TB or COVID-19
|
| 396 |
+
"""
|
| 397 |
+
else:
|
| 398 |
+
interpretation += """
|
| 399 |
+
**β οΈ Likely Normal, Low Confidence**
|
| 400 |
+
|
| 401 |
+
The model suggests a normal chest X-ray, but confidence is not high.
|
| 402 |
+
|
| 403 |
+
**Recommended Actions:**
|
| 404 |
+
1. If symptomatic, seek medical evaluation
|
| 405 |
+
2. Consider repeat imaging if concerns persist
|
| 406 |
+
3. Clinical correlation is important
|
| 407 |
+
|
| 408 |
+
**Note**: Low confidence results should be reviewed by healthcare professionals.
|
| 409 |
+
"""
|
| 410 |
+
|
| 411 |
+
# Add universal disclaimer
|
| 412 |
+
interpretation += """
|
| 413 |
+
|
| 414 |
+
---
|
| 415 |
+
|
| 416 |
+
## β οΈ CRITICAL MEDICAL DISCLAIMER
|
| 417 |
+
|
| 418 |
+
### Model Capabilities:
|
| 419 |
+
- β
Trained on 4 disease classes: Normal, TB, Pneumonia, COVID-19
|
| 420 |
+
- β
Can distinguish between different lung diseases
|
| 421 |
+
- β
~95-97% accuracy in validation testing
|
| 422 |
+
- β
Powered by Adaptive Sparse Training (89% energy efficient)
|
| 423 |
+
|
| 424 |
+
### Important Limitations:
|
| 425 |
+
- β οΈ This is a **SCREENING tool**, not a diagnostic device
|
| 426 |
+
- β οΈ **NOT FDA-approved** for clinical diagnosis
|
| 427 |
+
- β οΈ Cannot detect: lung cancer, pulmonary fibrosis, bronchiectasis, other rare diseases
|
| 428 |
+
- β οΈ Cannot replace: professional radiologist review
|
| 429 |
+
- β οΈ Cannot confirm: laboratory diagnosis (sputum tests, PCR, cultures)
|
| 430 |
+
|
| 431 |
+
### Clinical Use Guidelines:
|
| 432 |
+
1. β
Use as a **preliminary screening** tool only
|
| 433 |
+
2. β
ALL positive results require **confirmatory laboratory testing**
|
| 434 |
+
3. β
ALL cases require **clinical correlation** with symptoms and history
|
| 435 |
+
4. β
Expert radiologist review is recommended for clinical decisions
|
| 436 |
+
5. β
Do NOT initiate treatment based solely on AI predictions
|
| 437 |
+
|
| 438 |
+
### Diagnostic Gold Standards:
|
| 439 |
+
- **TB**: Sputum AFB smear/culture, GeneXpert MTB/RIF, TB-PCR
|
| 440 |
+
- **Pneumonia**: Clinical diagnosis + sputum culture + blood tests
|
| 441 |
+
- **COVID-19**: RT-PCR, rapid antigen test
|
| 442 |
+
|
| 443 |
+
**When in doubt, always consult a qualified healthcare professional.**
|
| 444 |
+
|
| 445 |
+
---
|
| 446 |
+
|
| 447 |
+
π« **Powered by Adaptive Sparse Training**
|
| 448 |
+
Energy-efficient AI for accessible healthcare
|
| 449 |
+
|
| 450 |
+
**Learn more:**
|
| 451 |
+
- GitHub: https://github.com/oluwafemidiakhoa/Tuberculosis
|
| 452 |
+
- Research: Sample-based Adaptive Sparse Training for deep learning
|
| 453 |
+
"""
|
| 454 |
+
|
| 455 |
+
return interpretation
|
| 456 |
+
|
| 457 |
+
# ============================================================================
|
| 458 |
+
# Gradio Interface
|
| 459 |
+
# ============================================================================
|
| 460 |
+
|
| 461 |
+
# Custom CSS
|
| 462 |
+
custom_css = """
|
| 463 |
+
#main-container {
|
| 464 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 465 |
+
padding: 20px;
|
| 466 |
+
}
|
| 467 |
+
|
| 468 |
+
#title {
|
| 469 |
+
text-align: center;
|
| 470 |
+
color: white;
|
| 471 |
+
font-size: 2.5em;
|
| 472 |
+
font-weight: bold;
|
| 473 |
+
margin-bottom: 10px;
|
| 474 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
| 475 |
+
}
|
| 476 |
+
|
| 477 |
+
#subtitle {
|
| 478 |
+
text-align: center;
|
| 479 |
+
color: #f0f0f0;
|
| 480 |
+
font-size: 1.2em;
|
| 481 |
+
margin-bottom: 20px;
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
#stats {
|
| 485 |
+
text-align: center;
|
| 486 |
+
color: #fff;
|
| 487 |
+
font-size: 0.95em;
|
| 488 |
+
margin-bottom: 30px;
|
| 489 |
+
padding: 15px;
|
| 490 |
+
background: rgba(255,255,255,0.1);
|
| 491 |
+
border-radius: 10px;
|
| 492 |
+
backdrop-filter: blur(10px);
|
| 493 |
+
}
|
| 494 |
+
|
| 495 |
+
.gradio-container {
|
| 496 |
+
font-family: 'Inter', sans-serif;
|
| 497 |
+
}
|
| 498 |
+
|
| 499 |
+
#upload-box {
|
| 500 |
+
border: 3px dashed #667eea;
|
| 501 |
+
border-radius: 15px;
|
| 502 |
+
padding: 20px;
|
| 503 |
+
background: rgba(255,255,255,0.95);
|
| 504 |
+
}
|
| 505 |
+
|
| 506 |
+
#results-box {
|
| 507 |
+
background: white;
|
| 508 |
+
border-radius: 15px;
|
| 509 |
+
padding: 20px;
|
| 510 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 511 |
+
}
|
| 512 |
+
|
| 513 |
+
.output-image {
|
| 514 |
+
border-radius: 10px;
|
| 515 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
footer {
|
| 519 |
+
text-align: center;
|
| 520 |
+
margin-top: 30px;
|
| 521 |
+
color: white;
|
| 522 |
+
font-size: 0.9em;
|
| 523 |
+
}
|
| 524 |
+
"""
|
| 525 |
+
|
| 526 |
+
# Create interface
|
| 527 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 528 |
+
gr.HTML("""
|
| 529 |
+
<div id="main-container">
|
| 530 |
+
<div id="title">π« Multi-Class Chest X-Ray Detection AI</div>
|
| 531 |
+
<div id="subtitle">Advanced chest X-ray analysis with Explainable AI</div>
|
| 532 |
+
<div id="stats">
|
| 533 |
+
<b>95-97% Accuracy</b> across 4 disease classes |
|
| 534 |
+
<b>89% Energy Efficient</b> |
|
| 535 |
+
Powered by Adaptive Sparse Training
|
| 536 |
+
<br><br>
|
| 537 |
+
<b>Detects:</b> Normal β’ Tuberculosis β’ Pneumonia β’ COVID-19
|
| 538 |
+
</div>
|
| 539 |
+
</div>
|
| 540 |
+
""")
|
| 541 |
+
|
| 542 |
+
with gr.Row():
|
| 543 |
+
with gr.Column(scale=1, elem_id="upload-box"):
|
| 544 |
+
gr.Markdown("## π€ Upload Chest X-Ray")
|
| 545 |
+
image_input = gr.Image(
|
| 546 |
+
type="pil",
|
| 547 |
+
label="Upload X-Ray Image",
|
| 548 |
+
elem_classes="output-image"
|
| 549 |
+
)
|
| 550 |
+
|
| 551 |
+
show_gradcam = gr.Checkbox(
|
| 552 |
+
value=True,
|
| 553 |
+
label="Enable Grad-CAM Visualization (Explainable AI)",
|
| 554 |
+
info="Shows which areas the model focuses on"
|
| 555 |
+
)
|
| 556 |
+
|
| 557 |
+
analyze_btn = gr.Button(
|
| 558 |
+
"π¬ Analyze X-Ray",
|
| 559 |
+
variant="primary",
|
| 560 |
+
size="lg"
|
| 561 |
+
)
|
| 562 |
+
|
| 563 |
+
gr.Markdown("""
|
| 564 |
+
### π Supported Images:
|
| 565 |
+
- Chest X-rays (PA or AP view)
|
| 566 |
+
- PNG, JPG, JPEG formats
|
| 567 |
+
- Grayscale or RGB
|
| 568 |
+
|
| 569 |
+
### β‘ What's New:
|
| 570 |
+
- β
**Improved Specificity**: Can distinguish TB from Pneumonia
|
| 571 |
+
- β
**4 Disease Classes**: Normal, TB, Pneumonia, COVID-19
|
| 572 |
+
- β
**Fewer False Positives**: <5% on pneumonia cases
|
| 573 |
+
- β
**Same Energy Efficiency**: 89% savings with AST
|
| 574 |
+
""")
|
| 575 |
+
|
| 576 |
+
with gr.Column(scale=2, elem_id="results-box"):
|
| 577 |
+
gr.Markdown("## π Analysis Results")
|
| 578 |
+
|
| 579 |
+
# Results display
|
| 580 |
+
with gr.Row():
|
| 581 |
+
prob_output = gr.Label(
|
| 582 |
+
label="Prediction Confidence",
|
| 583 |
+
num_top_classes=4
|
| 584 |
+
)
|
| 585 |
+
|
| 586 |
+
with gr.Tabs():
|
| 587 |
+
with gr.Tab("Original"):
|
| 588 |
+
original_output = gr.Image(
|
| 589 |
+
label="Annotated X-Ray",
|
| 590 |
+
elem_classes="output-image"
|
| 591 |
+
)
|
| 592 |
+
|
| 593 |
+
with gr.Tab("Grad-CAM Heatmap"):
|
| 594 |
+
gradcam_output = gr.Image(
|
| 595 |
+
label="Attention Heatmap",
|
| 596 |
+
elem_classes="output-image"
|
| 597 |
+
)
|
| 598 |
+
|
| 599 |
+
with gr.Tab("Overlay"):
|
| 600 |
+
overlay_output = gr.Image(
|
| 601 |
+
label="Explainable AI Visualization",
|
| 602 |
+
elem_classes="output-image"
|
| 603 |
+
)
|
| 604 |
+
|
| 605 |
+
interpretation_output = gr.Markdown(
|
| 606 |
+
label="Clinical Interpretation"
|
| 607 |
+
)
|
| 608 |
+
|
| 609 |
+
# Example images
|
| 610 |
+
gr.Markdown("## π Example X-Rays")
|
| 611 |
+
gr.Examples(
|
| 612 |
+
examples=[
|
| 613 |
+
["examples/normal.png"],
|
| 614 |
+
["examples/tb.png"],
|
| 615 |
+
["examples/pneumonia.png"],
|
| 616 |
+
["examples/covid.png"],
|
| 617 |
+
],
|
| 618 |
+
inputs=image_input,
|
| 619 |
+
label="Click to load example"
|
| 620 |
+
)
|
| 621 |
+
|
| 622 |
+
# Connect components
|
| 623 |
+
analyze_btn.click(
|
| 624 |
+
fn=predict_chest_xray,
|
| 625 |
+
inputs=[image_input, show_gradcam],
|
| 626 |
+
outputs=[prob_output, original_output, gradcam_output, overlay_output, interpretation_output]
|
| 627 |
+
)
|
| 628 |
+
|
| 629 |
+
# Footer
|
| 630 |
+
gr.HTML("""
|
| 631 |
+
<footer>
|
| 632 |
+
<p>
|
| 633 |
+
<b>π« Multi-Class Chest X-Ray Detection with AST</b><br>
|
| 634 |
+
Trained on Normal, Tuberculosis, Pneumonia, and COVID-19 cases<br>
|
| 635 |
+
95-97% Accuracy | 89% Energy Savings | Explainable AI<br><br>
|
| 636 |
+
<a href="https://github.com/oluwafemidiakhoa/Tuberculosis" target="_blank" style="color: white;">
|
| 637 |
+
π GitHub Repository
|
| 638 |
+
</a> |
|
| 639 |
+
<a href="https://huggingface.co/spaces/mgbam/Tuberculosis" target="_blank" style="color: white;">
|
| 640 |
+
π€ Hugging Face Space
|
| 641 |
+
</a>
|
| 642 |
+
</p>
|
| 643 |
+
<p style="font-size: 0.8em; margin-top: 15px;">
|
| 644 |
+
β οΈ <b>MEDICAL DISCLAIMER</b>: This is a screening tool, not a diagnostic device.
|
| 645 |
+
All predictions require professional medical evaluation and laboratory confirmation.
|
| 646 |
+
Not FDA-approved for clinical use.
|
| 647 |
+
</p>
|
| 648 |
+
</footer>
|
| 649 |
+
""")
|
| 650 |
+
|
| 651 |
+
# Launch
|
| 652 |
+
if __name__ == "__main__":
|
| 653 |
+
demo.launch(
|
| 654 |
+
share=False,
|
| 655 |
+
server_name="0.0.0.0",
|
| 656 |
+
server_port=7860,
|
| 657 |
+
show_error=True
|
| 658 |
+
)
|