Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,632 +1,67 @@
|
|
1 |
-
|
2 |
-
from transformers import ViTImageProcessor, ViTForImageClassification, AutoImageProcessor, AutoModelForImageClassification
|
3 |
-
from PIL import Image
|
4 |
-
import matplotlib.pyplot as plt
|
5 |
-
import numpy as np
|
6 |
-
import gradio as gr
|
7 |
-
import io
|
8 |
-
import base64
|
9 |
-
import torch.nn.functional as F
|
10 |
-
import warnings
|
11 |
-
import os
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
# --- CONFIGURACIÓN DE MODELOS VERIFICADOS ---
|
19 |
-
# Modelos que realmente existen y funcionan en HuggingFace
|
20 |
-
MODEL_CONFIGS = [
|
21 |
-
# Modelos específicos de cáncer de piel VERIFICADOS
|
22 |
-
{
|
23 |
-
'name': 'Syaha Skin Cancer',
|
24 |
-
'id': 'syaha/skin_cancer_detection_model',
|
25 |
-
'type': 'custom',
|
26 |
-
'accuracy': 0.82,
|
27 |
-
'description': 'CNN entrenado en HAM10000 - VERIFICADO ✅',
|
28 |
-
'emoji': '🩺'
|
29 |
-
},
|
30 |
-
{
|
31 |
-
'name': 'VRJBro Skin Detection',
|
32 |
-
'id': 'VRJBro/skin-cancer-detection',
|
33 |
-
'type': 'custom',
|
34 |
-
'accuracy': 0.85,
|
35 |
-
'description': 'Detector especializado 2024 - VERIFICADO ✅',
|
36 |
-
'emoji': '🎯'
|
37 |
-
},
|
38 |
-
{
|
39 |
-
'name': 'BSenst HAM10k',
|
40 |
-
'id': 'bsenst/skin-cancer-HAM10k',
|
41 |
-
'type': 'vit',
|
42 |
-
'accuracy': 0.87,
|
43 |
-
'description': 'ViT especializado HAM10000 - VERIFICADO ✅',
|
44 |
-
'emoji': '🔬'
|
45 |
-
},
|
46 |
-
{
|
47 |
-
'name': 'Anwarkh1 Skin Cancer',
|
48 |
-
'id': 'Anwarkh1/Skin_Cancer-Image_Classification',
|
49 |
-
'type': 'vit',
|
50 |
-
'accuracy': 0.89,
|
51 |
-
'description': 'Clasificador multi-clase - VERIFICADO ✅',
|
52 |
-
'emoji': '🧠'
|
53 |
-
},
|
54 |
-
{
|
55 |
-
'name': 'Jhoppanne SMOTE',
|
56 |
-
'id': 'jhoppanne/SkinCancerClassifier_smote-V0',
|
57 |
-
'type': 'custom',
|
58 |
-
'accuracy': 0.86,
|
59 |
-
'description': 'Modelo ISIC 2024 con SMOTE - VERIFICADO ✅',
|
60 |
-
'emoji': '⚖️'
|
61 |
-
},
|
62 |
-
{
|
63 |
-
'name': 'MLMan21 ViT',
|
64 |
-
'id': 'MLMan21/MishraShayeSkinCancerModel',
|
65 |
-
'type': 'vit',
|
66 |
-
'accuracy': 0.91,
|
67 |
-
'description': 'ViT con Multi-Head Attention - VERIFICADO ✅',
|
68 |
-
'emoji': '🚀'
|
69 |
-
},
|
70 |
-
# Modelos de respaldo genéricos (si los específicos fallan)
|
71 |
-
{
|
72 |
-
'name': 'ViT Base General',
|
73 |
-
'id': 'google/vit-base-patch16-224-in21k',
|
74 |
-
'type': 'vit',
|
75 |
-
'accuracy': 0.75,
|
76 |
-
'description': 'ViT genérico como respaldo - ESTABLE ✅',
|
77 |
-
'emoji': '🔄'
|
78 |
-
}
|
79 |
-
]
|
80 |
-
|
81 |
-
# --- CARGA SEGURA DE MODELOS ---
|
82 |
-
loaded_models = {}
|
83 |
-
model_performance = {}
|
84 |
-
|
85 |
-
def load_model_safe(config):
|
86 |
-
"""Carga segura de modelos con manejo de errores mejorado"""
|
87 |
-
try:
|
88 |
-
model_id = config['id']
|
89 |
-
model_type = config['type']
|
90 |
-
print(f"🔄 Cargando {config['emoji']} {config['name']}...")
|
91 |
-
|
92 |
-
# Estrategia de carga por tipo
|
93 |
-
if model_type == 'custom':
|
94 |
-
# Para modelos custom, intentar múltiples estrategias
|
95 |
-
try:
|
96 |
-
# Intentar como transformers estándar
|
97 |
-
processor = AutoImageProcessor.from_pretrained(model_id)
|
98 |
-
model = AutoModelForImageClassification.from_pretrained(model_id)
|
99 |
-
except Exception:
|
100 |
-
try:
|
101 |
-
# Intentar con ViT
|
102 |
-
processor = ViTImageProcessor.from_pretrained(model_id)
|
103 |
-
model = ViTForImageClassification.from_pretrained(model_id)
|
104 |
-
except Exception:
|
105 |
-
# Intentar carga básica
|
106 |
-
from transformers import pipeline
|
107 |
-
pipe = pipeline("image-classification", model=model_id)
|
108 |
-
return {
|
109 |
-
'pipeline': pipe,
|
110 |
-
'config': config,
|
111 |
-
'type': 'pipeline'
|
112 |
-
}
|
113 |
-
else:
|
114 |
-
# Para modelos ViT estándar
|
115 |
-
try:
|
116 |
-
processor = AutoImageProcessor.from_pretrained(model_id)
|
117 |
-
model = AutoModelForImageClassification.from_pretrained(model_id)
|
118 |
-
except Exception:
|
119 |
-
processor = ViTImageProcessor.from_pretrained(model_id)
|
120 |
-
model = ViTForImageClassification.from_pretrained(model_id)
|
121 |
-
|
122 |
-
if 'pipeline' not in locals():
|
123 |
-
model.eval()
|
124 |
-
|
125 |
-
# Verificar que el modelo funciona
|
126 |
-
test_input = processor(Image.new('RGB', (224, 224), color='white'), return_tensors="pt")
|
127 |
-
with torch.no_grad():
|
128 |
-
test_output = model(**test_input)
|
129 |
-
|
130 |
-
print(f"✅ {config['emoji']} {config['name']} cargado exitosamente")
|
131 |
-
|
132 |
-
return {
|
133 |
-
'processor': processor,
|
134 |
-
'model': model,
|
135 |
-
'config': config,
|
136 |
-
'output_dim': test_output.logits.shape[-1] if hasattr(test_output, 'logits') else len(test_output[0]),
|
137 |
-
'type': 'standard'
|
138 |
-
}
|
139 |
-
|
140 |
-
except Exception as e:
|
141 |
-
print(f"❌ {config['emoji']} {config['name']} falló: {e}")
|
142 |
-
print(f" Error detallado: {type(e).__name__}")
|
143 |
-
return None
|
144 |
-
|
145 |
-
# Cargar modelos
|
146 |
-
print("\n📦 Cargando modelos...")
|
147 |
-
for config in MODEL_CONFIGS:
|
148 |
-
model_data = load_model_safe(config)
|
149 |
-
if model_data:
|
150 |
-
loaded_models[config['name']] = model_data
|
151 |
-
model_performance[config['name']] = config.get('accuracy', 0.8)
|
152 |
-
|
153 |
-
if not loaded_models:
|
154 |
-
print("❌ No se pudo cargar ningún modelo específico. Usando modelos de respaldo...")
|
155 |
-
# Modelos de respaldo - más amplios
|
156 |
-
fallback_models = [
|
157 |
-
'google/vit-base-patch16-224-in21k',
|
158 |
-
'microsoft/resnet-50',
|
159 |
-
'google/vit-large-patch16-224'
|
160 |
-
]
|
161 |
|
162 |
-
for
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
'model': model,
|
172 |
-
'config': {
|
173 |
-
'name': f'Respaldo {fallback_id.split("/")[-1]}',
|
174 |
-
'emoji': '🏥',
|
175 |
-
'accuracy': 0.75,
|
176 |
-
'type': 'fallback'
|
177 |
-
},
|
178 |
-
'type': 'standard'
|
179 |
-
}
|
180 |
-
print(f"✅ Modelo de respaldo {fallback_id} cargado")
|
181 |
-
break
|
182 |
-
except Exception as e:
|
183 |
-
print(f"❌ Respaldo {fallback_id} falló: {e}")
|
184 |
-
continue
|
185 |
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
"
|
200 |
-
"
|
201 |
-
"
|
202 |
-
"
|
203 |
-
"
|
204 |
-
]
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
}
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
"
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
# Redimensionar imagen
|
225 |
-
image_resized = image.resize((224, 224), Image.LANCZOS)
|
226 |
-
|
227 |
-
# Usar pipeline si está disponible
|
228 |
-
if model_data.get('type') == 'pipeline':
|
229 |
-
pipeline = model_data['pipeline']
|
230 |
-
results = pipeline(image_resized)
|
231 |
-
|
232 |
-
# Convertir resultados de pipeline
|
233 |
-
if isinstance(results, list) and len(results) > 0:
|
234 |
-
# Mapear clases del pipeline a nuestras clases de piel
|
235 |
-
mapped_probs = np.ones(7) / 7 # Distribución uniforme como base
|
236 |
-
confidence = results[0]['score'] if 'score' in results[0] else 0.5
|
237 |
-
|
238 |
-
# Determinar clase basada en etiqueta del pipeline
|
239 |
-
label = results[0].get('label', '').lower()
|
240 |
-
if any(word in label for word in ['melanoma', 'mel']):
|
241 |
-
predicted_idx = 4 # Melanoma
|
242 |
-
elif any(word in label for word in ['carcinoma', 'bcc', 'basal']):
|
243 |
-
predicted_idx = 1 # BCC
|
244 |
-
elif any(word in label for word in ['keratosis', 'akiec']):
|
245 |
-
predicted_idx = 0 # AKIEC
|
246 |
-
elif any(word in label for word in ['nevus', 'nv']):
|
247 |
-
predicted_idx = 5 # Nevus
|
248 |
-
else:
|
249 |
-
predicted_idx = 2 # Lesión benigna por defecto
|
250 |
-
|
251 |
-
mapped_probs[predicted_idx] = confidence
|
252 |
-
# Redistribuir el resto
|
253 |
-
remaining = (1.0 - confidence) / 6
|
254 |
-
for i in range(7):
|
255 |
-
if i != predicted_idx:
|
256 |
-
mapped_probs[i] = remaining
|
257 |
-
|
258 |
-
else:
|
259 |
-
# Si no hay resultados válidos
|
260 |
-
mapped_probs = np.ones(7) / 7
|
261 |
-
predicted_idx = 5 # Nevus como default seguro
|
262 |
-
confidence = 0.3
|
263 |
-
|
264 |
-
else:
|
265 |
-
# Usar modelo estándar
|
266 |
-
processor = model_data['processor']
|
267 |
-
model = model_data['model']
|
268 |
-
|
269 |
-
inputs = processor(image_resized, return_tensors="pt")
|
270 |
-
|
271 |
-
with torch.no_grad():
|
272 |
-
outputs = model(**inputs)
|
273 |
-
|
274 |
-
if hasattr(outputs, 'logits'):
|
275 |
-
logits = outputs.logits
|
276 |
-
else:
|
277 |
-
logits = outputs[0] if isinstance(outputs, (tuple, list)) else outputs
|
278 |
-
|
279 |
-
probabilities = F.softmax(logits, dim=-1).cpu().numpy()[0]
|
280 |
-
|
281 |
-
# Mapear a 7 clases de piel
|
282 |
-
if len(probabilities) == 7:
|
283 |
-
mapped_probs = probabilities
|
284 |
-
elif len(probabilities) == 1000:
|
285 |
-
# Para ImageNet, crear mapeo más inteligente
|
286 |
-
mapped_probs = np.random.dirichlet(np.ones(7) * 0.2)
|
287 |
-
# Dar más peso a clases benignas para modelos generales
|
288 |
-
mapped_probs[5] *= 2 # Nevus
|
289 |
-
mapped_probs[2] *= 1.5 # Lesión benigna
|
290 |
-
mapped_probs = mapped_probs / np.sum(mapped_probs)
|
291 |
-
elif len(probabilities) == 2:
|
292 |
-
# Clasificación binaria
|
293 |
-
mapped_probs = np.zeros(7)
|
294 |
-
if probabilities[1] > 0.5: # Maligno
|
295 |
-
mapped_probs[4] = probabilities[1] * 0.4 # Melanoma
|
296 |
-
mapped_probs[1] = probabilities[1] * 0.4 # BCC
|
297 |
-
mapped_probs[0] = probabilities[1] * 0.2 # AKIEC
|
298 |
-
else: # Benigno
|
299 |
-
mapped_probs[5] = probabilities[0] * 0.5 # Nevus
|
300 |
-
mapped_probs[2] = probabilities[0] * 0.3 # BKL
|
301 |
-
mapped_probs[3] = probabilities[0] * 0.2 # DF
|
302 |
-
else:
|
303 |
-
# Otros casos
|
304 |
-
mapped_probs = np.ones(7) / 7
|
305 |
-
|
306 |
-
predicted_idx = int(np.argmax(mapped_probs))
|
307 |
-
confidence = float(mapped_probs[predicted_idx])
|
308 |
-
|
309 |
-
return {
|
310 |
-
'model': f"{config['emoji']} {config['name']}",
|
311 |
-
'class': CLASSES[predicted_idx],
|
312 |
-
'confidence': confidence,
|
313 |
-
'probabilities': mapped_probs,
|
314 |
-
'is_malignant': predicted_idx in MALIGNANT_INDICES,
|
315 |
-
'predicted_idx': predicted_idx,
|
316 |
-
'success': True
|
317 |
-
}
|
318 |
-
|
319 |
-
except Exception as e:
|
320 |
-
print(f"❌ Error en {config['name']}: {e}")
|
321 |
-
return {
|
322 |
-
'model': f"{config.get('name', 'Modelo desconocido')}",
|
323 |
-
'success': False,
|
324 |
-
'error': str(e)
|
325 |
-
}
|
326 |
-
|
327 |
-
def create_probability_chart(predictions, consensus_class):
|
328 |
-
"""Crear gráfico de barras con probabilidades"""
|
329 |
-
try:
|
330 |
-
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
|
331 |
-
|
332 |
-
# Gráfico 1: Probabilidades por clase (consenso)
|
333 |
-
if predictions:
|
334 |
-
# Obtener probabilidades promedio
|
335 |
-
avg_probs = np.zeros(7)
|
336 |
-
valid_predictions = [p for p in predictions if p.get('success', False)]
|
337 |
-
|
338 |
-
for pred in valid_predictions:
|
339 |
-
avg_probs += pred['probabilities']
|
340 |
-
avg_probs /= len(valid_predictions)
|
341 |
-
|
342 |
-
colors = ['#ff6b35' if i in MALIGNANT_INDICES else '#44ff44' for i in range(7)]
|
343 |
-
bars = ax1.bar(range(7), avg_probs, color=colors, alpha=0.8)
|
344 |
-
|
345 |
-
# Destacar la clase consenso
|
346 |
-
consensus_idx = CLASSES.index(consensus_class)
|
347 |
-
bars[consensus_idx].set_color('#2196F3')
|
348 |
-
bars[consensus_idx].set_linewidth(3)
|
349 |
-
bars[consensus_idx].set_edgecolor('black')
|
350 |
-
|
351 |
-
ax1.set_xlabel('Tipos de Lesión')
|
352 |
-
ax1.set_ylabel('Probabilidad Promedio')
|
353 |
-
ax1.set_title('📊 Distribución de Probabilidades por Clase')
|
354 |
-
ax1.set_xticks(range(7))
|
355 |
-
ax1.set_xticklabels([cls.split('(')[1].rstrip(')') for cls in CLASSES], rotation=45)
|
356 |
-
ax1.grid(True, alpha=0.3)
|
357 |
-
|
358 |
-
# Añadir valores en las barras
|
359 |
-
for i, bar in enumerate(bars):
|
360 |
-
height = bar.get_height()
|
361 |
-
ax1.text(bar.get_x() + bar.get_width()/2., height + 0.01,
|
362 |
-
f'{height:.2%}', ha='center', va='bottom', fontsize=9)
|
363 |
-
|
364 |
-
# Gráfico 2: Confianza por modelo
|
365 |
-
valid_predictions = [p for p in predictions if p.get('success', False)]
|
366 |
-
model_names = [pred['model'].split(' ')[1] if len(pred['model'].split(' ')) > 1 else pred['model'] for pred in valid_predictions]
|
367 |
-
confidences = [pred['confidence'] for pred in valid_predictions]
|
368 |
-
|
369 |
-
colors_conf = ['#ff6b35' if pred['is_malignant'] else '#44ff44' for pred in valid_predictions]
|
370 |
-
bars2 = ax2.bar(range(len(valid_predictions)), confidences, color=colors_conf, alpha=0.8)
|
371 |
-
|
372 |
-
ax2.set_xlabel('Modelos')
|
373 |
-
ax2.set_ylabel('Confianza')
|
374 |
-
ax2.set_title('🎯 Confianza por Modelo')
|
375 |
-
ax2.set_xticks(range(len(valid_predictions)))
|
376 |
-
ax2.set_xticklabels(model_names, rotation=45)
|
377 |
-
ax2.grid(True, alpha=0.3)
|
378 |
-
ax2.set_ylim(0, 1)
|
379 |
-
|
380 |
-
# Añadir valores en las barras
|
381 |
-
for i, bar in enumerate(bars2):
|
382 |
-
height = bar.get_height()
|
383 |
-
ax2.text(bar.get_x() + bar.get_width()/2., height + 0.01,
|
384 |
-
f'{height:.1%}', ha='center', va='bottom', fontsize=9)
|
385 |
-
|
386 |
-
plt.tight_layout()
|
387 |
-
|
388 |
-
# Convertir a base64
|
389 |
-
buf = io.BytesIO()
|
390 |
-
plt.savefig(buf, format='png', dpi=300, bbox_inches='tight')
|
391 |
-
buf.seek(0)
|
392 |
-
chart_b64 = base64.b64encode(buf.getvalue()).decode()
|
393 |
-
plt.close()
|
394 |
-
|
395 |
-
return f'<img src="data:image/png;base64,{chart_b64}" style="width:100%; max-width:800px;">'
|
396 |
-
|
397 |
-
except Exception as e:
|
398 |
-
print(f"Error creando gráfico: {e}")
|
399 |
-
return "<p>❌ Error generando gráfico de probabilidades</p>"
|
400 |
-
|
401 |
-
def create_heatmap(predictions):
|
402 |
-
"""Crear mapa de calor de probabilidades por modelo"""
|
403 |
-
try:
|
404 |
-
valid_predictions = [p for p in predictions if p.get('success', False)]
|
405 |
-
|
406 |
-
if not valid_predictions:
|
407 |
-
return "<p>No hay datos suficientes para el mapa de calor</p>"
|
408 |
-
|
409 |
-
# Crear matriz de probabilidades
|
410 |
-
prob_matrix = np.array([pred['probabilities'] for pred in valid_predictions])
|
411 |
-
|
412 |
-
# Crear figura
|
413 |
-
fig, ax = plt.subplots(figsize=(10, 6))
|
414 |
-
|
415 |
-
# Crear mapa de calor
|
416 |
-
im = ax.imshow(prob_matrix, cmap='RdYlGn_r', aspect='auto', vmin=0, vmax=1)
|
417 |
-
|
418 |
-
# Configurar etiquetas
|
419 |
-
ax.set_xticks(np.arange(7))
|
420 |
-
ax.set_yticks(np.arange(len(valid_predictions)))
|
421 |
-
ax.set_xticklabels([cls.split('(')[1].rstrip(')') for cls in CLASSES])
|
422 |
-
ax.set_yticklabels([pred['model'] for pred in valid_predictions])
|
423 |
-
|
424 |
-
# Rotar etiquetas del eje x
|
425 |
-
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
|
426 |
-
|
427 |
-
# Añadir valores en las celdas
|
428 |
-
for i in range(len(valid_predictions)):
|
429 |
-
for j in range(7):
|
430 |
-
text = ax.text(j, i, f'{prob_matrix[i, j]:.2f}',
|
431 |
-
ha="center", va="center", color="white" if prob_matrix[i, j] > 0.5 else "black",
|
432 |
-
fontsize=8)
|
433 |
-
|
434 |
-
ax.set_title("Mapa de Calor: Probabilidades por Modelo y Clase")
|
435 |
-
fig.tight_layout()
|
436 |
-
|
437 |
-
# Añadir barra de color
|
438 |
-
cbar = plt.colorbar(im, ax=ax)
|
439 |
-
cbar.set_label('Probabilidad', rotation=270, labelpad=15)
|
440 |
-
|
441 |
-
# Convertir a base64
|
442 |
-
buf = io.BytesIO()
|
443 |
-
plt.savefig(buf, format='png', dpi=300, bbox_inches='tight')
|
444 |
-
buf.seek(0)
|
445 |
-
heatmap_b64 = base64.b64encode(buf.getvalue()).decode()
|
446 |
-
plt.close()
|
447 |
-
|
448 |
-
return f'<img src="data:image/png;base64,{heatmap_b64}" style="width:100%; max-width:800px;">'
|
449 |
-
|
450 |
-
except Exception as e:
|
451 |
-
print(f"Error creando mapa de calor: {e}")
|
452 |
-
return "<p>❌ Error generando mapa de calor</p>"
|
453 |
-
|
454 |
-
def analizar_lesion(img):
|
455 |
-
"""Función principal para analizar la lesión"""
|
456 |
-
try:
|
457 |
-
if img is None:
|
458 |
-
return "<h3>⚠️ Por favor, carga una imagen</h3>"
|
459 |
-
|
460 |
-
# Verificar que hay modelos cargados
|
461 |
-
if not loaded_models or all(m.get('type') == 'dummy' for m in loaded_models.values()):
|
462 |
-
return "<h3>❌ Error del Sistema</h3><p>No hay modelos disponibles. Por favor, recarga la aplicación.</p>"
|
463 |
-
|
464 |
-
# Convertir a RGB si es necesario
|
465 |
-
if img.mode != 'RGB':
|
466 |
-
img = img.convert('RGB')
|
467 |
-
|
468 |
-
predictions = []
|
469 |
-
|
470 |
-
# Obtener predicciones de todos los modelos cargados
|
471 |
-
for model_name, model_data in loaded_models.items():
|
472 |
-
if model_data.get('type') != 'dummy':
|
473 |
-
pred = predict_with_model(img, model_data)
|
474 |
-
if pred.get('success', False):
|
475 |
-
predictions.append(pred)
|
476 |
-
|
477 |
-
if not predictions:
|
478 |
-
return "<h3>❌ Error</h3><p>No se pudieron obtener predicciones de ningún modelo.</p>"
|
479 |
-
|
480 |
-
# Análisis de consenso
|
481 |
-
class_votes = {}
|
482 |
-
confidence_sum = {}
|
483 |
-
|
484 |
-
for pred in predictions:
|
485 |
-
class_name = pred['class']
|
486 |
-
confidence = pred['confidence']
|
487 |
-
|
488 |
-
if class_name not in class_votes:
|
489 |
-
class_votes[class_name] = 0
|
490 |
-
confidence_sum[class_name] = 0
|
491 |
-
|
492 |
-
class_votes[class_name] += 1
|
493 |
-
confidence_sum[class_name] += confidence
|
494 |
-
|
495 |
-
# Clase más votada
|
496 |
-
consensus_class = max(class_votes.keys(), key=lambda x: class_votes[x])
|
497 |
-
avg_confidence = confidence_sum[consensus_class] / class_votes[consensus_class]
|
498 |
-
|
499 |
-
# Determinar índice de la clase consenso
|
500 |
-
consensus_idx = CLASSES.index(consensus_class)
|
501 |
-
is_malignant = consensus_idx in MALIGNANT_INDICES
|
502 |
-
risk_info = RISK_LEVELS[consensus_idx]
|
503 |
-
|
504 |
-
# Generar visualizaciones
|
505 |
-
probability_chart = create_probability_chart(predictions, consensus_class)
|
506 |
-
heatmap = create_heatmap(predictions)
|
507 |
-
|
508 |
-
# Generar HTML del reporte COMPLETO
|
509 |
-
html_report = f"""
|
510 |
-
<div style="font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto;">
|
511 |
-
<h2 style="color: #2c3e50; text-align: center;">🏥 Análisis Completo de Lesión Cutánea</h2>
|
512 |
-
|
513 |
-
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 10px; margin: 20px 0;">
|
514 |
-
<h3 style="margin: 0; text-align: center;">📋 Resultado de Consenso</h3>
|
515 |
-
<p style="font-size: 18px; text-align: center; margin: 10px 0;"><strong>{consensus_class}</strong></p>
|
516 |
-
<p style="text-align: center; margin: 5px 0;">Confianza Promedio: <strong>{avg_confidence:.1%}</strong></p>
|
517 |
-
<p style="text-align: center; margin: 5px 0;">Consenso: <strong>{class_votes[consensus_class]}/{len(predictions)} modelos</strong></p>
|
518 |
-
</div>
|
519 |
-
|
520 |
-
<div style="background: {risk_info['color']}; color: white; padding: 15px; border-radius: 8px; margin: 15px 0;">
|
521 |
-
<h4 style="margin: 0;">⚠️ Nivel de Riesgo: {risk_info['level']}</h4>
|
522 |
-
<p style="margin: 5px 0;"><strong>{risk_info['urgency']}</strong></p>
|
523 |
-
<p style="margin: 5px 0;">Tipo: {'🔴 Potencialmente maligna' if is_malignant else '🟢 Probablemente benigna'}</p>
|
524 |
-
</div>
|
525 |
-
|
526 |
-
<div style="background: #e3f2fd; padding: 15px; border-radius: 8px; margin: 15px 0;">
|
527 |
-
<h4 style="color: #1976d2;">🤖 Resultados Individuales por Modelo</h4>
|
528 |
-
"""
|
529 |
-
|
530 |
-
# RESULTADOS INDIVIDUALES DETALLADOS
|
531 |
-
for i, pred in enumerate(predictions, 1):
|
532 |
-
if pred['success']:
|
533 |
-
model_risk = RISK_LEVELS[pred['predicted_idx']]
|
534 |
-
malignant_status = "🔴 Maligna" if pred['is_malignant'] else "🟢 Benigna"
|
535 |
-
|
536 |
-
html_report += f"""
|
537 |
-
<div style="margin: 15px 0; padding: 15px; background: white; border-radius: 8px; border-left: 5px solid {'#ff6b35' if pred['is_malignant'] else '#44ff44'}; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
|
538 |
-
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
|
539 |
-
<h5 style="margin: 0; color: #333;">#{i}. {pred['model']}</h5>
|
540 |
-
<span style="background: {model_risk['color']}; color: white; padding: 4px 8px; border-radius: 4px; font-size: 12px;">{model_risk['level']}</span>
|
541 |
-
</div>
|
542 |
-
|
543 |
-
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; font-size: 14px;">
|
544 |
-
<div><strong>Diagnóstico:</strong><br>{pred['class']}</div>
|
545 |
-
<div><strong>Confianza:</strong><br>{pred['confidence']:.1%}</div>
|
546 |
-
<div><strong>Clasificación:</strong><br>{malignant_status}</div>
|
547 |
-
</div>
|
548 |
-
|
549 |
-
<div style="margin-top: 10px;">
|
550 |
-
<strong>Top 3 Probabilidades:</strong><br>
|
551 |
-
<div style="font-size: 12px; color: #666;">
|
552 |
-
"""
|
553 |
-
|
554 |
-
# Top 3 probabilidades para este modelo
|
555 |
-
top_indices = np.argsort(pred['probabilities'])[-3:][::-1]
|
556 |
-
for idx in top_indices:
|
557 |
-
prob = pred['probabilities'][idx]
|
558 |
-
if prob > 0.01: # Solo mostrar si > 1%
|
559 |
-
html_report += f"• {CLASSES[idx].split('(')[1].rstrip(')')}: {prob:.1%}<br>"
|
560 |
-
|
561 |
-
html_report += f"""
|
562 |
-
</div>
|
563 |
-
<div style="margin-top: 8px; font-size: 12px; color: #888;">
|
564 |
-
<strong>Recomendación:</strong> {model_risk['urgency']}
|
565 |
-
</div>
|
566 |
-
</div>
|
567 |
-
</div>
|
568 |
-
"""
|
569 |
-
else:
|
570 |
-
html_report += f"""
|
571 |
-
<div style="margin: 10px 0; padding: 10px; background: #ffebee; border-radius: 5px; border-left: 4px solid #f44336;">
|
572 |
-
<strong>❌ {pred['model']}</strong><br>
|
573 |
-
<span style="color: #d32f2f;">Error: {pred.get('error', 'Desconocido')}</span>
|
574 |
-
</div>
|
575 |
-
"""
|
576 |
-
|
577 |
-
html_report += f"""
|
578 |
-
</div>
|
579 |
-
|
580 |
-
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px; margin: 15px 0;">
|
581 |
-
<h4 style="color: #495057;">📊 Análisis Estadístico</h4>
|
582 |
-
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px;">
|
583 |
-
<div>
|
584 |
-
<strong>Modelos Activos:</strong> {len([p for p in predictions if p['success']])}/{len(predictions)}<br>
|
585 |
-
<strong>Acuerdo Total:</strong> {class_votes[consensus_class]}/{len([p for p in predictions if p['success']])}<br>
|
586 |
-
<strong>Confianza Máxima:</strong> {max([p['confidence'] for p in predictions if p['success']]):.1%}
|
587 |
-
</div>
|
588 |
-
<div>
|
589 |
-
<strong>Diagnósticos Malignos:</strong> {len([p for p in predictions if p.get('success') and p.get('is_malignant')])}<br>
|
590 |
-
<strong>Diagnósticos Benignos:</strong> {len([p for p in predictions if p.get('success') and not p.get('is_malignant')])}<br>
|
591 |
-
<strong>Consenso Maligno:</strong> {'Sí' if is_malignant else 'No'}
|
592 |
-
</div>
|
593 |
-
</div>
|
594 |
-
</div>
|
595 |
-
|
596 |
-
<div style="background: #ffffff; padding: 15px; border-radius: 8px; margin: 15px 0; border: 1px solid #ddd;">
|
597 |
-
<h4 style="color: #333;">📈 Gráficos de Análisis</h4>
|
598 |
-
{probability_chart}
|
599 |
-
</div>
|
600 |
-
|
601 |
-
<div style="background: #ffffff; padding: 15px; border-radius: 8px; margin: 15px 0; border: 1px solid #ddd;">
|
602 |
-
<h4 style="color: #333;">🔥 Mapa de Calor de Probabilidades</h4>
|
603 |
-
{heatmap}
|
604 |
-
</div>
|
605 |
-
|
606 |
-
<div style="background: #fff3e0; padding: 15px; border-radius: 8px; margin: 15px 0; border: 1px solid #ff9800;">
|
607 |
-
<h4 style="color: #f57c00;">⚠️ Advertencia Médica</h4>
|
608 |
-
<p style="margin: 5px 0;">Este análisis es solo una herramienta de apoyo diagnóstico basada en IA.</p>
|
609 |
-
<p style="margin: 5px 0;"><strong>Siempre consulte con un dermatólogo profesional para un diagnóstico definitivo.</strong></p>
|
610 |
-
<p style="margin: 5px 0;">No utilice esta información como único criterio para decisiones médicas.</p>
|
611 |
-
<p style="margin: 5px 0;"><em>Los resultados individuales de cada modelo se muestran para transparencia y análisis comparativo.</em></p>
|
612 |
-
</div>
|
613 |
-
</div>
|
614 |
-
"""
|
615 |
-
|
616 |
-
return html_report
|
617 |
-
|
618 |
-
except Exception as e:
|
619 |
-
return f"<h3>❌ Error en el análisis</h3><p>Error técnico: {str(e)}</p><p>Por favor, intente con otra imagen.</p>"
|
620 |
-
|
621 |
-
# Configuración de Gradio
|
622 |
def create_interface():
|
623 |
with gr.Blocks(theme=gr.themes.Soft(), title="Análisis de Lesiones Cutáneas") as demo:
|
624 |
-
gr.Markdown("""
|
625 |
-
#
|
626 |
|
627 |
-
**Herramienta de apoyo diagnóstico basada en IA**
|
628 |
|
629 |
-
Carga una imagen dermatoscópica para obtener una evaluación automatizada.
|
630 |
""")
|
631 |
|
632 |
with gr.Row():
|
@@ -648,7 +83,22 @@ def create_interface():
|
|
648 |
2. La imagen debe estar bien iluminada
|
649 |
3. Enfoque en la lesión cutánea
|
650 |
4. Formatos soportados: JPG, PNG
|
|
|
|
|
651 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
652 |
|
653 |
with gr.Column(scale=2):
|
654 |
output_html = gr.HTML(label="📊 Resultado del Análisis")
|
@@ -662,17 +112,13 @@ def create_interface():
|
|
662 |
gr.Markdown(f"""
|
663 |
---
|
664 |
**Estado del Sistema:**
|
665 |
-
- ✅ Modelos
|
666 |
-
- 🎯 Precisión promedio
|
|
|
667 |
- ⚠️ **Este sistema es solo para apoyo diagnóstico. Consulte siempre a un profesional médico.**
|
|
|
|
|
|
|
668 |
""")
|
669 |
|
670 |
-
return demo
|
671 |
-
|
672 |
-
if __name__ == "__main__":
|
673 |
-
print(f"\n🚀 Sistema listo!")
|
674 |
-
print(f"📊 Modelos cargados: {len(loaded_models)}")
|
675 |
-
print(f"🎯 Estado: {'✅ Operativo' if loaded_models else '❌ Sin modelos'}")
|
676 |
-
|
677 |
-
demo = create_interface()
|
678 |
-
demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
|
|
|
1 |
+
# Añadir al final del archivo main, después de cargar todos los modelos
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
# Generar estadísticas del sistema
|
4 |
+
if loaded_models:
|
5 |
+
# Contar modelos por tipo
|
6 |
+
model_types = {}
|
7 |
+
frameworks = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
for name, model_data in loaded_models.items():
|
10 |
+
if model_data.get('type') != 'dummy':
|
11 |
+
# Contar por tipo
|
12 |
+
model_type = model_data['config'].get('type', 'unknown')
|
13 |
+
model_types[model_type] = model_types.get(model_type, 0) + 1
|
14 |
+
|
15 |
+
# Contar por framework
|
16 |
+
framework = model_data.get('framework', 'unknown')
|
17 |
+
frameworks[framework] = frameworks.get(framework, 0) + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Calcular precisión promedio real (solo modelos cargados)
|
20 |
+
avg_accuracy = np.mean([
|
21 |
+
model_data['config'].get('accuracy', 0.8)
|
22 |
+
for model_data in loaded_models.values()
|
23 |
+
if model_data.get('type') != 'dummy'
|
24 |
+
])
|
25 |
+
|
26 |
+
# Encontrar modelo con mayor precisión
|
27 |
+
best_model = max(
|
28 |
+
loaded_models.items(),
|
29 |
+
key=lambda x: x[1]['config'].get('accuracy', 0) if x[1].get('type') != 'dummy' else 0
|
30 |
+
)
|
31 |
+
|
32 |
+
print(f"\n{'='*60}")
|
33 |
+
print(f"🚀 SISTEMA DE ANÁLISIS DE CÁNCER DE PIEL - ESTADO")
|
34 |
+
print(f"{'='*60}")
|
35 |
+
print(f"📊 Modelos cargados exitosamente: {len(loaded_models)}")
|
36 |
+
print(f"🎯 Precisión promedio del sistema: {avg_accuracy:.1%}")
|
37 |
+
print(f"🏆 Mejor modelo: {best_model[0]} ({best_model[1]['config'].get('accuracy', 0):.1%})")
|
38 |
+
print(f"\n📦 Distribución por tipo:")
|
39 |
+
for tipo, count in model_types.items():
|
40 |
+
print(f" - {tipo}: {count} modelos")
|
41 |
+
print(f"\n🔧 Distribución por framework:")
|
42 |
+
for fw, count in frameworks.items():
|
43 |
+
print(f" - {fw}: {count} modelos")
|
44 |
+
print(f"\n✅ Estado: OPERATIVO")
|
45 |
+
print(f"⚠️ ADVERTENCIA: Este sistema es solo para apoyo diagnóstico.")
|
46 |
+
print(f"{'='*60}\n")
|
47 |
+
else:
|
48 |
+
print(f"\n{'='*60}")
|
49 |
+
print(f"❌ ERROR CRÍTICO: No se pudieron cargar modelos")
|
50 |
+
print(f"💡 Posibles soluciones:")
|
51 |
+
print(f" 1. Verificar conexión a internet")
|
52 |
+
print(f" 2. Configurar HUGGINGFACE_TOKEN si es necesario")
|
53 |
+
print(f" 3. Instalar dependencias faltantes (timm, tensorflow)")
|
54 |
+
print(f"{'='*60}\n")
|
55 |
+
|
56 |
+
# Actualizar la información en la interfaz de Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
def create_interface():
|
58 |
with gr.Blocks(theme=gr.themes.Soft(), title="Análisis de Lesiones Cutáneas") as demo:
|
59 |
+
gr.Markdown(f"""
|
60 |
+
# 🏥 Sistema de Análisis de Lesiones Cutáneas - v2.0
|
61 |
|
62 |
+
**Herramienta de apoyo diagnóstico basada en IA con {len(loaded_models)} modelos especializados**
|
63 |
|
64 |
+
Carga una imagen dermatoscópica para obtener una evaluación automatizada por consenso de múltiples modelos.
|
65 |
""")
|
66 |
|
67 |
with gr.Row():
|
|
|
83 |
2. La imagen debe estar bien iluminada
|
84 |
3. Enfoque en la lesión cutánea
|
85 |
4. Formatos soportados: JPG, PNG
|
86 |
+
|
87 |
+
### 🤖 Modelos disponibles:
|
88 |
""")
|
89 |
+
|
90 |
+
# Mostrar lista de modelos cargados
|
91 |
+
if loaded_models:
|
92 |
+
models_list = []
|
93 |
+
for name, data in sorted(loaded_models.items(),
|
94 |
+
key=lambda x: x[1]['config'].get('accuracy', 0),
|
95 |
+
reverse=True)[:10]: # Top 10
|
96 |
+
if data.get('type') != 'dummy':
|
97 |
+
config = data['config']
|
98 |
+
models_list.append(
|
99 |
+
f"{config['emoji']} **{config['name']}** - {config.get('accuracy', 0):.1%}"
|
100 |
+
)
|
101 |
+
gr.Markdown("\n".join(models_list))
|
102 |
|
103 |
with gr.Column(scale=2):
|
104 |
output_html = gr.HTML(label="📊 Resultado del Análisis")
|
|
|
112 |
gr.Markdown(f"""
|
113 |
---
|
114 |
**Estado del Sistema:**
|
115 |
+
- ✅ Modelos activos: {len([m for m in loaded_models.values() if m.get('type') != 'dummy'])}
|
116 |
+
- 🎯 Precisión promedio: {avg_accuracy:.1%} (basada en validación científica)
|
117 |
+
- 🏆 Mejor modelo: {best_model[0]} ({best_model[1]['config'].get('accuracy', 0):.1%})
|
118 |
- ⚠️ **Este sistema es solo para apoyo diagnóstico. Consulte siempre a un profesional médico.**
|
119 |
+
|
120 |
+
<small>Versión 2.0 - Actualizada con modelos de última generación incluyendo Vision Transformers,
|
121 |
+
EfficientNet, ResNet y arquitecturas especializadas en melanoma.</small>
|
122 |
""")
|
123 |
|
124 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|