Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,57 @@
|
|
| 1 |
-
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
def analizar_lesion_combined(img):
|
| 4 |
try:
|
| 5 |
-
# Convertir imagen para Fastai
|
| 6 |
img_fastai = PILImage.create(img)
|
| 7 |
-
|
| 8 |
-
# ViT prediction
|
| 9 |
inputs = feature_extractor(img, return_tensors="pt")
|
| 10 |
with torch.no_grad():
|
| 11 |
outputs = model_vit(**inputs)
|
|
@@ -14,24 +60,19 @@ def analizar_lesion_combined(img):
|
|
| 14 |
pred_class_vit = CLASSES[pred_idx_vit]
|
| 15 |
confidence_vit = probs_vit[pred_idx_vit]
|
| 16 |
|
| 17 |
-
# Fast.ai models
|
| 18 |
pred_fast_malignant, _, probs_fast_mal = model_malignancy.predict(img_fastai)
|
| 19 |
-
prob_malignant = float(probs_fast_mal[1])
|
| 20 |
pred_fast_type, _, probs_fast_type = model_norm2000.predict(img_fastai)
|
| 21 |
|
| 22 |
-
# Modelo TensorFlow ISIC (usando TFSMLayer)
|
| 23 |
x_isic = preprocess_image_isic(img)
|
| 24 |
preds_isic_dict = model_isic(x_isic)
|
| 25 |
-
|
| 26 |
print("🔍 Claves de salida de model_isic:", preds_isic_dict.keys())
|
| 27 |
-
|
| 28 |
key = list(preds_isic_dict.keys())[0]
|
| 29 |
preds_isic = preds_isic_dict[key].numpy()[0]
|
| 30 |
pred_idx_isic = int(np.argmax(preds_isic))
|
| 31 |
pred_class_isic = CLASSES[pred_idx_isic]
|
| 32 |
confidence_isic = preds_isic[pred_idx_isic]
|
| 33 |
|
| 34 |
-
# Gráfico ViT
|
| 35 |
colors_bars = [RISK_LEVELS[i]['color'] for i in range(7)]
|
| 36 |
fig, ax = plt.subplots(figsize=(8, 3))
|
| 37 |
ax.bar(CLASSES, probs_vit*100, color=colors_bars)
|
|
@@ -48,9 +89,7 @@ def analizar_lesion_combined(img):
|
|
| 48 |
img_b64 = base64.b64encode(img_bytes).decode("utf-8")
|
| 49 |
html_chart = f'<img src="data:image/png;base64,{img_b64}" style="max-width:100%"/>'
|
| 50 |
|
| 51 |
-
|
| 52 |
-
informe = f"""
|
| 53 |
-
<div style="font-family:sans-serif; max-width:800px; margin:auto">
|
| 54 |
<h2>🧪 Diagnóstico por 4 modelos de IA</h2>
|
| 55 |
<table style="border-collapse: collapse; width:100%; font-size:16px">
|
| 56 |
<tr><th style="text-align:left">🔍 Modelo</th><th>Resultado</th><th>Confianza</th></tr>
|
|
@@ -58,10 +97,7 @@ def analizar_lesion_combined(img):
|
|
| 58 |
<tr><td>🧬 Fast.ai (clasificación)</td><td><b>{pred_fast_type}</b></td><td>N/A</td></tr>
|
| 59 |
<tr><td>⚠️ Fast.ai (malignidad)</td><td><b>{"Maligno" if prob_malignant > 0.5 else "Benigno"}</b></td><td>{prob_malignant:.1%}</td></tr>
|
| 60 |
<tr><td>🔬 ISIC TensorFlow</td><td><b>{pred_class_isic}</b></td><td>{confidence_isic:.1%}</td></tr>
|
| 61 |
-
</table>
|
| 62 |
-
<br>
|
| 63 |
-
<b>🩺 Recomendación automática:</b><br>
|
| 64 |
-
"""
|
| 65 |
|
| 66 |
cancer_risk_score = sum(probs_vit[i] * RISK_LEVELS[i]['weight'] for i in range(7))
|
| 67 |
if prob_malignant > 0.7 or cancer_risk_score > 0.6:
|
|
@@ -74,7 +110,6 @@ def analizar_lesion_combined(img):
|
|
| 74 |
informe += "✅ <b>BAJO RIESGO</b> – Seguimiento de rutina (3-6 meses)"
|
| 75 |
|
| 76 |
informe += "</div>"
|
| 77 |
-
|
| 78 |
return informe, html_chart
|
| 79 |
|
| 80 |
except Exception as e:
|
|
@@ -83,3 +118,17 @@ def analizar_lesion_combined(img):
|
|
| 83 |
traceback.print_exc()
|
| 84 |
return f"<b>Error interno:</b> {str(e)}", ""
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
import numpy as np
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import base64
|
| 8 |
+
import io
|
| 9 |
+
from fastai.vision.all import *
|
| 10 |
+
import tensorflow as tf
|
| 11 |
+
from tensorflow import keras
|
| 12 |
+
import zipfile
|
| 13 |
+
import os
|
| 14 |
+
import traceback
|
| 15 |
+
|
| 16 |
+
# Cargar modelo TensorFlow ISIC (descomprimir solo una vez)
|
| 17 |
+
if not os.path.exists("saved_model"):
|
| 18 |
+
with zipfile.ZipFile("saved_model.zip", "r") as zip_ref:
|
| 19 |
+
zip_ref.extractall(".")
|
| 20 |
+
|
| 21 |
+
# Cargar modelo ISIC con TensorFlow
|
| 22 |
+
model_isic = tf.keras.models.load_model("saved_model")
|
| 23 |
+
|
| 24 |
+
# Cargar modelos fastai
|
| 25 |
+
model_malignancy = load_learner("modelo_malignancy.pkl")
|
| 26 |
+
model_norm2000 = load_learner("modelo_norm2000.pkl")
|
| 27 |
+
|
| 28 |
+
# Cargar modelo ViT
|
| 29 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 30 |
+
feature_extractor = AutoImageProcessor.from_pretrained("nateraw/vit-skin-cancer")
|
| 31 |
+
model_vit = AutoModelForImageClassification.from_pretrained("nateraw/vit-skin-cancer")
|
| 32 |
+
|
| 33 |
+
# Clases y colores
|
| 34 |
+
CLASSES = ['akiec', 'bcc', 'bkl', 'df', 'mel', 'nv', 'vasc']
|
| 35 |
+
RISK_LEVELS = {
|
| 36 |
+
0: {"label": "akiec", "color": "#FF6F61", "weight": 0.9},
|
| 37 |
+
1: {"label": "bcc", "color": "#FF8C42", "weight": 0.7},
|
| 38 |
+
2: {"label": "bkl", "color": "#FFD166", "weight": 0.3},
|
| 39 |
+
3: {"label": "df", "color": "#06D6A0", "weight": 0.1},
|
| 40 |
+
4: {"label": "mel", "color": "#EF476F", "weight": 1.0},
|
| 41 |
+
5: {"label": "nv", "color": "#118AB2", "weight": 0.2},
|
| 42 |
+
6: {"label": "vasc", "color": "#073B4C", "weight": 0.4},
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# Preprocesado para TensorFlow ISIC
|
| 46 |
+
def preprocess_image_isic(pil_image):
|
| 47 |
+
image = pil_image.resize((224, 224))
|
| 48 |
+
array = np.array(image) / 255.0
|
| 49 |
+
return np.expand_dims(array, axis=0)
|
| 50 |
+
|
| 51 |
+
# Función de análisis (como ya la tienes)
|
| 52 |
def analizar_lesion_combined(img):
|
| 53 |
try:
|
|
|
|
| 54 |
img_fastai = PILImage.create(img)
|
|
|
|
|
|
|
| 55 |
inputs = feature_extractor(img, return_tensors="pt")
|
| 56 |
with torch.no_grad():
|
| 57 |
outputs = model_vit(**inputs)
|
|
|
|
| 60 |
pred_class_vit = CLASSES[pred_idx_vit]
|
| 61 |
confidence_vit = probs_vit[pred_idx_vit]
|
| 62 |
|
|
|
|
| 63 |
pred_fast_malignant, _, probs_fast_mal = model_malignancy.predict(img_fastai)
|
| 64 |
+
prob_malignant = float(probs_fast_mal[1])
|
| 65 |
pred_fast_type, _, probs_fast_type = model_norm2000.predict(img_fastai)
|
| 66 |
|
|
|
|
| 67 |
x_isic = preprocess_image_isic(img)
|
| 68 |
preds_isic_dict = model_isic(x_isic)
|
|
|
|
| 69 |
print("🔍 Claves de salida de model_isic:", preds_isic_dict.keys())
|
|
|
|
| 70 |
key = list(preds_isic_dict.keys())[0]
|
| 71 |
preds_isic = preds_isic_dict[key].numpy()[0]
|
| 72 |
pred_idx_isic = int(np.argmax(preds_isic))
|
| 73 |
pred_class_isic = CLASSES[pred_idx_isic]
|
| 74 |
confidence_isic = preds_isic[pred_idx_isic]
|
| 75 |
|
|
|
|
| 76 |
colors_bars = [RISK_LEVELS[i]['color'] for i in range(7)]
|
| 77 |
fig, ax = plt.subplots(figsize=(8, 3))
|
| 78 |
ax.bar(CLASSES, probs_vit*100, color=colors_bars)
|
|
|
|
| 89 |
img_b64 = base64.b64encode(img_bytes).decode("utf-8")
|
| 90 |
html_chart = f'<img src="data:image/png;base64,{img_b64}" style="max-width:100%"/>'
|
| 91 |
|
| 92 |
+
informe = f"""<div style="font-family:sans-serif; max-width:800px; margin:auto">
|
|
|
|
|
|
|
| 93 |
<h2>🧪 Diagnóstico por 4 modelos de IA</h2>
|
| 94 |
<table style="border-collapse: collapse; width:100%; font-size:16px">
|
| 95 |
<tr><th style="text-align:left">🔍 Modelo</th><th>Resultado</th><th>Confianza</th></tr>
|
|
|
|
| 97 |
<tr><td>🧬 Fast.ai (clasificación)</td><td><b>{pred_fast_type}</b></td><td>N/A</td></tr>
|
| 98 |
<tr><td>⚠️ Fast.ai (malignidad)</td><td><b>{"Maligno" if prob_malignant > 0.5 else "Benigno"}</b></td><td>{prob_malignant:.1%}</td></tr>
|
| 99 |
<tr><td>🔬 ISIC TensorFlow</td><td><b>{pred_class_isic}</b></td><td>{confidence_isic:.1%}</td></tr>
|
| 100 |
+
</table><br><b>🩺 Recomendación automática:</b><br>"""
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
cancer_risk_score = sum(probs_vit[i] * RISK_LEVELS[i]['weight'] for i in range(7))
|
| 103 |
if prob_malignant > 0.7 or cancer_risk_score > 0.6:
|
|
|
|
| 110 |
informe += "✅ <b>BAJO RIESGO</b> – Seguimiento de rutina (3-6 meses)"
|
| 111 |
|
| 112 |
informe += "</div>"
|
|
|
|
| 113 |
return informe, html_chart
|
| 114 |
|
| 115 |
except Exception as e:
|
|
|
|
| 118 |
traceback.print_exc()
|
| 119 |
return f"<b>Error interno:</b> {str(e)}", ""
|
| 120 |
|
| 121 |
+
# INTERFAZ
|
| 122 |
+
demo = gr.Interface(
|
| 123 |
+
fn=analizar_lesion_combined,
|
| 124 |
+
inputs=gr.Image(type="pil", label="Sube una imagen de la lesión"),
|
| 125 |
+
outputs=[gr.HTML(label="Informe combinado"), gr.HTML(label="Gráfico ViT")],
|
| 126 |
+
title="Detector de Lesiones Cutáneas (ViT + Fast.ai + ISIC TensorFlow)",
|
| 127 |
+
description="Comparación entre ViT transformer (HAM10000), dos modelos Fast.ai y el modelo ISIC TensorFlow.",
|
| 128 |
+
flagging_mode="never"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# LANZAMIENTO
|
| 132 |
+
if __name__ == "__main__":
|
| 133 |
+
demo.launch()
|
| 134 |
+
|