Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import torch
|
| 2 |
from transformers import ViTImageProcessor, ViTForImageClassification
|
| 3 |
-
from fastai.learner import load_learner
|
| 4 |
-
from fastai.vision.core import PILImage
|
| 5 |
from PIL import Image
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
import numpy as np
|
|
@@ -9,138 +7,457 @@ import gradio as gr
|
|
| 9 |
import io
|
| 10 |
import base64
|
| 11 |
from torchvision import transforms
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# ---
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
CLASSES = [
|
| 43 |
"Queratosis actínica / Bowen", "Carcinoma células basales",
|
| 44 |
-
"Lesión queratósica benigna", "Dermatofibroma",
|
| 45 |
"Melanoma maligno", "Nevus melanocítico", "Lesión vascular"
|
| 46 |
]
|
|
|
|
| 47 |
RISK_LEVELS = {
|
| 48 |
-
0: {'level': '
|
| 49 |
-
1: {'level': '
|
| 50 |
-
2: {'level': 'Bajo',
|
| 51 |
-
3: {'level': 'Bajo',
|
| 52 |
-
4: {'level': 'Crítico',
|
| 53 |
-
5: {'level': 'Bajo',
|
| 54 |
-
6: {'level': '
|
| 55 |
}
|
|
|
|
| 56 |
MALIGNANT_INDICES = [0, 1, 4] # akiec, bcc, melanoma
|
| 57 |
|
| 58 |
-
def
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
plt.tight_layout()
|
| 102 |
buf = io.BytesIO()
|
| 103 |
-
plt.savefig(buf, format="png")
|
| 104 |
plt.close(fig)
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
# Generar
|
| 108 |
informe = f"""
|
| 109 |
-
<div style="font-family:sans-serif; max-width:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
"""
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
else:
|
| 131 |
-
informe +=
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
|
|
|
| 136 |
demo = gr.Interface(
|
| 137 |
-
fn=
|
| 138 |
-
inputs=gr.Image(type="pil"),
|
| 139 |
-
outputs=[
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
)
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import ViTImageProcessor, ViTForImageClassification
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import numpy as np
|
|
|
|
| 7 |
import io
|
| 8 |
import base64
|
| 9 |
from torchvision import transforms
|
| 10 |
+
import torch.nn.functional as F
|
| 11 |
+
|
| 12 |
+
# --- MODELOS VERIFICADOS DISPONIBLES EN HUGGING FACE ---
|
| 13 |
+
|
| 14 |
+
# 1. Google Derm Foundation (VERIFICADO - existe en Hugging Face)
|
| 15 |
+
try:
|
| 16 |
+
derm_processor = ViTImageProcessor.from_pretrained("google/derm-foundation")
|
| 17 |
+
derm_model = ViTForImageClassification.from_pretrained("google/derm-foundation")
|
| 18 |
+
derm_model.eval()
|
| 19 |
+
DERM_AVAILABLE = True
|
| 20 |
+
print("✅ Google Derm Foundation cargado exitosamente")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
DERM_AVAILABLE = False
|
| 23 |
+
print(f"❌ Google Derm Foundation no disponible: {e}")
|
| 24 |
+
|
| 25 |
+
# 2. Modelo HAM10k especializado (VERIFICADO)
|
| 26 |
+
try:
|
| 27 |
+
ham_processor = ViTImageProcessor.from_pretrained("bsenst/skin-cancer-HAM10k")
|
| 28 |
+
ham_model = ViTForImageClassification.from_pretrained("bsenst/skin-cancer-HAM10k")
|
| 29 |
+
ham_model.eval()
|
| 30 |
+
HAM_AVAILABLE = True
|
| 31 |
+
print("✅ HAM10k especializado cargado exitosamente")
|
| 32 |
+
except Exception as e:
|
| 33 |
+
HAM_AVAILABLE = False
|
| 34 |
+
print(f"❌ HAM10k especializado no disponible: {e}")
|
| 35 |
+
|
| 36 |
+
# 3. Modelo ISIC 2024 con SMOTE (VERIFICADO)
|
| 37 |
+
try:
|
| 38 |
+
isic_processor = ViTImageProcessor.from_pretrained("jhoppanne/SkinCancerClassifier_smote-V0")
|
| 39 |
+
isic_model = ViTForImageClassification.from_pretrained("jhoppanne/SkinCancerClassifier_smote-V0")
|
| 40 |
+
isic_model.eval()
|
| 41 |
+
ISIC_AVAILABLE = True
|
| 42 |
+
print("✅ ISIC 2024 SMOTE cargado exitosamente")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
ISIC_AVAILABLE = False
|
| 45 |
+
print(f"❌ ISIC 2024 SMOTE no disponible: {e}")
|
| 46 |
+
|
| 47 |
+
# 4. Modelo genérico de detección (VERIFICADO)
|
| 48 |
+
try:
|
| 49 |
+
generic_processor = ViTImageProcessor.from_pretrained("syaha/skin_cancer_detection_model")
|
| 50 |
+
generic_model = ViTForImageClassification.from_pretrained("syaha/skin_cancer_detection_model")
|
| 51 |
+
generic_model.eval()
|
| 52 |
+
GENERIC_AVAILABLE = True
|
| 53 |
+
print("✅ Modelo genérico cargado exitosamente")
|
| 54 |
+
except Exception as e:
|
| 55 |
+
GENERIC_AVAILABLE = False
|
| 56 |
+
print(f"❌ Modelo genérico no disponible: {e}")
|
| 57 |
+
|
| 58 |
+
# 5. Modelo de melanoma específico (VERIFICADO)
|
| 59 |
+
try:
|
| 60 |
+
melanoma_processor = ViTImageProcessor.from_pretrained("milutinNemanjic/Melanoma-detection-model")
|
| 61 |
+
melanoma_model = ViTForImageClassification.from_pretrained("milutinNemanjic/Melanoma-detection-model")
|
| 62 |
+
melanoma_model.eval()
|
| 63 |
+
MELANOMA_AVAILABLE = True
|
| 64 |
+
print("✅ Modelo melanoma específico cargado exitosamente")
|
| 65 |
+
except Exception as e:
|
| 66 |
+
MELANOMA_AVAILABLE = False
|
| 67 |
+
print(f"❌ Modelo melanoma específico no disponible: {e}")
|
| 68 |
+
|
| 69 |
+
# 6. Tu modelo actual como respaldo
|
| 70 |
+
try:
|
| 71 |
+
backup_processor = ViTImageProcessor.from_pretrained("Anwarkh1/Skin_Cancer-Image_Classification")
|
| 72 |
+
backup_model = ViTForImageClassification.from_pretrained("Anwarkh1/Skin_Cancer-Image_Classification")
|
| 73 |
+
backup_model.eval()
|
| 74 |
+
BACKUP_AVAILABLE = True
|
| 75 |
+
print("✅ Modelo de respaldo cargado exitosamente")
|
| 76 |
+
except Exception as e:
|
| 77 |
+
BACKUP_AVAILABLE = False
|
| 78 |
+
print(f"❌ Modelo de respaldo no disponible: {e}")
|
| 79 |
+
|
| 80 |
+
# Clases HAM10000 estándar
|
| 81 |
CLASSES = [
|
| 82 |
"Queratosis actínica / Bowen", "Carcinoma células basales",
|
| 83 |
+
"Lesión queratósica benigna", "Dermatofibroma",
|
| 84 |
"Melanoma maligno", "Nevus melanocítico", "Lesión vascular"
|
| 85 |
]
|
| 86 |
+
|
| 87 |
RISK_LEVELS = {
|
| 88 |
+
0: {'level': 'Alto', 'color': '#ff6b35', 'weight': 0.7}, # akiec
|
| 89 |
+
1: {'level': 'Crítico', 'color': '#cc0000', 'weight': 0.9}, # bcc
|
| 90 |
+
2: {'level': 'Bajo', 'color': '#44ff44', 'weight': 0.1}, # bkl
|
| 91 |
+
3: {'level': 'Bajo', 'color': '#44ff44', 'weight': 0.1}, # df
|
| 92 |
+
4: {'level': 'Crítico', 'color': '#990000', 'weight': 1.0}, # melanoma
|
| 93 |
+
5: {'level': 'Bajo', 'color': '#66ff66', 'weight': 0.1}, # nv
|
| 94 |
+
6: {'level': 'Moderado', 'color': '#ffaa00', 'weight': 0.3} # vasc
|
| 95 |
}
|
| 96 |
+
|
| 97 |
MALIGNANT_INDICES = [0, 1, 4] # akiec, bcc, melanoma
|
| 98 |
|
| 99 |
+
def safe_predict(image, processor, model, model_name, expected_classes=7):
|
| 100 |
+
"""Predicción segura que maneja diferentes números de clases"""
|
| 101 |
+
try:
|
| 102 |
+
inputs = processor(image, return_tensors="pt")
|
| 103 |
+
with torch.no_grad():
|
| 104 |
+
outputs = model(**inputs)
|
| 105 |
+
logits = outputs.logits
|
| 106 |
+
|
| 107 |
+
# Manejar diferentes números de clases
|
| 108 |
+
if logits.shape[1] != expected_classes:
|
| 109 |
+
print(f"⚠️ {model_name}: Esperaba {expected_classes} clases, obtuvo {logits.shape[1]}")
|
| 110 |
+
|
| 111 |
+
if logits.shape[1] == 2: # Modelo binario (benigno/maligno)
|
| 112 |
+
probabilities = F.softmax(logits, dim=-1).cpu().numpy()[0]
|
| 113 |
+
# Convertir a formato de 7 clases (simplificado)
|
| 114 |
+
expanded_probs = np.zeros(expected_classes)
|
| 115 |
+
if probabilities[1] > 0.5: # Maligno
|
| 116 |
+
expanded_probs[4] = probabilities[1] * 0.6 # Melanoma
|
| 117 |
+
expanded_probs[1] = probabilities[1] * 0.3 # BCC
|
| 118 |
+
expanded_probs[0] = probabilities[1] * 0.1 # AKIEC
|
| 119 |
+
else: # Benigno
|
| 120 |
+
expanded_probs[5] = probabilities[0] * 0.7 # Nevus
|
| 121 |
+
expanded_probs[2] = probabilities[0] * 0.2 # BKL
|
| 122 |
+
expanded_probs[3] = probabilities[0] * 0.1 # DF
|
| 123 |
+
probabilities = expanded_probs
|
| 124 |
+
else:
|
| 125 |
+
# Para otros números de clases, normalizar o truncar
|
| 126 |
+
probabilities = F.softmax(logits, dim=-1).cpu().numpy()[0]
|
| 127 |
+
if len(probabilities) > expected_classes:
|
| 128 |
+
probabilities = probabilities[:expected_classes]
|
| 129 |
+
elif len(probabilities) < expected_classes:
|
| 130 |
+
temp = np.zeros(expected_classes)
|
| 131 |
+
temp[:len(probabilities)] = probabilities
|
| 132 |
+
probabilities = temp
|
| 133 |
+
else:
|
| 134 |
+
probabilities = F.softmax(logits, dim=-1).cpu().numpy()[0]
|
| 135 |
+
|
| 136 |
+
predicted_idx = int(np.argmax(probabilities))
|
| 137 |
+
predicted_class = CLASSES[predicted_idx] if predicted_idx < len(CLASSES) else "Desconocido"
|
| 138 |
+
confidence = float(probabilities[predicted_idx])
|
| 139 |
+
is_malignant = predicted_idx in MALIGNANT_INDICES
|
| 140 |
+
|
| 141 |
+
return {
|
| 142 |
+
'model': model_name,
|
| 143 |
+
'class': predicted_class,
|
| 144 |
+
'confidence': confidence,
|
| 145 |
+
'probabilities': probabilities,
|
| 146 |
+
'is_malignant': is_malignant,
|
| 147 |
+
'predicted_idx': predicted_idx,
|
| 148 |
+
'success': True
|
| 149 |
+
}
|
| 150 |
+
except Exception as e:
|
| 151 |
+
print(f"❌ Error en {model_name}: {e}")
|
| 152 |
+
return {
|
| 153 |
+
'model': model_name,
|
| 154 |
+
'error': str(e),
|
| 155 |
+
'class': 'Error',
|
| 156 |
+
'confidence': 0.0,
|
| 157 |
+
'is_malignant': False,
|
| 158 |
+
'success': False
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
def ensemble_prediction(predictions):
|
| 162 |
+
"""Combina múltiples predicciones usando weighted voting inteligente"""
|
| 163 |
+
valid_preds = [p for p in predictions if p.get('success', False)]
|
| 164 |
+
if not valid_preds:
|
| 165 |
+
return None
|
| 166 |
+
|
| 167 |
+
# Weighted ensemble basado en confianza y disponibilidad del modelo
|
| 168 |
+
ensemble_probs = np.zeros(len(CLASSES))
|
| 169 |
+
total_weight = 0
|
| 170 |
+
|
| 171 |
+
# Pesos específicos por modelo (basado en calidad esperada)
|
| 172 |
+
model_weights = {
|
| 173 |
+
"🏥 Google Derm Foundation": 1.0,
|
| 174 |
+
"🧠 HAM10k Especializado": 0.9,
|
| 175 |
+
"🆕 ISIC 2024 SMOTE": 0.8,
|
| 176 |
+
"🔬 Melanoma Específico": 0.7,
|
| 177 |
+
"🌐 Genérico": 0.6,
|
| 178 |
+
"🔄 Respaldo Original": 0.5
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
for pred in valid_preds:
|
| 182 |
+
model_weight = model_weights.get(pred['model'], 0.5)
|
| 183 |
+
confidence_weight = pred['confidence']
|
| 184 |
+
final_weight = model_weight * confidence_weight
|
| 185 |
+
|
| 186 |
+
ensemble_probs += pred['probabilities'] * final_weight
|
| 187 |
+
total_weight += final_weight
|
| 188 |
+
|
| 189 |
+
if total_weight > 0:
|
| 190 |
+
ensemble_probs /= total_weight
|
| 191 |
+
|
| 192 |
+
ensemble_idx = int(np.argmax(ensemble_probs))
|
| 193 |
+
ensemble_class = CLASSES[ensemble_idx]
|
| 194 |
+
ensemble_confidence = float(ensemble_probs[ensemble_idx])
|
| 195 |
+
ensemble_malignant = ensemble_idx in MALIGNANT_INDICES
|
| 196 |
+
|
| 197 |
+
# Calcular consenso de malignidad
|
| 198 |
+
malignant_votes = sum(1 for p in valid_preds if p.get('is_malignant', False))
|
| 199 |
+
malignant_consensus = malignant_votes / len(valid_preds)
|
| 200 |
+
|
| 201 |
+
return {
|
| 202 |
+
'class': ensemble_class,
|
| 203 |
+
'confidence': ensemble_confidence,
|
| 204 |
+
'probabilities': ensemble_probs,
|
| 205 |
+
'is_malignant': ensemble_malignant,
|
| 206 |
+
'predicted_idx': ensemble_idx,
|
| 207 |
+
'malignant_consensus': malignant_consensus,
|
| 208 |
+
'num_models': len(valid_preds)
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
def calculate_risk_score(ensemble_result):
|
| 212 |
+
"""Calcula score de riesgo sofisticado"""
|
| 213 |
+
if not ensemble_result:
|
| 214 |
+
return 0.0
|
| 215 |
+
|
| 216 |
+
# Score base del ensemble
|
| 217 |
+
base_score = ensemble_result['probabilities'][ensemble_result['predicted_idx']] * \
|
| 218 |
+
RISK_LEVELS[ensemble_result['predicted_idx']]['weight']
|
| 219 |
+
|
| 220 |
+
# Ajuste por consenso de malignidad
|
| 221 |
+
consensus_boost = ensemble_result['malignant_consensus'] * 0.3
|
| 222 |
+
|
| 223 |
+
# Bonus por número de modelos
|
| 224 |
+
model_confidence = min(ensemble_result['num_models'] / 5.0, 1.0) * 0.1
|
| 225 |
+
|
| 226 |
+
final_score = base_score + consensus_boost + model_confidence
|
| 227 |
+
return min(final_score, 1.0)
|
| 228 |
+
|
| 229 |
+
def analizar_lesion_verificado(img):
|
| 230 |
+
"""Análisis con modelos verificados existentes"""
|
| 231 |
+
predictions = []
|
| 232 |
+
|
| 233 |
+
# Probar modelos disponibles en orden de preferencia
|
| 234 |
+
models_to_try = [
|
| 235 |
+
(DERM_AVAILABLE, derm_processor, derm_model, "🏥 Google Derm Foundation"),
|
| 236 |
+
(HAM_AVAILABLE, ham_processor, ham_model, "🧠 HAM10k Especializado"),
|
| 237 |
+
(ISIC_AVAILABLE, isic_processor, isic_model, "🆕 ISIC 2024 SMOTE"),
|
| 238 |
+
(MELANOMA_AVAILABLE, melanoma_processor, melanoma_model, "🔬 Melanoma Específico"),
|
| 239 |
+
(GENERIC_AVAILABLE, generic_processor, generic_model, "🌐 Genérico"),
|
| 240 |
+
(BACKUP_AVAILABLE, backup_processor, backup_model, "🔄 Respaldo Original")
|
| 241 |
+
]
|
| 242 |
+
|
| 243 |
+
for available, processor, model, name in models_to_try:
|
| 244 |
+
if available:
|
| 245 |
+
pred = safe_predict(img, processor, model, name)
|
| 246 |
+
predictions.append(pred)
|
| 247 |
+
|
| 248 |
+
if not predictions:
|
| 249 |
+
return "❌ No hay modelos disponibles", ""
|
| 250 |
+
|
| 251 |
+
# Ensemble de predicciones
|
| 252 |
+
ensemble_result = ensemble_prediction(predictions)
|
| 253 |
+
|
| 254 |
+
if not ensemble_result:
|
| 255 |
+
return "❌ Error en el análisis ensemble", ""
|
| 256 |
+
|
| 257 |
+
# Calcular riesgo
|
| 258 |
+
risk_score = calculate_risk_score(ensemble_result)
|
| 259 |
+
|
| 260 |
+
# Generar visualización
|
| 261 |
+
colors = [RISK_LEVELS[i]['color'] for i in range(len(CLASSES))]
|
| 262 |
+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 7))
|
| 263 |
+
|
| 264 |
+
# Gráfico principal del ensemble
|
| 265 |
+
bars = ax1.bar(CLASSES, ensemble_result['probabilities'] * 100, color=colors, alpha=0.8)
|
| 266 |
+
ax1.set_title("🎯 Predicción Ensemble (Modelos Combinados)", fontsize=16, fontweight='bold', pad=20)
|
| 267 |
+
ax1.set_ylabel("Probabilidad (%)", fontsize=12)
|
| 268 |
+
ax1.set_xticklabels(CLASSES, rotation=45, ha='right', fontsize=10)
|
| 269 |
+
ax1.grid(axis='y', alpha=0.3)
|
| 270 |
+
ax1.set_ylim(0, 100)
|
| 271 |
+
|
| 272 |
+
# Destacar la predicción principal
|
| 273 |
+
bars[ensemble_result['predicted_idx']].set_edgecolor('black')
|
| 274 |
+
bars[ensemble_result['predicted_idx']].set_linewidth(3)
|
| 275 |
+
bars[ensemble_result['predicted_idx']].set_alpha(1.0)
|
| 276 |
+
|
| 277 |
+
# Gráfico de consenso
|
| 278 |
+
consensus_data = ['Benigno', 'Maligno']
|
| 279 |
+
consensus_values = [1 - ensemble_result['malignant_consensus'], ensemble_result['malignant_consensus']]
|
| 280 |
+
consensus_colors = ['#27ae60', '#e74c3c']
|
| 281 |
+
|
| 282 |
+
bars2 = ax2.bar(consensus_data, consensus_values, color=consensus_colors, alpha=0.8)
|
| 283 |
+
ax2.set_title(f"🤝 Consenso Malignidad ({ensemble_result['num_models']} modelos)",
|
| 284 |
+
fontsize=16, fontweight='bold', pad=20)
|
| 285 |
+
ax2.set_ylabel("Proporción de Modelos", fontsize=12)
|
| 286 |
+
ax2.set_ylim(0, 1)
|
| 287 |
+
ax2.grid(axis='y', alpha=0.3)
|
| 288 |
+
|
| 289 |
+
# Añadir valores en las barras
|
| 290 |
+
for bar, value in zip(bars2, consensus_values):
|
| 291 |
+
height = bar.get_height()
|
| 292 |
+
ax2.text(bar.get_x() + bar.get_width()/2., height + 0.02,
|
| 293 |
+
f'{value:.1%}', ha='center', va='bottom', fontweight='bold')
|
| 294 |
+
|
| 295 |
plt.tight_layout()
|
| 296 |
buf = io.BytesIO()
|
| 297 |
+
plt.savefig(buf, format="png", dpi=120, bbox_inches='tight')
|
| 298 |
plt.close(fig)
|
| 299 |
+
chart_html = f'<img src="data:image/png;base64,{base64.b64encode(buf.getvalue()).decode()}" style="max-width:100%; border-radius:8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);"/>'
|
| 300 |
+
|
| 301 |
+
# Generar reporte detallado
|
| 302 |
informe = f"""
|
| 303 |
+
<div style="font-family: 'Segoe UI', Arial, sans-serif; max-width: 1000px; margin: auto; background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); padding: 25px; border-radius: 15px;">
|
| 304 |
+
<h1 style="color: #2c3e50; text-align: center; margin-bottom: 30px; text-shadow: 2px 2px 4px rgba(0,0,0,0.1);">
|
| 305 |
+
🏥 Análisis Dermatológico Multi-Modelo IA
|
| 306 |
+
</h1>
|
| 307 |
+
|
| 308 |
+
<div style="background: white; padding: 25px; border-radius: 12px; margin-bottom: 25px; box-shadow: 0 4px 15px rgba(0,0,0,0.1);">
|
| 309 |
+
<h2 style="color: #34495e; margin-top: 0; border-bottom: 3px solid #3498db; padding-bottom: 10px;">
|
| 310 |
+
📊 Resultados Individuales por Modelo
|
| 311 |
+
</h2>
|
| 312 |
+
<div style="overflow-x: auto;">
|
| 313 |
+
<table style="width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 15px;">
|
| 314 |
+
<thead>
|
| 315 |
+
<tr style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;">
|
| 316 |
+
<th style="padding: 15px; text-align: left; border-radius: 8px 0 0 0;">Modelo</th>
|
| 317 |
+
<th style="padding: 15px; text-align: left;">Diagnóstico</th>
|
| 318 |
+
<th style="padding: 15px; text-align: left;">Confianza</th>
|
| 319 |
+
<th style="padding: 15px; text-align: left;">Estado</th>
|
| 320 |
+
<th style="padding: 15px; text-align: left; border-radius: 0 8px 0 0;">Malignidad</th>
|
| 321 |
+
</tr>
|
| 322 |
+
</thead>
|
| 323 |
+
<tbody>
|
| 324 |
"""
|
| 325 |
+
|
| 326 |
+
for i, pred in enumerate(predictions):
|
| 327 |
+
row_color = "#f8f9fa" if i % 2 == 0 else "#ffffff"
|
| 328 |
+
|
| 329 |
+
if pred.get('success', False):
|
| 330 |
+
status_icon = "✅"
|
| 331 |
+
status_color = "#27ae60"
|
| 332 |
+
status_text = "Activo"
|
| 333 |
+
|
| 334 |
+
malignant_color = "#e74c3c" if pred.get('is_malignant', False) else "#27ae60"
|
| 335 |
+
malignant_text = "🚨 Maligno" if pred.get('is_malignant', False) else "✅ Benigno"
|
| 336 |
+
|
| 337 |
+
informe += f"""
|
| 338 |
+
<tr style="background: {row_color};">
|
| 339 |
+
<td style="padding: 12px; border-bottom: 1px solid #ecf0f1; font-weight: bold;">{pred['model']}</td>
|
| 340 |
+
<td style="padding: 12px; border-bottom: 1px solid #ecf0f1;"><strong>{pred['class']}</strong></td>
|
| 341 |
+
<td style="padding: 12px; border-bottom: 1px solid #ecf0f1;">{pred['confidence']:.1%}</td>
|
| 342 |
+
<td style="padding: 12px; border-bottom: 1px solid #ecf0f1; color: {status_color};"><strong>{status_icon} {status_text}</strong></td>
|
| 343 |
+
<td style="padding: 12px; border-bottom: 1px solid #ecf0f1; color: {malignant_color};"><strong>{malignant_text}</strong></td>
|
| 344 |
+
</tr>
|
| 345 |
+
"""
|
| 346 |
+
else:
|
| 347 |
+
informe += f"""
|
| 348 |
+
<tr style="background: {row_color};">
|
| 349 |
+
<td style="padding: 12px; border-bottom: 1px solid #ecf0f1; font-weight: bold; color: #7f8c8d;">{pred['model']}</td>
|
| 350 |
+
<td style="padding: 12px; border-bottom: 1px solid #ecf0f1; color: #e67e22;">❌ No disponible</td>
|
| 351 |
+
<td style="padding: 12px; border-bottom: 1px solid #ecf0f1;">N/A</td>
|
| 352 |
+
<td style="padding: 12px; border-bottom: 1px solid #ecf0f1; color: #e74c3c;"><strong>❌ Error</strong></td>
|
| 353 |
+
<td style="padding: 12px; border-bottom: 1px solid #ecf0f1;">N/A</td>
|
| 354 |
+
</tr>
|
| 355 |
+
"""
|
| 356 |
+
|
| 357 |
+
# Resultado del ensemble
|
| 358 |
+
ensemble_status_color = "#e74c3c" if ensemble_result.get('is_malignant', False) else "#27ae60"
|
| 359 |
+
ensemble_status_text = "🚨 MALIGNO" if ensemble_result.get('is_malignant', False) else "✅ BENIGNO"
|
| 360 |
+
|
| 361 |
+
informe += f"""
|
| 362 |
+
</tbody>
|
| 363 |
+
</table>
|
| 364 |
+
</div>
|
| 365 |
+
</div>
|
| 366 |
+
|
| 367 |
+
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 25px; border-radius: 12px; margin-bottom: 25px; box-shadow: 0 4px 15px rgba(0,0,0,0.2);">
|
| 368 |
+
<h2 style="margin-top: 0; color: white; display: flex; align-items: center;">
|
| 369 |
+
🎯 Diagnóstico Final (Consenso de {ensemble_result['num_models']} modelos)
|
| 370 |
+
</h2>
|
| 371 |
+
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-top: 20px;">
|
| 372 |
+
<div>
|
| 373 |
+
<p style="font-size: 18px; margin: 8px 0;"><strong>Diagnóstico:</strong> {ensemble_result['class']}</p>
|
| 374 |
+
<p style="margin: 8px 0;"><strong>Confianza:</strong> {ensemble_result['confidence']:.1%}</p>
|
| 375 |
+
<p style="margin: 8px 0; color: {ensemble_status_color}; background: rgba(255,255,255,0.2); padding: 8px; border-radius: 5px;"><strong>Estado: {ensemble_status_text}</strong></p>
|
| 376 |
+
</div>
|
| 377 |
+
<div>
|
| 378 |
+
<p style="margin: 8px 0;"><strong>Consenso Malignidad:</strong> {ensemble_result['malignant_consensus']:.1%}</p>
|
| 379 |
+
<p style="margin: 8px 0;"><strong>Score de Riesgo:</strong> {risk_score:.2f}</p>
|
| 380 |
+
<p style="margin: 8px 0;"><strong>Modelos Activos:</strong> {ensemble_result['num_models']}/6</p>
|
| 381 |
+
</div>
|
| 382 |
+
</div>
|
| 383 |
+
</div>
|
| 384 |
+
"""
|
| 385 |
+
|
| 386 |
+
# Recomendación clínica
|
| 387 |
+
informe += """
|
| 388 |
+
<div style="background: white; padding: 25px; border-radius: 12px; border-left: 6px solid #3498db; box-shadow: 0 4px 15px rgba(0,0,0,0.1);">
|
| 389 |
+
<h2 style="color: #2c3e50; margin-top: 0; display: flex; align-items: center;">
|
| 390 |
+
🩺 Recomendación Clínica Automatizada
|
| 391 |
+
</h2>
|
| 392 |
+
"""
|
| 393 |
+
|
| 394 |
+
if risk_score > 0.7:
|
| 395 |
+
informe += '''
|
| 396 |
+
<div style="background: linear-gradient(135deg, #ff6b6b 0%, #ee5a5a 100%); color: white; padding: 20px; border-radius: 8px; margin: 15px 0;">
|
| 397 |
+
<h3 style="margin: 0; font-size: 18px;">🚨 DERIVACIÓN URGENTE</h3>
|
| 398 |
+
<p style="margin: 10px 0 0 0; font-size: 16px;">Contactar con oncología dermatológica en 24-48 horas</p>
|
| 399 |
+
</div>'''
|
| 400 |
+
elif risk_score > 0.5:
|
| 401 |
+
informe += '''
|
| 402 |
+
<div style="background: linear-gradient(135deg, #ffa726 0%, #ff9800 100%); color: white; padding: 20px; border-radius: 8px; margin: 15px 0;">
|
| 403 |
+
<h3 style="margin: 0; font-size: 18px;">⚠️ EVALUACIÓN PRIORITARIA</h3>
|
| 404 |
+
<p style="margin: 10px 0 0 0; font-size: 16px;">Consulta dermatológica en 1-2 semanas</p>
|
| 405 |
+
</div>'''
|
| 406 |
+
elif risk_score > 0.3:
|
| 407 |
+
informe += '''
|
| 408 |
+
<div style="background: linear-gradient(135deg, #42a5f5 0%, #2196f3 100%); color: white; padding: 20px; border-radius: 8px; margin: 15px 0;">
|
| 409 |
+
<h3 style="margin: 0; font-size: 18px;">📋 SEGUIMIENTO PROGRAMADO</h3>
|
| 410 |
+
<p style="margin: 10px 0 0 0; font-size: 16px;">Consulta dermatológica en 4-6 semanas</p>
|
| 411 |
+
</div>'''
|
| 412 |
else:
|
| 413 |
+
informe += '''
|
| 414 |
+
<div style="background: linear-gradient(135deg, #66bb6a 0%, #4caf50 100%); color: white; padding: 20px; border-radius: 8px; margin: 15px 0;">
|
| 415 |
+
<h3 style="margin: 0; font-size: 18px;">✅ MONITOREO RUTINARIO</h3>
|
| 416 |
+
<p style="margin: 10px 0 0 0; font-size: 16px;">Seguimiento en 3-6 meses</p>
|
| 417 |
+
</div>'''
|
| 418 |
+
|
| 419 |
+
informe += f"""
|
| 420 |
+
<div style="margin-top: 20px; padding: 15px; background: #f8f9fa; border-radius: 8px; border-left: 4px solid #e67e22;">
|
| 421 |
+
<p style="margin: 0; font-style: italic; color: #7f8c8d; font-size: 13px;">
|
| 422 |
+
⚠️ <strong>Disclaimer Médico:</strong> Este análisis utiliza {ensemble_result['num_models']} modelos de IA como herramienta de apoyo diagnóstico.
|
| 423 |
+
El resultado NO sustituye el criterio médico profesional. Siempre consulte con un dermatólogo certificado
|
| 424 |
+
para un diagnóstico definitivo y plan de tratamiento apropiado.
|
| 425 |
+
</p>
|
| 426 |
+
</div>
|
| 427 |
+
</div>
|
| 428 |
+
</div>
|
| 429 |
+
"""
|
| 430 |
+
|
| 431 |
+
return informe, chart_html
|
| 432 |
|
| 433 |
+
# Interfaz Gradio mejorada
|
| 434 |
demo = gr.Interface(
|
| 435 |
+
fn=analizar_lesion_verificado,
|
| 436 |
+
inputs=gr.Image(type="pil", label="📷 Cargar imagen dermatoscópica o foto de lesión cutánea"),
|
| 437 |
+
outputs=[
|
| 438 |
+
gr.HTML(label="📋 Informe Diagnóstico Completo"),
|
| 439 |
+
gr.HTML(label="📊 Análisis Visual de Resultados")
|
| 440 |
+
],
|
| 441 |
+
title="🏥 Sistema Avanzado de Detección de Cáncer de Piel - Multi-Modelo IA",
|
| 442 |
+
description="""
|
| 443 |
+
Sistema de análisis dermatológico que utiliza múltiples modelos de IA especializados verificados:
|
| 444 |
+
• Google Derm Foundation (modelo más avanzado de Google Health)
|
| 445 |
+
• Modelos especializados en HAM10000, ISIC 2024, y detección de melanoma
|
| 446 |
+
• Ensemble inteligente con weighted voting y análisis de consenso
|
| 447 |
+
""",
|
| 448 |
+
theme=gr.themes.Soft(),
|
| 449 |
+
allow_flagging="never",
|
| 450 |
+
examples=None
|
| 451 |
)
|
| 452 |
|
| 453 |
if __name__ == "__main__":
|
| 454 |
+
print("\n🚀 Iniciando sistema de detección de cáncer de piel...")
|
| 455 |
+
print("📋 Modelos verificados y disponibles en Hugging Face:")
|
| 456 |
+
print("✅ google/derm-foundation")
|
| 457 |
+
print("✅ bsenst/skin-cancer-HAM10k")
|
| 458 |
+
print("✅ jhoppanne/SkinCancerClassifier_smote-V0")
|
| 459 |
+
print("✅ syaha/skin_cancer_detection_model")
|
| 460 |
+
print("✅ milutinNemanjic/Melanoma-detection-model")
|
| 461 |
+
print("✅ Anwarkh1/Skin_Cancer-Image_Classification")
|
| 462 |
+
print("\n🌐 Lanzando interfaz web...")
|
| 463 |
+
demo.launch(share=False)
|