Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,7 +38,7 @@ MODEL_CONFIGS = {
|
|
| 38 |
},
|
| 39 |
{
|
| 40 |
'name': 'Anwarkh1 Skin Cancer',
|
| 41 |
-
'id': 'Anwarkh1/Skin_Cancer-Image_Classification',
|
| 42 |
'type': 'vit',
|
| 43 |
'accuracy': 0.89,
|
| 44 |
'description': 'Clasificador multi-clase de lesiones de piel',
|
|
@@ -75,15 +75,15 @@ MODEL_CONFIGS = {
|
|
| 75 |
'name': 'ViT Base General',
|
| 76 |
'id': 'google/vit-base-patch16-224',
|
| 77 |
'type': 'vit',
|
| 78 |
-
'accuracy': 0.78,
|
| 79 |
'description': 'ViT base pre-entrenado en ImageNet-1k. Excelente para características visuales generales.',
|
| 80 |
'emoji': '📈'
|
| 81 |
},
|
| 82 |
{
|
| 83 |
'name': 'ResNet-50 (Microsoft)',
|
| 84 |
'id': 'microsoft/resnet-50',
|
| 85 |
-
'type': 'custom',
|
| 86 |
-
'accuracy': 0.77,
|
| 87 |
'description': 'Un clásico ResNet-50, robusto y de alto rendimiento en clasificación de imágenes generales.',
|
| 88 |
'emoji': '⚙️'
|
| 89 |
},
|
|
@@ -91,7 +91,7 @@ MODEL_CONFIGS = {
|
|
| 91 |
'name': 'DeiT Base (Facebook)',
|
| 92 |
'id': 'facebook/deit-base-patch16-224',
|
| 93 |
'type': 'vit',
|
| 94 |
-
'accuracy': 0.79,
|
| 95 |
'description': 'Data-efficient Image Transformer, eficiente y de buen rendimiento general.',
|
| 96 |
'emoji': '💡'
|
| 97 |
},
|
|
@@ -99,15 +99,15 @@ MODEL_CONFIGS = {
|
|
| 99 |
'name': 'MobileNetV2 (Google)',
|
| 100 |
'id': 'google/mobilenet_v2_1.0_224',
|
| 101 |
'type': 'custom',
|
| 102 |
-
'accuracy': 0.72,
|
| 103 |
'description': 'MobileNetV2, modelo ligero y rápido, ideal para entornos con recursos limitados.',
|
| 104 |
'emoji': '📱'
|
| 105 |
},
|
| 106 |
{
|
| 107 |
'name': 'Swin Tiny (Microsoft)',
|
| 108 |
-
'id': 'microsoft/swin-tiny-patch4-window7-224',
|
| 109 |
-
'type': 'custom',
|
| 110 |
-
'accuracy': 0.81,
|
| 111 |
'description': 'Swin Transformer (Tiny), potente para visión por computadora.',
|
| 112 |
'emoji': '🌀'
|
| 113 |
},
|
|
@@ -128,34 +128,56 @@ loaded_models = {}
|
|
| 128 |
model_performance = {}
|
| 129 |
|
| 130 |
def load_model_safe(config):
|
| 131 |
-
"""Carga segura de modelos con manejo de errores mejorado"""
|
| 132 |
try:
|
| 133 |
model_id = config['id']
|
| 134 |
model_type = config['type']
|
| 135 |
print(f"🔄 Cargando {config['emoji']} {config['name']}...")
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
model.eval()
|
| 151 |
-
|
| 152 |
# Verificar que el modelo funciona con una entrada dummy
|
| 153 |
test_input = processor(Image.new('RGB', (224, 224), color='white'), return_tensors="pt")
|
| 154 |
with torch.no_grad():
|
| 155 |
test_output = model(**test_input)
|
| 156 |
-
|
| 157 |
print(f"✅ {config['emoji']} {config['name']} cargado exitosamente")
|
| 158 |
-
|
| 159 |
return {
|
| 160 |
'processor': processor,
|
| 161 |
'model': model,
|
|
@@ -163,7 +185,7 @@ def load_model_safe(config):
|
|
| 163 |
'output_dim': test_output.logits.shape[-1] if hasattr(test_output, 'logits') else len(test_output[0]),
|
| 164 |
'category': config.get('category', 'general') # Añadimos la categoría aquí
|
| 165 |
}
|
| 166 |
-
|
| 167 |
except Exception as e:
|
| 168 |
print(f"❌ {config['emoji']} {config['name']} falló: {e}")
|
| 169 |
print(f" Error detallado: {type(e).__name__}")
|
|
@@ -175,7 +197,7 @@ print("\n📦 Cargando modelos...")
|
|
| 175 |
for category, configs in MODEL_CONFIGS.items():
|
| 176 |
for config in configs:
|
| 177 |
# Añadir la categoría al diccionario de configuración antes de pasar a load_model_safe
|
| 178 |
-
config['category'] = category
|
| 179 |
model_data = load_model_safe(config)
|
| 180 |
if model_data:
|
| 181 |
loaded_models[config['name']] = model_data
|
|
@@ -189,20 +211,20 @@ if not loaded_models:
|
|
| 189 |
'microsoft/resnet-50',
|
| 190 |
'google/vit-large-patch16-224'
|
| 191 |
]
|
| 192 |
-
|
| 193 |
for fallback_id in fallback_models:
|
| 194 |
try:
|
| 195 |
print(f"🔄 Intentando modelo de respaldo: {fallback_id}")
|
| 196 |
processor = AutoImageProcessor.from_pretrained(fallback_id)
|
| 197 |
model = AutoModelForImageClassification.from_pretrained(fallback_id)
|
| 198 |
model.eval()
|
| 199 |
-
|
| 200 |
loaded_models[f'Respaldo-{fallback_id.split("/")[-1]}'] = {
|
| 201 |
'processor': processor,
|
| 202 |
'model': model,
|
| 203 |
'config': {
|
| 204 |
-
'name': f'Respaldo {fallback_id.split("/")[-1]}',
|
| 205 |
-
'emoji': '🏥',
|
| 206 |
'accuracy': 0.75,
|
| 207 |
'type': 'fallback',
|
| 208 |
'category': 'general' # El de respaldo es general
|
|
@@ -215,7 +237,7 @@ if not loaded_models:
|
|
| 215 |
except Exception as e:
|
| 216 |
print(f"❌ Respaldo {fallback_id} falló: {e}")
|
| 217 |
continue
|
| 218 |
-
|
| 219 |
if not loaded_models:
|
| 220 |
print(f"❌ ERROR CRÍTICO: No se pudo cargar ningún modelo")
|
| 221 |
print("💡 Verifica tu conexión a internet y que tengas transformers instalado")
|
|
@@ -227,12 +249,12 @@ if not loaded_models:
|
|
| 227 |
|
| 228 |
# Clases de lesiones de piel (HAM10000 dataset)
|
| 229 |
CLASSES = [
|
| 230 |
-
"Queratosis actínica / Bowen (AKIEC)",
|
| 231 |
"Carcinoma células basales (BCC)",
|
| 232 |
-
"Lesión queratósica benigna (BKL)",
|
| 233 |
-
"Dermatofibroma (DF)",
|
| 234 |
-
"Melanoma maligno (MEL)",
|
| 235 |
-
"Nevus melanocítico (NV)",
|
| 236 |
"Lesión vascular (VASC)"
|
| 237 |
]
|
| 238 |
|
|
@@ -253,62 +275,62 @@ def predict_with_model(image, model_data):
|
|
| 253 |
"""Predicción con un modelo específico - versión mejorada"""
|
| 254 |
try:
|
| 255 |
config = model_data['config']
|
| 256 |
-
|
| 257 |
# Redimensionar imagen
|
| 258 |
image_resized = image.resize((224, 224), Image.LANCZOS)
|
| 259 |
-
|
| 260 |
if model_data.get('type') == 'pipeline': # Esto debería ser poco común con la lista actual
|
| 261 |
pipeline = model_data['pipeline']
|
| 262 |
results = pipeline(image_resized)
|
| 263 |
-
|
| 264 |
if isinstance(results, list) and len(results) > 0:
|
| 265 |
-
mapped_probs = np.ones(7) / 7
|
| 266 |
confidence = results[0]['score'] if 'score' in results[0] else 0.5
|
| 267 |
-
|
| 268 |
label = results[0].get('label', '').lower()
|
| 269 |
if any(word in label for word in ['melanoma', 'mel', 'malignant', 'cancer']):
|
| 270 |
-
predicted_idx = 4
|
| 271 |
elif any(word in label for word in ['carcinoma', 'bcc', 'basal']):
|
| 272 |
-
predicted_idx = 1
|
| 273 |
elif any(word in label for word in ['keratosis', 'akiec']):
|
| 274 |
-
predicted_idx = 0
|
| 275 |
elif any(word in label for word in ['nevus', 'nv', 'benign']):
|
| 276 |
-
predicted_idx = 5
|
| 277 |
else:
|
| 278 |
-
predicted_idx = 2
|
| 279 |
-
|
| 280 |
mapped_probs[predicted_idx] = confidence
|
| 281 |
remaining_sum = (1.0 - confidence)
|
| 282 |
-
if remaining_sum < 0: remaining_sum = 0
|
| 283 |
-
|
| 284 |
-
num_other_classes = 6
|
| 285 |
if num_other_classes > 0:
|
| 286 |
remaining_per_class = remaining_sum / num_other_classes
|
| 287 |
for i in range(7):
|
| 288 |
if i != predicted_idx:
|
| 289 |
mapped_probs[i] = remaining_per_class
|
| 290 |
-
|
| 291 |
else:
|
| 292 |
mapped_probs = np.ones(7) / 7
|
| 293 |
-
predicted_idx = 5
|
| 294 |
confidence = 0.3
|
| 295 |
-
|
| 296 |
else: # Usar modelo estándar (AutoModel/ViT)
|
| 297 |
processor = model_data['processor']
|
| 298 |
model = model_data['model']
|
| 299 |
-
|
| 300 |
inputs = processor(image_resized, return_tensors="pt")
|
| 301 |
-
|
| 302 |
with torch.no_grad():
|
| 303 |
outputs = model(**inputs)
|
| 304 |
-
|
| 305 |
if hasattr(outputs, 'logits'):
|
| 306 |
logits = outputs.logits
|
| 307 |
else:
|
| 308 |
logits = outputs[0] if isinstance(outputs, (tuple, list)) else outputs
|
| 309 |
-
|
| 310 |
probabilities = F.softmax(logits, dim=-1).cpu().numpy()[0]
|
| 311 |
-
|
| 312 |
# --- Mapeo de probabilidades según el número de clases de salida del modelo ---
|
| 313 |
if len(probabilities) == 7: # Modelos ya entrenados para 7 clases de piel
|
| 314 |
mapped_probs = probabilities
|
|
@@ -327,21 +349,43 @@ def predict_with_model(image, model_data):
|
|
| 327 |
mapped_probs[3] = probabilities[0] * 0.1 # DF
|
| 328 |
mapped_probs[6] = probabilities[0] * 0.1 # VASC
|
| 329 |
mapped_probs = mapped_probs / np.sum(mapped_probs) # Normalizar para que sumen 1
|
| 330 |
-
elif len(probabilities)
|
| 331 |
-
mapped_probs = np.
|
| 332 |
-
#
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
else: # Otros casos de dimensiones de salida no esperadas: distribución uniforme
|
| 339 |
print(f"Advertencia: Dimensión de salida inesperada para {config['name']} ({len(probabilities)} clases). Usando distribución uniforme.")
|
| 340 |
mapped_probs = np.ones(7) / 7
|
| 341 |
-
|
| 342 |
predicted_idx = int(np.argmax(mapped_probs))
|
| 343 |
confidence = float(mapped_probs[predicted_idx])
|
| 344 |
-
|
| 345 |
return {
|
| 346 |
'model': f"{config['emoji']} {config['name']}",
|
| 347 |
'class': CLASSES[predicted_idx],
|
|
@@ -352,7 +396,7 @@ def predict_with_model(image, model_data):
|
|
| 352 |
'success': True,
|
| 353 |
'category': model_data['category'] # Añadir la categoría de vuelta
|
| 354 |
}
|
| 355 |
-
|
| 356 |
except Exception as e:
|
| 357 |
print(f"❌ Error en {config['name']}: {e}")
|
| 358 |
return {
|
|
@@ -366,12 +410,12 @@ def create_probability_chart(predictions, consensus_class):
|
|
| 366 |
"""Crear gráfico de barras con probabilidades"""
|
| 367 |
try:
|
| 368 |
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
|
| 369 |
-
|
| 370 |
# Gráfico 1: Probabilidades por clase (consenso)
|
| 371 |
if predictions:
|
| 372 |
avg_probs = np.zeros(7)
|
| 373 |
valid_predictions = [p for p in predictions if p.get('success', False)]
|
| 374 |
-
|
| 375 |
if len(valid_predictions) > 0:
|
| 376 |
for pred in valid_predictions:
|
| 377 |
if isinstance(pred['probabilities'], np.ndarray) and len(pred['probabilities']) == 7 and not np.isnan(pred['probabilities']).any():
|
|
@@ -380,37 +424,37 @@ def create_probability_chart(predictions, consensus_class):
|
|
| 380 |
print(f"Advertencia: Probabilidades no válidas para {pred['model']}: {pred['probabilities']}")
|
| 381 |
avg_probs /= len(valid_predictions)
|
| 382 |
else:
|
| 383 |
-
avg_probs = np.ones(7) / 7
|
| 384 |
-
|
| 385 |
colors = ['#ff6b35' if i in MALIGNANT_INDICES else '#44ff44' for i in range(7)]
|
| 386 |
bars = ax1.bar(range(7), avg_probs, color=colors, alpha=0.8)
|
| 387 |
-
|
| 388 |
if consensus_class in CLASSES:
|
| 389 |
consensus_idx = CLASSES.index(consensus_class)
|
| 390 |
bars[consensus_idx].set_color('#2196F3')
|
| 391 |
bars[consensus_idx].set_linewidth(3)
|
| 392 |
bars[consensus_idx].set_edgecolor('black')
|
| 393 |
-
|
| 394 |
ax1.set_xlabel('Tipos de Lesión')
|
| 395 |
ax1.set_ylabel('Probabilidad Promedio')
|
| 396 |
ax1.set_title('📊 Distribución de Probabilidades por Clase')
|
| 397 |
ax1.set_xticks(range(7))
|
| 398 |
ax1.set_xticklabels([cls.split('(')[1].rstrip(')') for cls in CLASSES], rotation=45)
|
| 399 |
ax1.grid(True, alpha=0.3)
|
| 400 |
-
|
| 401 |
for i, bar in enumerate(bars):
|
| 402 |
height = bar.get_height()
|
| 403 |
ax1.text(bar.get_x() + bar.get_width()/2., height + 0.01,
|
| 404 |
-
|
| 405 |
-
|
| 406 |
# Gráfico 2: Confianza por modelo
|
| 407 |
valid_predictions = [p for p in predictions if p.get('success', False)]
|
| 408 |
model_names = [pred['model'].split(' ')[1] if len(pred['model'].split(' ')) > 1 else pred['model'] for pred in valid_predictions]
|
| 409 |
confidences = [pred['confidence'] for pred in valid_predictions]
|
| 410 |
-
|
| 411 |
colors_conf = ['#ff6b35' if pred['is_malignant'] else '#44ff44' for pred in valid_predictions]
|
| 412 |
bars2 = ax2.bar(range(len(valid_predictions)), confidences, color=colors_conf, alpha=0.8)
|
| 413 |
-
|
| 414 |
ax2.set_xlabel('Modelos')
|
| 415 |
ax2.set_ylabel('Confianza')
|
| 416 |
ax2.set_title('🎯 Confianza por Modelo')
|
|
@@ -418,22 +462,22 @@ def create_probability_chart(predictions, consensus_class):
|
|
| 418 |
ax2.set_xticklabels(model_names, rotation=45)
|
| 419 |
ax2.grid(True, alpha=0.3)
|
| 420 |
ax2.set_ylim(0, 1)
|
| 421 |
-
|
| 422 |
for i, bar in enumerate(bars2):
|
| 423 |
height = bar.get_height()
|
| 424 |
ax2.text(bar.get_x() + bar.get_width()/2., height + 0.01,
|
| 425 |
-
|
| 426 |
-
|
| 427 |
plt.tight_layout()
|
| 428 |
-
|
| 429 |
buf = io.BytesIO()
|
| 430 |
plt.savefig(buf, format='png', dpi=300, bbox_inches='tight')
|
| 431 |
buf.seek(0)
|
| 432 |
chart_b64 = base64.b64encode(buf.getvalue()).decode()
|
| 433 |
plt.close()
|
| 434 |
-
|
| 435 |
return f'<img src="data:image/png;base64,{chart_b64}" style="width:100%; max-width:800px;">'
|
| 436 |
-
|
| 437 |
except Exception as e:
|
| 438 |
print(f"Error creando gráfico: {e}")
|
| 439 |
return "<p>❌ Error generando gráfico de probabilidades</p>"
|
|
@@ -442,10 +486,10 @@ def create_heatmap(predictions):
|
|
| 442 |
"""Crear mapa de calor de probabilidades por modelo"""
|
| 443 |
try:
|
| 444 |
valid_predictions = [p for p in predictions if p.get('success', False)]
|
| 445 |
-
|
| 446 |
if not valid_predictions:
|
| 447 |
return "<p>No hay datos suficientes para el mapa de calor</p>"
|
| 448 |
-
|
| 449 |
prob_matrix_list = []
|
| 450 |
model_names_for_heatmap = []
|
| 451 |
for pred in valid_predictions:
|
|
@@ -454,43 +498,43 @@ def create_heatmap(predictions):
|
|
| 454 |
model_names_for_heatmap.append(pred['model'])
|
| 455 |
else:
|
| 456 |
print(f"Advertencia: Probabilidades no válidas para heatmap de {pred['model']}: {pred['probabilities']}")
|
| 457 |
-
|
| 458 |
if not prob_matrix_list:
|
| 459 |
return "<p>No hay datos válidos para el mapa de calor después de filtrar.</p>"
|
| 460 |
|
| 461 |
prob_matrix = np.array(prob_matrix_list)
|
| 462 |
-
|
| 463 |
-
fig, ax = plt.subplots(figsize=(10, len(model_names_for_heatmap) * 0.8))
|
| 464 |
-
|
| 465 |
im = ax.imshow(prob_matrix, cmap='RdYlGn_r', aspect='auto', vmin=0, vmax=1)
|
| 466 |
-
|
| 467 |
ax.set_xticks(np.arange(7))
|
| 468 |
ax.set_yticks(np.arange(len(model_names_for_heatmap)))
|
| 469 |
ax.set_xticklabels([cls.split('(')[1].rstrip(')') for cls in CLASSES])
|
| 470 |
ax.set_yticklabels(model_names_for_heatmap)
|
| 471 |
-
|
| 472 |
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
|
| 473 |
-
|
| 474 |
for i in range(len(model_names_for_heatmap)):
|
| 475 |
for j in range(7):
|
| 476 |
text = ax.text(j, i, f'{prob_matrix[i, j]:.2f}',
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
ax.set_title("Mapa de Calor: Probabilidades por Modelo y Clase")
|
| 481 |
fig.tight_layout()
|
| 482 |
-
|
| 483 |
cbar = plt.colorbar(im, ax=ax)
|
| 484 |
cbar.set_label('Probabilidad', rotation=270, labelpad=15)
|
| 485 |
-
|
| 486 |
buf = io.BytesIO()
|
| 487 |
plt.savefig(buf, format='png', dpi=300, bbox_inches='tight')
|
| 488 |
buf.seek(0)
|
| 489 |
heatmap_b64 = base64.b64encode(buf.getvalue()).decode()
|
| 490 |
plt.close()
|
| 491 |
-
|
| 492 |
return f'<img src="data:image/png;base64,{heatmap_b64}" style="width:100%; max-width:800px;">'
|
| 493 |
-
|
| 494 |
except Exception as e:
|
| 495 |
print(f"Error creando mapa de calor: {e}")
|
| 496 |
return "<p>❌ Error generando mapa de calor</p>"
|
|
@@ -500,79 +544,79 @@ def analizar_lesion(img):
|
|
| 500 |
try:
|
| 501 |
if img is None:
|
| 502 |
return "<h3>⚠️ Por favor, carga una imagen</h3>"
|
| 503 |
-
|
| 504 |
if not loaded_models or all(m.get('type') == 'dummy' for m in loaded_models.values()):
|
| 505 |
return "<h3>❌ Error del Sistema</h3><p>No hay modelos disponibles. Por favor, recarga la aplicación.</p>"
|
| 506 |
-
|
| 507 |
if img.mode != 'RGB':
|
| 508 |
img = img.convert('RGB')
|
| 509 |
-
|
| 510 |
predictions = []
|
| 511 |
-
|
| 512 |
for model_name, model_data in loaded_models.items():
|
| 513 |
if model_data.get('type') != 'dummy':
|
| 514 |
pred = predict_with_model(img, model_data)
|
| 515 |
if pred.get('success', False):
|
| 516 |
predictions.append(pred)
|
| 517 |
-
|
| 518 |
if not predictions:
|
| 519 |
return "<h3>❌ Error</h3><p>No se pudieron obtener predicciones de ningún modelo.</p>"
|
| 520 |
-
|
| 521 |
# Análisis de consenso
|
| 522 |
class_votes = {}
|
| 523 |
confidence_sum = {}
|
| 524 |
-
|
| 525 |
for pred in predictions:
|
| 526 |
class_name = pred['class']
|
| 527 |
confidence = pred['confidence']
|
| 528 |
-
|
| 529 |
if class_name not in class_votes:
|
| 530 |
class_votes[class_name] = 0
|
| 531 |
confidence_sum[class_name] = 0
|
| 532 |
-
|
| 533 |
class_votes[class_name] += 1
|
| 534 |
confidence_sum[class_name] += confidence
|
| 535 |
-
|
| 536 |
# Manejar el caso donde no hay votos por alguna razón (aunque predictions ya valida que hay)
|
| 537 |
if not class_votes:
|
| 538 |
return "<h3>❌ Error en el Consenso</h3><p>No se pudieron consolidar los votos de los modelos.</p>"
|
| 539 |
-
|
| 540 |
consensus_class = max(class_votes.keys(), key=lambda x: class_votes[x])
|
| 541 |
avg_confidence = confidence_sum[consensus_class] / class_votes[consensus_class]
|
| 542 |
-
|
| 543 |
consensus_idx = CLASSES.index(consensus_class)
|
| 544 |
is_malignant = consensus_idx in MALIGNANT_INDICES
|
| 545 |
risk_info = RISK_LEVELS[consensus_idx]
|
| 546 |
-
|
| 547 |
probability_chart = create_probability_chart(predictions, consensus_class)
|
| 548 |
heatmap = create_heatmap(predictions)
|
| 549 |
-
|
| 550 |
html_report = f"""
|
| 551 |
<div style="font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto;">
|
| 552 |
<h2 style="color: #2c3e50; text-align: center;">🏥 Análisis Completo de Lesión Cutánea</h2>
|
| 553 |
-
|
| 554 |
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 10px; margin: 20px 0;">
|
| 555 |
<h3 style="margin: 0; text-align: center;">📋 Resultado de Consenso</h3>
|
| 556 |
<p style="font-size: 18px; text-align: center; margin: 10px 0;"><strong>{consensus_class}</strong></p>
|
| 557 |
<p style="text-align: center; margin: 5px 0;">Confianza Promedio: <strong>{avg_confidence:.1%}</strong></p>
|
| 558 |
<p style="text-align: center; margin: 5px 0;">Consenso: <strong>{class_votes[consensus_class]}/{len(predictions)} modelos</strong></p>
|
| 559 |
</div>
|
| 560 |
-
|
| 561 |
<div style="background: {risk_info['color']}; color: white; padding: 15px; border-radius: 8px; margin: 15px 0;">
|
| 562 |
<h4 style="margin: 0;">⚠️ Nivel de Riesgo: {risk_info['level']}</h4>
|
| 563 |
<p style="margin: 5px 0;"><strong>{risk_info['urgency']}</strong></p>
|
| 564 |
<p style="margin: 5px 0;">Tipo: {'🔴 Potencialmente maligna' if is_malignant else '🟢 Probablemente benigna'}</p>
|
| 565 |
</div>
|
| 566 |
-
|
| 567 |
<div style="background: #e3f2fd; padding: 15px; border-radius: 8px; margin: 15px 0;">
|
| 568 |
<h4 style="color: #1976d2;">🤖 Resultados Individuales por Modelo</h4>
|
| 569 |
<p style="font-size: 0.9em; color: #555;">
|
| 570 |
A continuación se detallan las predicciones de cada modelo. Es importante destacar que los <strong>modelos entrenados específicamente en lesiones de piel (Categoría: Especializados) suelen ser más fiables</strong> para este tipo de análisis que los modelos generales.
|
| 571 |
</p>
|
| 572 |
"""
|
| 573 |
-
|
| 574 |
# RESULTADOS INDIVIDUALES DETALLADOS - Separados por categoría
|
| 575 |
-
|
| 576 |
# Especializados
|
| 577 |
html_report += """
|
| 578 |
<h5 style="color: #007bff; border-bottom: 1px solid #007bff; padding-bottom: 5px; margin-top: 20px;">
|
|
@@ -585,31 +629,31 @@ def analizar_lesion(img):
|
|
| 585 |
specialized_models_found = True
|
| 586 |
model_risk = RISK_LEVELS[pred['predicted_idx']]
|
| 587 |
malignant_status = "🔴 Maligna" if pred['is_malignant'] else "🟢 Benigna"
|
| 588 |
-
|
| 589 |
html_report += f"""
|
| 590 |
<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);">
|
| 591 |
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
|
| 592 |
<h5 style="margin: 0; color: #333;">{pred['model']}</h5>
|
| 593 |
<span style="background: {model_risk['color']}; color: white; padding: 4px 8px; border-radius: 4px; font-size: 12px;">{model_risk['level']}</span>
|
| 594 |
</div>
|
| 595 |
-
|
| 596 |
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; font-size: 14px;">
|
| 597 |
<div><strong>Diagnóstico:</strong><br>{pred['class']}</div>
|
| 598 |
<div><strong>Confianza:</strong><br>{pred['confidence']:.1%}</div>
|
| 599 |
<div><strong>Clasificación:</strong><br>{malignant_status}</div>
|
| 600 |
</div>
|
| 601 |
-
|
| 602 |
<div style="margin-top: 10px;">
|
| 603 |
<strong>Top 3 Probabilidades:</strong><br>
|
| 604 |
<div style="font-size: 12px; color: #666;">
|
| 605 |
"""
|
| 606 |
-
|
| 607 |
top_indices = np.argsort(pred['probabilities'])[-3:][::-1]
|
| 608 |
for idx in top_indices:
|
| 609 |
prob = pred['probabilities'][idx]
|
| 610 |
if prob > 0.01:
|
| 611 |
html_report += f"• {CLASSES[idx].split('(')[1].rstrip(')')}: {prob:.1%}<br>"
|
| 612 |
-
|
| 613 |
html_report += f"""
|
| 614 |
</div>
|
| 615 |
<div style="margin-top: 8px; font-size: 12px; color: #888;">
|
|
@@ -636,31 +680,31 @@ def analizar_lesion(img):
|
|
| 636 |
general_models_found = True
|
| 637 |
model_risk = RISK_LEVELS[pred['predicted_idx']]
|
| 638 |
malignant_status = "🔴 Maligna" if pred['is_malignant'] else "🟢 Benigna"
|
| 639 |
-
|
| 640 |
html_report += f"""
|
| 641 |
<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);">
|
| 642 |
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
|
| 643 |
<h5 style="margin: 0; color: #333;">{pred['model']}</h5>
|
| 644 |
<span style="background: {model_risk['color']}; color: white; padding: 4px 8px; border-radius: 4px; font-size: 12px;">{model_risk['level']}</span>
|
| 645 |
</div>
|
| 646 |
-
|
| 647 |
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; font-size: 14px;">
|
| 648 |
<div><strong>Diagnóstico:</strong><br>{pred['class']}</div>
|
| 649 |
<div><strong>Confianza:</strong><br>{pred['confidence']:.1%}</div>
|
| 650 |
<div><strong>Clasificación:</strong><br>{malignant_status}</div>
|
| 651 |
</div>
|
| 652 |
-
|
| 653 |
<div style="margin-top: 10px;">
|
| 654 |
<strong>Top 3 Probabilidades:</strong><br>
|
| 655 |
<div style="font-size: 12px; color: #666;">
|
| 656 |
"""
|
| 657 |
-
|
| 658 |
top_indices = np.argsort(pred['probabilities'])[-3:][::-1]
|
| 659 |
for idx in top_indices:
|
| 660 |
prob = pred['probabilities'][idx]
|
| 661 |
if prob > 0.01:
|
| 662 |
html_report += f"• {CLASSES[idx].split('(')[1].rstrip(')')}: {prob:.1%}<br>"
|
| 663 |
-
|
| 664 |
html_report += f"""
|
| 665 |
</div>
|
| 666 |
<div style="margin-top: 8px; font-size: 12px; color: #888;">
|
|
@@ -671,113 +715,98 @@ def analizar_lesion(img):
|
|
| 671 |
"""
|
| 672 |
if not general_models_found:
|
| 673 |
html_report += "<p style='color: #888;'>No se cargaron modelos generales o fallaron al predecir.</p>"
|
| 674 |
-
|
| 675 |
html_report += f"""
|
| 676 |
</div>
|
| 677 |
-
|
| 678 |
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px; margin: 15px 0;">
|
| 679 |
<h4 style="color: #495057;">📊 Análisis Estadístico</h4>
|
| 680 |
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px;">
|
| 681 |
<div>
|
| 682 |
-
|
| 683 |
-
<strong>Acuerdo Total:</strong> {class_votes[consensus_class]}/{len([p for p in predictions if p['success']])}<br>
|
| 684 |
-
<strong>Confianza Máxima:</strong> {max([p['confidence'] for p in predictions if p['success']]):.1%}
|
| 685 |
</div>
|
| 686 |
<div>
|
| 687 |
-
|
| 688 |
-
<strong>Diagnósticos Benignos:</strong> {len([p for p in predictions if p.get('success') and not p.get('is_malignant')])}<br>
|
| 689 |
-
<strong>Consenso Maligno:</strong> {'Sí' if is_malignant else 'No'}
|
| 690 |
</div>
|
| 691 |
</div>
|
| 692 |
</div>
|
| 693 |
-
|
| 694 |
-
<div style="background: #
|
| 695 |
-
<h4 style="
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
<h4 style="color: #f57c00;">⚠️ Advertencia Médica</h4>
|
| 706 |
-
<p style="margin: 5px 0;">Este análisis es solo una herramienta de apoyo diagnóstico basada en IA.</p>
|
| 707 |
-
<p style="margin: 5px 0;"><strong>Siempre consulte con un dermatólogo profesional para un diagnóstico definitivo.</strong></p>
|
| 708 |
-
<p style="margin: 5px 0;">No utilice esta información como único criterio para decisiones médicas.</p>
|
| 709 |
-
<p style="margin: 5px 0;"><em>Los resultados individuales de cada modelo se muestran para transparencia y análisis comparativo.</em></p>
|
| 710 |
</div>
|
| 711 |
</div>
|
| 712 |
"""
|
| 713 |
-
|
| 714 |
return html_report
|
| 715 |
-
|
| 716 |
except Exception as e:
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
#
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
print(f"🚀 Modelos cargados exitosamente: {len(loaded_models)}")
|
| 780 |
-
print(f"🎯 Estado: {'✅ Operativo' if loaded_models else '❌ Sin modelos'}")
|
| 781 |
-
|
| 782 |
-
demo = create_interface()
|
| 783 |
-
demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 38 |
},
|
| 39 |
{
|
| 40 |
'name': 'Anwarkh1 Skin Cancer',
|
| 41 |
+
'id': 'Anwarkh1/Skin_Cancer-Image_Classification',
|
| 42 |
'type': 'vit',
|
| 43 |
'accuracy': 0.89,
|
| 44 |
'description': 'Clasificador multi-clase de lesiones de piel',
|
|
|
|
| 75 |
'name': 'ViT Base General',
|
| 76 |
'id': 'google/vit-base-patch16-224',
|
| 77 |
'type': 'vit',
|
| 78 |
+
'accuracy': 0.78,
|
| 79 |
'description': 'ViT base pre-entrenado en ImageNet-1k. Excelente para características visuales generales.',
|
| 80 |
'emoji': '📈'
|
| 81 |
},
|
| 82 |
{
|
| 83 |
'name': 'ResNet-50 (Microsoft)',
|
| 84 |
'id': 'microsoft/resnet-50',
|
| 85 |
+
'type': 'custom',
|
| 86 |
+
'accuracy': 0.77,
|
| 87 |
'description': 'Un clásico ResNet-50, robusto y de alto rendimiento en clasificación de imágenes generales.',
|
| 88 |
'emoji': '⚙️'
|
| 89 |
},
|
|
|
|
| 91 |
'name': 'DeiT Base (Facebook)',
|
| 92 |
'id': 'facebook/deit-base-patch16-224',
|
| 93 |
'type': 'vit',
|
| 94 |
+
'accuracy': 0.79,
|
| 95 |
'description': 'Data-efficient Image Transformer, eficiente y de buen rendimiento general.',
|
| 96 |
'emoji': '💡'
|
| 97 |
},
|
|
|
|
| 99 |
'name': 'MobileNetV2 (Google)',
|
| 100 |
'id': 'google/mobilenet_v2_1.0_224',
|
| 101 |
'type': 'custom',
|
| 102 |
+
'accuracy': 0.72,
|
| 103 |
'description': 'MobileNetV2, modelo ligero y rápido, ideal para entornos con recursos limitados.',
|
| 104 |
'emoji': '📱'
|
| 105 |
},
|
| 106 |
{
|
| 107 |
'name': 'Swin Tiny (Microsoft)',
|
| 108 |
+
'id': 'microsoft/swin-tiny-patch4-window7-224',
|
| 109 |
+
'type': 'custom',
|
| 110 |
+
'accuracy': 0.81,
|
| 111 |
'description': 'Swin Transformer (Tiny), potente para visión por computadora.',
|
| 112 |
'emoji': '🌀'
|
| 113 |
},
|
|
|
|
| 128 |
model_performance = {}
|
| 129 |
|
| 130 |
def load_model_safe(config):
|
| 131 |
+
"""Carga segura de modelos con manejo de errores mejorado y revisiones específicas."""
|
| 132 |
try:
|
| 133 |
model_id = config['id']
|
| 134 |
model_type = config['type']
|
| 135 |
print(f"🔄 Cargando {config['emoji']} {config['name']}...")
|
| 136 |
+
|
| 137 |
+
# Intentar cargar con revisiones específicas para evitar problemas de safetensors/float16
|
| 138 |
+
# Si PyTorch es 2.6.0, es posible que 'safetensors' aún no sea 100% estable en todos los modelos/configuraciones
|
| 139 |
+
# y que el soporte de float16 requiera revisión específica.
|
| 140 |
+
revisions_to_try = ["main", "no_float16_weights", None] # None intentará el valor por defecto
|
| 141 |
+
|
| 142 |
+
processor = None
|
| 143 |
+
model = None
|
| 144 |
+
load_successful = False
|
| 145 |
+
|
| 146 |
+
for revision in revisions_to_try:
|
| 147 |
+
try:
|
| 148 |
+
if revision:
|
| 149 |
+
print(f" Intentando revisión: {revision}")
|
| 150 |
+
processor = AutoImageProcessor.from_pretrained(model_id, revision=revision)
|
| 151 |
+
model = AutoModelForImageClassification.from_pretrained(model_id, revision=revision)
|
| 152 |
+
else:
|
| 153 |
+
processor = AutoImageProcessor.from_pretrained(model_id)
|
| 154 |
+
model = AutoModelForImageClassification.from_pretrained(model_id)
|
| 155 |
+
load_successful = True
|
| 156 |
+
break # Éxito en la carga, salir del bucle de revisiones
|
| 157 |
+
except Exception as e_rev:
|
| 158 |
+
print(f" Fallo con revisión '{revision}': {e_rev}")
|
| 159 |
+
if model_type == 'vit' and revision is None: # Si el tipo es 'vit' y la carga inicial falló, probar ViTImageProcessor
|
| 160 |
+
try:
|
| 161 |
+
processor = ViTImageProcessor.from_pretrained(model_id)
|
| 162 |
+
model = ViTForImageClassification.from_pretrained(model_id)
|
| 163 |
+
load_successful = True
|
| 164 |
+
break
|
| 165 |
+
except Exception as e_vit:
|
| 166 |
+
print(f" Fallo con ViTImageProcessor/ViTForImageClassification: {e_vit}")
|
| 167 |
+
continue # Intentar la siguiente revisión
|
| 168 |
+
|
| 169 |
+
if not load_successful:
|
| 170 |
+
raise Exception("No se pudo cargar el modelo con ninguna revisión o método alternativo.")
|
| 171 |
+
|
| 172 |
model.eval()
|
| 173 |
+
|
| 174 |
# Verificar que el modelo funciona con una entrada dummy
|
| 175 |
test_input = processor(Image.new('RGB', (224, 224), color='white'), return_tensors="pt")
|
| 176 |
with torch.no_grad():
|
| 177 |
test_output = model(**test_input)
|
| 178 |
+
|
| 179 |
print(f"✅ {config['emoji']} {config['name']} cargado exitosamente")
|
| 180 |
+
|
| 181 |
return {
|
| 182 |
'processor': processor,
|
| 183 |
'model': model,
|
|
|
|
| 185 |
'output_dim': test_output.logits.shape[-1] if hasattr(test_output, 'logits') else len(test_output[0]),
|
| 186 |
'category': config.get('category', 'general') # Añadimos la categoría aquí
|
| 187 |
}
|
| 188 |
+
|
| 189 |
except Exception as e:
|
| 190 |
print(f"❌ {config['emoji']} {config['name']} falló: {e}")
|
| 191 |
print(f" Error detallado: {type(e).__name__}")
|
|
|
|
| 197 |
for category, configs in MODEL_CONFIGS.items():
|
| 198 |
for config in configs:
|
| 199 |
# Añadir la categoría al diccionario de configuración antes de pasar a load_model_safe
|
| 200 |
+
config['category'] = category
|
| 201 |
model_data = load_model_safe(config)
|
| 202 |
if model_data:
|
| 203 |
loaded_models[config['name']] = model_data
|
|
|
|
| 211 |
'microsoft/resnet-50',
|
| 212 |
'google/vit-large-patch16-224'
|
| 213 |
]
|
| 214 |
+
|
| 215 |
for fallback_id in fallback_models:
|
| 216 |
try:
|
| 217 |
print(f"🔄 Intentando modelo de respaldo: {fallback_id}")
|
| 218 |
processor = AutoImageProcessor.from_pretrained(fallback_id)
|
| 219 |
model = AutoModelForImageClassification.from_pretrained(fallback_id)
|
| 220 |
model.eval()
|
| 221 |
+
|
| 222 |
loaded_models[f'Respaldo-{fallback_id.split("/")[-1]}'] = {
|
| 223 |
'processor': processor,
|
| 224 |
'model': model,
|
| 225 |
'config': {
|
| 226 |
+
'name': f'Respaldo {fallback_id.split("/")[-1]}',
|
| 227 |
+
'emoji': '🏥',
|
| 228 |
'accuracy': 0.75,
|
| 229 |
'type': 'fallback',
|
| 230 |
'category': 'general' # El de respaldo es general
|
|
|
|
| 237 |
except Exception as e:
|
| 238 |
print(f"❌ Respaldo {fallback_id} falló: {e}")
|
| 239 |
continue
|
| 240 |
+
|
| 241 |
if not loaded_models:
|
| 242 |
print(f"❌ ERROR CRÍTICO: No se pudo cargar ningún modelo")
|
| 243 |
print("💡 Verifica tu conexión a internet y que tengas transformers instalado")
|
|
|
|
| 249 |
|
| 250 |
# Clases de lesiones de piel (HAM10000 dataset)
|
| 251 |
CLASSES = [
|
| 252 |
+
"Queratosis actínica / Bowen (AKIEC)",
|
| 253 |
"Carcinoma células basales (BCC)",
|
| 254 |
+
"Lesión queratósica benigna (BKL)",
|
| 255 |
+
"Dermatofibroma (DF)",
|
| 256 |
+
"Melanoma maligno (MEL)",
|
| 257 |
+
"Nevus melanocítico (NV)",
|
| 258 |
"Lesión vascular (VASC)"
|
| 259 |
]
|
| 260 |
|
|
|
|
| 275 |
"""Predicción con un modelo específico - versión mejorada"""
|
| 276 |
try:
|
| 277 |
config = model_data['config']
|
| 278 |
+
|
| 279 |
# Redimensionar imagen
|
| 280 |
image_resized = image.resize((224, 224), Image.LANCZOS)
|
| 281 |
+
|
| 282 |
if model_data.get('type') == 'pipeline': # Esto debería ser poco común con la lista actual
|
| 283 |
pipeline = model_data['pipeline']
|
| 284 |
results = pipeline(image_resized)
|
| 285 |
+
|
| 286 |
if isinstance(results, list) and len(results) > 0:
|
| 287 |
+
mapped_probs = np.ones(7) / 7
|
| 288 |
confidence = results[0]['score'] if 'score' in results[0] else 0.5
|
| 289 |
+
|
| 290 |
label = results[0].get('label', '').lower()
|
| 291 |
if any(word in label for word in ['melanoma', 'mel', 'malignant', 'cancer']):
|
| 292 |
+
predicted_idx = 4
|
| 293 |
elif any(word in label for word in ['carcinoma', 'bcc', 'basal']):
|
| 294 |
+
predicted_idx = 1
|
| 295 |
elif any(word in label for word in ['keratosis', 'akiec']):
|
| 296 |
+
predicted_idx = 0
|
| 297 |
elif any(word in label for word in ['nevus', 'nv', 'benign']):
|
| 298 |
+
predicted_idx = 5
|
| 299 |
else:
|
| 300 |
+
predicted_idx = 2
|
| 301 |
+
|
| 302 |
mapped_probs[predicted_idx] = confidence
|
| 303 |
remaining_sum = (1.0 - confidence)
|
| 304 |
+
if remaining_sum < 0: remaining_sum = 0
|
| 305 |
+
|
| 306 |
+
num_other_classes = 6
|
| 307 |
if num_other_classes > 0:
|
| 308 |
remaining_per_class = remaining_sum / num_other_classes
|
| 309 |
for i in range(7):
|
| 310 |
if i != predicted_idx:
|
| 311 |
mapped_probs[i] = remaining_per_class
|
| 312 |
+
|
| 313 |
else:
|
| 314 |
mapped_probs = np.ones(7) / 7
|
| 315 |
+
predicted_idx = 5
|
| 316 |
confidence = 0.3
|
| 317 |
+
|
| 318 |
else: # Usar modelo estándar (AutoModel/ViT)
|
| 319 |
processor = model_data['processor']
|
| 320 |
model = model_data['model']
|
| 321 |
+
|
| 322 |
inputs = processor(image_resized, return_tensors="pt")
|
| 323 |
+
|
| 324 |
with torch.no_grad():
|
| 325 |
outputs = model(**inputs)
|
| 326 |
+
|
| 327 |
if hasattr(outputs, 'logits'):
|
| 328 |
logits = outputs.logits
|
| 329 |
else:
|
| 330 |
logits = outputs[0] if isinstance(outputs, (tuple, list)) else outputs
|
| 331 |
+
|
| 332 |
probabilities = F.softmax(logits, dim=-1).cpu().numpy()[0]
|
| 333 |
+
|
| 334 |
# --- Mapeo de probabilidades según el número de clases de salida del modelo ---
|
| 335 |
if len(probabilities) == 7: # Modelos ya entrenados para 7 clases de piel
|
| 336 |
mapped_probs = probabilities
|
|
|
|
| 349 |
mapped_probs[3] = probabilities[0] * 0.1 # DF
|
| 350 |
mapped_probs[6] = probabilities[0] * 0.1 # VASC
|
| 351 |
mapped_probs = mapped_probs / np.sum(mapped_probs) # Normalizar para que sumen 1
|
| 352 |
+
elif len(probabilities) in [1000, 900]: # Modelos generales como los de ImageNet (1000 clases) o modelos preentrenados en ImageNet-21k (900 clases)
|
| 353 |
+
mapped_probs = np.zeros(7)
|
| 354 |
+
# Intentar mapear las clases del modelo a las clases de piel si hay un id2label
|
| 355 |
+
if hasattr(model, 'config') and hasattr(model.config, 'id2label'):
|
| 356 |
+
model_labels = {v.lower(): k for k, v in model.config.id2label.items()}
|
| 357 |
+
# Asignar probabilidades a las clases de piel si coinciden
|
| 358 |
+
for i, skin_class in enumerate(CLASSES):
|
| 359 |
+
# Intentar buscar la etiqueta completa o una parte clave
|
| 360 |
+
key_words = skin_class.split('(')[1].rstrip(')').lower().split()
|
| 361 |
+
found = False
|
| 362 |
+
for key_word in key_words:
|
| 363 |
+
for model_label, model_idx in model_labels.items():
|
| 364 |
+
if key_word in model_label:
|
| 365 |
+
# Sumar la probabilidad de la clase del modelo a la clase de piel
|
| 366 |
+
mapped_probs[i] += probabilities[model_idx]
|
| 367 |
+
found = True
|
| 368 |
+
break
|
| 369 |
+
if found: break # Ya encontramos una coincidencia para esta clase de piel
|
| 370 |
+
|
| 371 |
+
# Si después del intento de mapeo, las probabilidades son cero o muy bajas,
|
| 372 |
+
# o si no hay id2label, usar la distribución uniforme (o heurística)
|
| 373 |
+
if np.sum(mapped_probs) == 0:
|
| 374 |
+
print(f"Advertencia: No se pudo mapear clases específicas para {config['name']} ({len(probabilities)} clases). Usando distribución heurística.")
|
| 375 |
+
mapped_probs = np.ones(7) / 7 # Empezamos con distribución uniforme
|
| 376 |
+
# Ajuste heurístico: Asignamos un poco más de peso a clases benignas por defecto
|
| 377 |
+
mapped_probs[5] += 0.1 # Aumentar Nevus (NV) ligeramente
|
| 378 |
+
mapped_probs[2] += 0.05 # Aumentar Lesión queratósica benigna (BKL) ligeramente
|
| 379 |
+
mapped_probs = mapped_probs / np.sum(mapped_probs) # Re-normalizar
|
| 380 |
+
else:
|
| 381 |
+
mapped_probs = mapped_probs / np.sum(mapped_probs) # Normalizar las probabilidades mapeadas
|
| 382 |
else: # Otros casos de dimensiones de salida no esperadas: distribución uniforme
|
| 383 |
print(f"Advertencia: Dimensión de salida inesperada para {config['name']} ({len(probabilities)} clases). Usando distribución uniforme.")
|
| 384 |
mapped_probs = np.ones(7) / 7
|
| 385 |
+
|
| 386 |
predicted_idx = int(np.argmax(mapped_probs))
|
| 387 |
confidence = float(mapped_probs[predicted_idx])
|
| 388 |
+
|
| 389 |
return {
|
| 390 |
'model': f"{config['emoji']} {config['name']}",
|
| 391 |
'class': CLASSES[predicted_idx],
|
|
|
|
| 396 |
'success': True,
|
| 397 |
'category': model_data['category'] # Añadir la categoría de vuelta
|
| 398 |
}
|
| 399 |
+
|
| 400 |
except Exception as e:
|
| 401 |
print(f"❌ Error en {config['name']}: {e}")
|
| 402 |
return {
|
|
|
|
| 410 |
"""Crear gráfico de barras con probabilidades"""
|
| 411 |
try:
|
| 412 |
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
|
| 413 |
+
|
| 414 |
# Gráfico 1: Probabilidades por clase (consenso)
|
| 415 |
if predictions:
|
| 416 |
avg_probs = np.zeros(7)
|
| 417 |
valid_predictions = [p for p in predictions if p.get('success', False)]
|
| 418 |
+
|
| 419 |
if len(valid_predictions) > 0:
|
| 420 |
for pred in valid_predictions:
|
| 421 |
if isinstance(pred['probabilities'], np.ndarray) and len(pred['probabilities']) == 7 and not np.isnan(pred['probabilities']).any():
|
|
|
|
| 424 |
print(f"Advertencia: Probabilidades no válidas para {pred['model']}: {pred['probabilities']}")
|
| 425 |
avg_probs /= len(valid_predictions)
|
| 426 |
else:
|
| 427 |
+
avg_probs = np.ones(7) / 7
|
| 428 |
+
|
| 429 |
colors = ['#ff6b35' if i in MALIGNANT_INDICES else '#44ff44' for i in range(7)]
|
| 430 |
bars = ax1.bar(range(7), avg_probs, color=colors, alpha=0.8)
|
| 431 |
+
|
| 432 |
if consensus_class in CLASSES:
|
| 433 |
consensus_idx = CLASSES.index(consensus_class)
|
| 434 |
bars[consensus_idx].set_color('#2196F3')
|
| 435 |
bars[consensus_idx].set_linewidth(3)
|
| 436 |
bars[consensus_idx].set_edgecolor('black')
|
| 437 |
+
|
| 438 |
ax1.set_xlabel('Tipos de Lesión')
|
| 439 |
ax1.set_ylabel('Probabilidad Promedio')
|
| 440 |
ax1.set_title('📊 Distribución de Probabilidades por Clase')
|
| 441 |
ax1.set_xticks(range(7))
|
| 442 |
ax1.set_xticklabels([cls.split('(')[1].rstrip(')') for cls in CLASSES], rotation=45)
|
| 443 |
ax1.grid(True, alpha=0.3)
|
| 444 |
+
|
| 445 |
for i, bar in enumerate(bars):
|
| 446 |
height = bar.get_height()
|
| 447 |
ax1.text(bar.get_x() + bar.get_width()/2., height + 0.01,
|
| 448 |
+
f'{height:.2%}', ha='center', va='bottom', fontsize=9)
|
| 449 |
+
|
| 450 |
# Gráfico 2: Confianza por modelo
|
| 451 |
valid_predictions = [p for p in predictions if p.get('success', False)]
|
| 452 |
model_names = [pred['model'].split(' ')[1] if len(pred['model'].split(' ')) > 1 else pred['model'] for pred in valid_predictions]
|
| 453 |
confidences = [pred['confidence'] for pred in valid_predictions]
|
| 454 |
+
|
| 455 |
colors_conf = ['#ff6b35' if pred['is_malignant'] else '#44ff44' for pred in valid_predictions]
|
| 456 |
bars2 = ax2.bar(range(len(valid_predictions)), confidences, color=colors_conf, alpha=0.8)
|
| 457 |
+
|
| 458 |
ax2.set_xlabel('Modelos')
|
| 459 |
ax2.set_ylabel('Confianza')
|
| 460 |
ax2.set_title('🎯 Confianza por Modelo')
|
|
|
|
| 462 |
ax2.set_xticklabels(model_names, rotation=45)
|
| 463 |
ax2.grid(True, alpha=0.3)
|
| 464 |
ax2.set_ylim(0, 1)
|
| 465 |
+
|
| 466 |
for i, bar in enumerate(bars2):
|
| 467 |
height = bar.get_height()
|
| 468 |
ax2.text(bar.get_x() + bar.get_width()/2., height + 0.01,
|
| 469 |
+
f'{height:.1%}', ha='center', va='bottom', fontsize=9)
|
| 470 |
+
|
| 471 |
plt.tight_layout()
|
| 472 |
+
|
| 473 |
buf = io.BytesIO()
|
| 474 |
plt.savefig(buf, format='png', dpi=300, bbox_inches='tight')
|
| 475 |
buf.seek(0)
|
| 476 |
chart_b64 = base64.b64encode(buf.getvalue()).decode()
|
| 477 |
plt.close()
|
| 478 |
+
|
| 479 |
return f'<img src="data:image/png;base64,{chart_b64}" style="width:100%; max-width:800px;">'
|
| 480 |
+
|
| 481 |
except Exception as e:
|
| 482 |
print(f"Error creando gráfico: {e}")
|
| 483 |
return "<p>❌ Error generando gráfico de probabilidades</p>"
|
|
|
|
| 486 |
"""Crear mapa de calor de probabilidades por modelo"""
|
| 487 |
try:
|
| 488 |
valid_predictions = [p for p in predictions if p.get('success', False)]
|
| 489 |
+
|
| 490 |
if not valid_predictions:
|
| 491 |
return "<p>No hay datos suficientes para el mapa de calor</p>"
|
| 492 |
+
|
| 493 |
prob_matrix_list = []
|
| 494 |
model_names_for_heatmap = []
|
| 495 |
for pred in valid_predictions:
|
|
|
|
| 498 |
model_names_for_heatmap.append(pred['model'])
|
| 499 |
else:
|
| 500 |
print(f"Advertencia: Probabilidades no válidas para heatmap de {pred['model']}: {pred['probabilities']}")
|
| 501 |
+
|
| 502 |
if not prob_matrix_list:
|
| 503 |
return "<p>No hay datos válidos para el mapa de calor después de filtrar.</p>"
|
| 504 |
|
| 505 |
prob_matrix = np.array(prob_matrix_list)
|
| 506 |
+
|
| 507 |
+
fig, ax = plt.subplots(figsize=(10, len(model_names_for_heatmap) * 0.8))
|
| 508 |
+
|
| 509 |
im = ax.imshow(prob_matrix, cmap='RdYlGn_r', aspect='auto', vmin=0, vmax=1)
|
| 510 |
+
|
| 511 |
ax.set_xticks(np.arange(7))
|
| 512 |
ax.set_yticks(np.arange(len(model_names_for_heatmap)))
|
| 513 |
ax.set_xticklabels([cls.split('(')[1].rstrip(')') for cls in CLASSES])
|
| 514 |
ax.set_yticklabels(model_names_for_heatmap)
|
| 515 |
+
|
| 516 |
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
|
| 517 |
+
|
| 518 |
for i in range(len(model_names_for_heatmap)):
|
| 519 |
for j in range(7):
|
| 520 |
text = ax.text(j, i, f'{prob_matrix[i, j]:.2f}',
|
| 521 |
+
ha="center", va="center", color="white" if prob_matrix[i, j] > 0.5 else "black",
|
| 522 |
+
fontsize=8)
|
| 523 |
+
|
| 524 |
ax.set_title("Mapa de Calor: Probabilidades por Modelo y Clase")
|
| 525 |
fig.tight_layout()
|
| 526 |
+
|
| 527 |
cbar = plt.colorbar(im, ax=ax)
|
| 528 |
cbar.set_label('Probabilidad', rotation=270, labelpad=15)
|
| 529 |
+
|
| 530 |
buf = io.BytesIO()
|
| 531 |
plt.savefig(buf, format='png', dpi=300, bbox_inches='tight')
|
| 532 |
buf.seek(0)
|
| 533 |
heatmap_b64 = base64.b64encode(buf.getvalue()).decode()
|
| 534 |
plt.close()
|
| 535 |
+
|
| 536 |
return f'<img src="data:image/png;base64,{heatmap_b64}" style="width:100%; max-width:800px;">'
|
| 537 |
+
|
| 538 |
except Exception as e:
|
| 539 |
print(f"Error creando mapa de calor: {e}")
|
| 540 |
return "<p>❌ Error generando mapa de calor</p>"
|
|
|
|
| 544 |
try:
|
| 545 |
if img is None:
|
| 546 |
return "<h3>⚠️ Por favor, carga una imagen</h3>"
|
| 547 |
+
|
| 548 |
if not loaded_models or all(m.get('type') == 'dummy' for m in loaded_models.values()):
|
| 549 |
return "<h3>❌ Error del Sistema</h3><p>No hay modelos disponibles. Por favor, recarga la aplicación.</p>"
|
| 550 |
+
|
| 551 |
if img.mode != 'RGB':
|
| 552 |
img = img.convert('RGB')
|
| 553 |
+
|
| 554 |
predictions = []
|
| 555 |
+
|
| 556 |
for model_name, model_data in loaded_models.items():
|
| 557 |
if model_data.get('type') != 'dummy':
|
| 558 |
pred = predict_with_model(img, model_data)
|
| 559 |
if pred.get('success', False):
|
| 560 |
predictions.append(pred)
|
| 561 |
+
|
| 562 |
if not predictions:
|
| 563 |
return "<h3>❌ Error</h3><p>No se pudieron obtener predicciones de ningún modelo.</p>"
|
| 564 |
+
|
| 565 |
# Análisis de consenso
|
| 566 |
class_votes = {}
|
| 567 |
confidence_sum = {}
|
| 568 |
+
|
| 569 |
for pred in predictions:
|
| 570 |
class_name = pred['class']
|
| 571 |
confidence = pred['confidence']
|
| 572 |
+
|
| 573 |
if class_name not in class_votes:
|
| 574 |
class_votes[class_name] = 0
|
| 575 |
confidence_sum[class_name] = 0
|
| 576 |
+
|
| 577 |
class_votes[class_name] += 1
|
| 578 |
confidence_sum[class_name] += confidence
|
| 579 |
+
|
| 580 |
# Manejar el caso donde no hay votos por alguna razón (aunque predictions ya valida que hay)
|
| 581 |
if not class_votes:
|
| 582 |
return "<h3>❌ Error en el Consenso</h3><p>No se pudieron consolidar los votos de los modelos.</p>"
|
| 583 |
+
|
| 584 |
consensus_class = max(class_votes.keys(), key=lambda x: class_votes[x])
|
| 585 |
avg_confidence = confidence_sum[consensus_class] / class_votes[consensus_class]
|
| 586 |
+
|
| 587 |
consensus_idx = CLASSES.index(consensus_class)
|
| 588 |
is_malignant = consensus_idx in MALIGNANT_INDICES
|
| 589 |
risk_info = RISK_LEVELS[consensus_idx]
|
| 590 |
+
|
| 591 |
probability_chart = create_probability_chart(predictions, consensus_class)
|
| 592 |
heatmap = create_heatmap(predictions)
|
| 593 |
+
|
| 594 |
html_report = f"""
|
| 595 |
<div style="font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto;">
|
| 596 |
<h2 style="color: #2c3e50; text-align: center;">🏥 Análisis Completo de Lesión Cutánea</h2>
|
| 597 |
+
|
| 598 |
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 10px; margin: 20px 0;">
|
| 599 |
<h3 style="margin: 0; text-align: center;">📋 Resultado de Consenso</h3>
|
| 600 |
<p style="font-size: 18px; text-align: center; margin: 10px 0;"><strong>{consensus_class}</strong></p>
|
| 601 |
<p style="text-align: center; margin: 5px 0;">Confianza Promedio: <strong>{avg_confidence:.1%}</strong></p>
|
| 602 |
<p style="text-align: center; margin: 5px 0;">Consenso: <strong>{class_votes[consensus_class]}/{len(predictions)} modelos</strong></p>
|
| 603 |
</div>
|
| 604 |
+
|
| 605 |
<div style="background: {risk_info['color']}; color: white; padding: 15px; border-radius: 8px; margin: 15px 0;">
|
| 606 |
<h4 style="margin: 0;">⚠️ Nivel de Riesgo: {risk_info['level']}</h4>
|
| 607 |
<p style="margin: 5px 0;"><strong>{risk_info['urgency']}</strong></p>
|
| 608 |
<p style="margin: 5px 0;">Tipo: {'🔴 Potencialmente maligna' if is_malignant else '🟢 Probablemente benigna'}</p>
|
| 609 |
</div>
|
| 610 |
+
|
| 611 |
<div style="background: #e3f2fd; padding: 15px; border-radius: 8px; margin: 15px 0;">
|
| 612 |
<h4 style="color: #1976d2;">🤖 Resultados Individuales por Modelo</h4>
|
| 613 |
<p style="font-size: 0.9em; color: #555;">
|
| 614 |
A continuación se detallan las predicciones de cada modelo. Es importante destacar que los <strong>modelos entrenados específicamente en lesiones de piel (Categoría: Especializados) suelen ser más fiables</strong> para este tipo de análisis que los modelos generales.
|
| 615 |
</p>
|
| 616 |
"""
|
| 617 |
+
|
| 618 |
# RESULTADOS INDIVIDUALES DETALLADOS - Separados por categoría
|
| 619 |
+
|
| 620 |
# Especializados
|
| 621 |
html_report += """
|
| 622 |
<h5 style="color: #007bff; border-bottom: 1px solid #007bff; padding-bottom: 5px; margin-top: 20px;">
|
|
|
|
| 629 |
specialized_models_found = True
|
| 630 |
model_risk = RISK_LEVELS[pred['predicted_idx']]
|
| 631 |
malignant_status = "🔴 Maligna" if pred['is_malignant'] else "🟢 Benigna"
|
| 632 |
+
|
| 633 |
html_report += f"""
|
| 634 |
<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);">
|
| 635 |
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
|
| 636 |
<h5 style="margin: 0; color: #333;">{pred['model']}</h5>
|
| 637 |
<span style="background: {model_risk['color']}; color: white; padding: 4px 8px; border-radius: 4px; font-size: 12px;">{model_risk['level']}</span>
|
| 638 |
</div>
|
| 639 |
+
|
| 640 |
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; font-size: 14px;">
|
| 641 |
<div><strong>Diagnóstico:</strong><br>{pred['class']}</div>
|
| 642 |
<div><strong>Confianza:</strong><br>{pred['confidence']:.1%}</div>
|
| 643 |
<div><strong>Clasificación:</strong><br>{malignant_status}</div>
|
| 644 |
</div>
|
| 645 |
+
|
| 646 |
<div style="margin-top: 10px;">
|
| 647 |
<strong>Top 3 Probabilidades:</strong><br>
|
| 648 |
<div style="font-size: 12px; color: #666;">
|
| 649 |
"""
|
| 650 |
+
|
| 651 |
top_indices = np.argsort(pred['probabilities'])[-3:][::-1]
|
| 652 |
for idx in top_indices:
|
| 653 |
prob = pred['probabilities'][idx]
|
| 654 |
if prob > 0.01:
|
| 655 |
html_report += f"• {CLASSES[idx].split('(')[1].rstrip(')')}: {prob:.1%}<br>"
|
| 656 |
+
|
| 657 |
html_report += f"""
|
| 658 |
</div>
|
| 659 |
<div style="margin-top: 8px; font-size: 12px; color: #888;">
|
|
|
|
| 680 |
general_models_found = True
|
| 681 |
model_risk = RISK_LEVELS[pred['predicted_idx']]
|
| 682 |
malignant_status = "🔴 Maligna" if pred['is_malignant'] else "🟢 Benigna"
|
| 683 |
+
|
| 684 |
html_report += f"""
|
| 685 |
<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);">
|
| 686 |
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
|
| 687 |
<h5 style="margin: 0; color: #333;">{pred['model']}</h5>
|
| 688 |
<span style="background: {model_risk['color']}; color: white; padding: 4px 8px; border-radius: 4px; font-size: 12px;">{model_risk['level']}</span>
|
| 689 |
</div>
|
| 690 |
+
|
| 691 |
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; font-size: 14px;">
|
| 692 |
<div><strong>Diagnóstico:</strong><br>{pred['class']}</div>
|
| 693 |
<div><strong>Confianza:</strong><br>{pred['confidence']:.1%}</div>
|
| 694 |
<div><strong>Clasificación:</strong><br>{malignant_status}</div>
|
| 695 |
</div>
|
| 696 |
+
|
| 697 |
<div style="margin-top: 10px;">
|
| 698 |
<strong>Top 3 Probabilidades:</strong><br>
|
| 699 |
<div style="font-size: 12px; color: #666;">
|
| 700 |
"""
|
| 701 |
+
|
| 702 |
top_indices = np.argsort(pred['probabilities'])[-3:][::-1]
|
| 703 |
for idx in top_indices:
|
| 704 |
prob = pred['probabilities'][idx]
|
| 705 |
if prob > 0.01:
|
| 706 |
html_report += f"• {CLASSES[idx].split('(')[1].rstrip(')')}: {prob:.1%}<br>"
|
| 707 |
+
|
| 708 |
html_report += f"""
|
| 709 |
</div>
|
| 710 |
<div style="margin-top: 8px; font-size: 12px; color: #888;">
|
|
|
|
| 715 |
"""
|
| 716 |
if not general_models_found:
|
| 717 |
html_report += "<p style='color: #888;'>No se cargaron modelos generales o fallaron al predecir.</p>"
|
| 718 |
+
|
| 719 |
html_report += f"""
|
| 720 |
</div>
|
| 721 |
+
|
| 722 |
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px; margin: 15px 0;">
|
| 723 |
<h4 style="color: #495057;">📊 Análisis Estadístico</h4>
|
| 724 |
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px;">
|
| 725 |
<div>
|
| 726 |
+
{probability_chart}
|
|
|
|
|
|
|
| 727 |
</div>
|
| 728 |
<div>
|
| 729 |
+
{heatmap}
|
|
|
|
|
|
|
| 730 |
</div>
|
| 731 |
</div>
|
| 732 |
</div>
|
| 733 |
+
|
| 734 |
+
<div style="background: #fff3cd; color: #856404; padding: 15px; border-radius: 8px; margin: 15px 0; border: 1px solid #ffeeba;">
|
| 735 |
+
<h4 style="margin-top: 0;">Disclaimer Importante:</h4>
|
| 736 |
+
<p style="font-size: 0.9em; margin-bottom: 5px;">
|
| 737 |
+
Esta herramienta es un <strong>prototipo de investigación</strong> y no debe ser utilizada como un diagnóstico médico definitivo. Los resultados son generados por modelos de inteligencia artificial y pueden contener errores.
|
| 738 |
+
</p>
|
| 739 |
+
<p style="font-size: 0.9em; margin-bottom: 5px;">
|
| 740 |
+
<strong>Siempre consulte a un profesional médico cualificado</strong> para cualquier inquietud sobre su salud. La automedicación o el autodiagnóstico basado en esta herramienta puede ser perjudicial.
|
| 741 |
+
</p>
|
| 742 |
+
<p style="font-size: 0.9em; margin-bottom: 0;">
|
| 743 |
+
La precisión de los modelos puede variar. Los modelos especializados en piel tienden a ser más fiables para estas tareas específicas.
|
| 744 |
+
</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 745 |
</div>
|
| 746 |
</div>
|
| 747 |
"""
|
|
|
|
| 748 |
return html_report
|
| 749 |
+
|
| 750 |
except Exception as e:
|
| 751 |
+
error_message = f"<h3>❌ Error Inesperado en el Análisis:</h3><p>Se produjo un error durante el procesamiento: {str(e)}</p><p>Por favor, intenta con otra imagen o recarga la aplicación.</p>"
|
| 752 |
+
print(error_message)
|
| 753 |
+
return error_message
|
| 754 |
+
|
| 755 |
+
|
| 756 |
+
# --- INTERFAZ GRADIO ---
|
| 757 |
+
# Componentes de entrada y salida
|
| 758 |
+
image_input = gr.Image(type="pil", label="Sube una imagen de la lesión cutánea")
|
| 759 |
+
output_html = gr.HTML(label="Informe de Análisis")
|
| 760 |
+
|
| 761 |
+
# Títulos y descripción para la interfaz
|
| 762 |
+
title = "Skin Lesion Analysis AI"
|
| 763 |
+
description = """
|
| 764 |
+
<h1 style="text-align: center; color: #2c3e50;">🩺 Analizador de Lesiones Cutáneas impulsado por IA 🩺</h1>
|
| 765 |
+
<p style="text-align: center; font-size: 1.1em; color: #555;">
|
| 766 |
+
Esta herramienta utiliza una batería de modelos de Visión por Computadora (tanto especializados en lesiones de piel como generales) para analizar imágenes y ofrecer un consenso sobre el tipo de lesión.
|
| 767 |
+
Proporciona un informe detallado con diagnósticos individuales de cada modelo y un consenso general, incluyendo un nivel de riesgo.
|
| 768 |
+
</p>
|
| 769 |
+
<p style="text-align: center; font-size: 1.1em; color: #555;">
|
| 770 |
+
<strong>Instrucciones:</strong> Sube una imagen clara de la lesión cutánea (óptimamente con buena iluminación y sin reflejos).
|
| 771 |
+
</p>
|
| 772 |
+
<p style="text-align: center; font-size: 0.9em; color: #888;">
|
| 773 |
+
⚠️ **Importante:** Esta herramienta es solo para **fines de investigación y educativos**. No reemplaza el consejo médico profesional. Siempre consulta a un dermatólogo para un diagnóstico y tratamiento precisos.
|
| 774 |
+
</p>
|
| 775 |
+
"""
|
| 776 |
+
article = """
|
| 777 |
+
<div style="text-align: center; padding: 20px; background-color: #f0f2f5; border-top: 1px solid #e0e2e5;">
|
| 778 |
+
<h3 style="color: #333;">¿Cómo funciona?</h3>
|
| 779 |
+
<p style="color: #666;">
|
| 780 |
+
El sistema carga múltiples modelos de aprendizaje profundo (Convolutional Neural Networks y Vision Transformers) entrenados en diversos datasets, incluyendo conjuntos de datos médicos de lesiones cutáneas (como HAM10000 e ISIC) y datasets generales de imágenes (como ImageNet).
|
| 781 |
+
Cada modelo procesa la imagen de forma independiente y genera una predicción de probabilidad para cada una de las 7 clases de lesiones de piel más comunes.
|
| 782 |
+
Posteriormente, se realiza un análisis de consenso para consolidar las predicciones, ponderando la confianza de cada modelo y dando preferencia a los modelos entrenados específicamente para el dominio de la piel.
|
| 783 |
+
Finalmente, se genera un informe visual con gráficos de barras y mapas de calor para facilitar la interpretación de los resultados.
|
| 784 |
+
</p>
|
| 785 |
+
<h4 style="color: #333;">Clases de Lesiones Analizadas:</h4>
|
| 786 |
+
<ul style="list-style-type: none; padding: 0; color: #666; display: inline-block; text-align: left;">
|
| 787 |
+
<li><strong>AKIEC:</strong> Queratosis actínica / Carcinoma de Bowen</li>
|
| 788 |
+
<li><strong>BCC:</strong> Carcinoma de células basales</li>
|
| 789 |
+
<li><strong>BKL:</strong> Lesión queratósica benigna (verruga seborreica, queratosis actínica, liquen plano)</li>
|
| 790 |
+
<li><strong>DF:</strong> Dermatofibroma</li>
|
| 791 |
+
<li><strong>MEL:</strong> Melanoma maligno</li>
|
| 792 |
+
<li><strong>NV:</strong> Nevus melanocítico (Lunar)</li>
|
| 793 |
+
<li><strong>VASC:</strong> Lesión vascular (angiomas, telangiectasias)</li>
|
| 794 |
+
</ul>
|
| 795 |
+
<p style="font-size: 0.8em; color: #999; margin-top: 20px;">
|
| 796 |
+
Desarrollado con ❤️ para investigación en IA y salud.
|
| 797 |
+
</p>
|
| 798 |
+
</div>
|
| 799 |
+
"""
|
| 800 |
+
|
| 801 |
+
# Lanzar la interfaz Gradio
|
| 802 |
+
gr.Interface(
|
| 803 |
+
fn=analizar_lesion,
|
| 804 |
+
inputs=image_input,
|
| 805 |
+
outputs=output_html,
|
| 806 |
+
title=title,
|
| 807 |
+
description=description,
|
| 808 |
+
article=article,
|
| 809 |
+
theme="soft",
|
| 810 |
+
allow_flagging="auto", # Permite que los usuarios marquen resultados para mejorar el modelo
|
| 811 |
+
flagging_dir="flagged_data" # Directorio para guardar los datos marcados
|
| 812 |
+
).launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|