Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -547,10 +547,125 @@ def create_probability_chart(predictions, consensus_class):
|
|
| 547 |
</div>
|
| 548 |
"""
|
| 549 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
return html_report
|
| 551 |
|
| 552 |
except Exception as e:
|
| 553 |
return f"<h3>❌ Error en el análisis</h3><p>Error técnico: {str(e)}</p><p>Por favor, intente con otra imagen.</p>"
|
|
|
|
|
|
|
|
|
|
| 554 |
|
| 555 |
# Configuración de Gradio
|
| 556 |
def create_interface():
|
|
|
|
| 547 |
</div>
|
| 548 |
"""
|
| 549 |
|
| 550 |
+
# Generar visualizaciones
|
| 551 |
+
probability_chart = create_probability_chart(predictions, consensus_class)
|
| 552 |
+
heatmap = create_heatmap(predictions)
|
| 553 |
+
|
| 554 |
+
# Generar HTML del reporte COMPLETO
|
| 555 |
+
html_report = f"""
|
| 556 |
+
<div style="font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto;">
|
| 557 |
+
<h2 style="color: #2c3e50; text-align: center;">🏥 Análisis Completo de Lesión Cutánea</h2>
|
| 558 |
+
|
| 559 |
+
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 10px; margin: 20px 0;">
|
| 560 |
+
<h3 style="margin: 0; text-align: center;">📋 Resultado de Consenso</h3>
|
| 561 |
+
<p style="font-size: 18px; text-align: center; margin: 10px 0;"><strong>{consensus_class}</strong></p>
|
| 562 |
+
<p style="text-align: center; margin: 5px 0;">Confianza Promedio: <strong>{avg_confidence:.1%}</strong></p>
|
| 563 |
+
<p style="text-align: center; margin: 5px 0;">Consenso: <strong>{class_votes[consensus_class]}/{len(predictions)} modelos</strong></p>
|
| 564 |
+
</div>
|
| 565 |
+
|
| 566 |
+
<div style="background: {risk_info['color']}; color: white; padding: 15px; border-radius: 8px; margin: 15px 0;">
|
| 567 |
+
<h4 style="margin: 0;">⚠️ Nivel de Riesgo: {risk_info['level']}</h4>
|
| 568 |
+
<p style="margin: 5px 0;"><strong>{risk_info['urgency']}</strong></p>
|
| 569 |
+
<p style="margin: 5px 0;">Tipo: {'🔴 Potencialmente maligna' if is_malignant else '🟢 Probablemente benigna'}</p>
|
| 570 |
+
</div>
|
| 571 |
+
|
| 572 |
+
<div style="background: #e3f2fd; padding: 15px; border-radius: 8px; margin: 15px 0;">
|
| 573 |
+
<h4 style="color: #1976d2;">🤖 Resultados Individuales por Modelo</h4>
|
| 574 |
+
"""
|
| 575 |
+
|
| 576 |
+
# RESULTADOS INDIVIDUALES DETALLADOS
|
| 577 |
+
for i, pred in enumerate(predictions, 1):
|
| 578 |
+
if pred['success']:
|
| 579 |
+
model_risk = RISK_LEVELS[pred['predicted_idx']]
|
| 580 |
+
malignant_status = "🔴 Maligna" if pred['is_malignant'] else "🟢 Benigna"
|
| 581 |
+
|
| 582 |
+
html_report += f"""
|
| 583 |
+
<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);">
|
| 584 |
+
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
|
| 585 |
+
<h5 style="margin: 0; color: #333;">#{i}. {pred['model']}</h5>
|
| 586 |
+
<span style="background: {model_risk['color']}; color: white; padding: 4px 8px; border-radius: 4px; font-size: 12px;">{model_risk['level']}</span>
|
| 587 |
+
</div>
|
| 588 |
+
|
| 589 |
+
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; font-size: 14px;">
|
| 590 |
+
<div><strong>Diagnóstico:</strong><br>{pred['class']}</div>
|
| 591 |
+
<div><strong>Confianza:</strong><br>{pred['confidence']:.1%}</div>
|
| 592 |
+
<div><strong>Clasificación:</strong><br>{malignant_status}</div>
|
| 593 |
+
</div>
|
| 594 |
+
|
| 595 |
+
<div style="margin-top: 10px;">
|
| 596 |
+
<strong>Top 3 Probabilidades:</strong><br>
|
| 597 |
+
<div style="font-size: 12px; color: #666;">
|
| 598 |
+
"""
|
| 599 |
+
|
| 600 |
+
# Top 3 probabilidades para este modelo
|
| 601 |
+
top_indices = np.argsort(pred['probabilities'])[-3:][::-1]
|
| 602 |
+
for idx in top_indices:
|
| 603 |
+
prob = pred['probabilities'][idx]
|
| 604 |
+
if prob > 0.01: # Solo mostrar si > 1%
|
| 605 |
+
html_report += f"• {CLASSES[idx].split('(')[1].rstrip(')')}: {prob:.1%}<br>"
|
| 606 |
+
|
| 607 |
+
html_report += f"""
|
| 608 |
+
</div>
|
| 609 |
+
<div style="margin-top: 8px; font-size: 12px; color: #888;">
|
| 610 |
+
<strong>Recomendación:</strong> {model_risk['urgency']}
|
| 611 |
+
</div>
|
| 612 |
+
</div>
|
| 613 |
+
</div>
|
| 614 |
+
"""
|
| 615 |
+
else:
|
| 616 |
+
html_report += f"""
|
| 617 |
+
<div style="margin: 10px 0; padding: 10px; background: #ffebee; border-radius: 5px; border-left: 4px solid #f44336;">
|
| 618 |
+
<strong>❌ {pred['model']}</strong><br>
|
| 619 |
+
<span style="color: #d32f2f;">Error: {pred.get('error', 'Desconocido')}</span>
|
| 620 |
+
</div>
|
| 621 |
+
"""
|
| 622 |
+
|
| 623 |
+
html_report += f"""
|
| 624 |
+
</div>
|
| 625 |
+
|
| 626 |
+
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px; margin: 15px 0;">
|
| 627 |
+
<h4 style="color: #495057;">📊 Análisis Estadístico</h4>
|
| 628 |
+
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px;">
|
| 629 |
+
<div>
|
| 630 |
+
<strong>Modelos Activos:</strong> {len([p for p in predictions if p['success']])}/{len(predictions)}<br>
|
| 631 |
+
<strong>Acuerdo Total:</strong> {class_votes[consensus_class]}/{len([p for p in predictions if p['success']])}<br>
|
| 632 |
+
<strong>Confianza Máxima:</strong> {max([p['confidence'] for p in predictions if p['success']]):.1%}
|
| 633 |
+
</div>
|
| 634 |
+
<div>
|
| 635 |
+
<strong>Diagnósticos Malignos:</strong> {len([p for p in predictions if p.get('success') and p.get('is_malignant')])}<br>
|
| 636 |
+
<strong>Diagnósticos Benignos:</strong> {len([p for p in predictions if p.get('success') and not p.get('is_malignant')])}<br>
|
| 637 |
+
<strong>Consenso Maligno:</strong> {'Sí' if is_malignant else 'No'}
|
| 638 |
+
</div>
|
| 639 |
+
</div>
|
| 640 |
+
</div>
|
| 641 |
+
|
| 642 |
+
<div style="background: #ffffff; padding: 15px; border-radius: 8px; margin: 15px 0; border: 1px solid #ddd;">
|
| 643 |
+
<h4 style="color: #333;">📈 Gráficos de Análisis</h4>
|
| 644 |
+
{probability_chart}
|
| 645 |
+
</div>
|
| 646 |
+
|
| 647 |
+
<div style="background: #ffffff; padding: 15px; border-radius: 8px; margin: 15px 0; border: 1px solid #ddd;">
|
| 648 |
+
<h4 style="color: #333;">🔥 Mapa de Calor de Probabilidades</h4>
|
| 649 |
+
{heatmap}
|
| 650 |
+
</div>
|
| 651 |
+
|
| 652 |
+
<div style="background: #fff3e0; padding: 15px; border-radius: 8px; margin: 15px 0; border: 1px solid #ff9800;">
|
| 653 |
+
<h4 style="color: #f57c00;">⚠️ Advertencia Médica</h4>
|
| 654 |
+
<p style="margin: 5px 0;">Este análisis es solo una herramienta de apoyo diagnóstico basada en IA.</p>
|
| 655 |
+
<p style="margin: 5px 0;"><strong>Siempre consulte con un dermatólogo profesional para un diagnóstico definitivo.</strong></p>
|
| 656 |
+
<p style="margin: 5px 0;">No utilice esta información como único criterio para decisiones médicas.</p>
|
| 657 |
+
<p style="margin: 5px 0;"><em>Los resultados individuales de cada modelo se muestran para transparencia y análisis comparativo.</em></p>
|
| 658 |
+
</div>
|
| 659 |
+
</div>
|
| 660 |
+
"""
|
| 661 |
+
|
| 662 |
return html_report
|
| 663 |
|
| 664 |
except Exception as e:
|
| 665 |
return f"<h3>❌ Error en el análisis</h3><p>Error técnico: {str(e)}</p><p>Por favor, intente con otra imagen.</p>"
|
| 666 |
+
|
| 667 |
+
except Exception as e:
|
| 668 |
+
return f"<h3>❌ Error en el análisis</h3><p>Error técnico: {str(e)}</p><p>Por favor, intente con otra imagen.</p>"
|
| 669 |
|
| 670 |
# Configuración de Gradio
|
| 671 |
def create_interface():
|