Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,10 +6,10 @@ import base64
|
|
| 6 |
import io
|
| 7 |
from fastai.vision.all import *
|
| 8 |
import tensorflow as tf
|
|
|
|
| 9 |
import zipfile
|
| 10 |
import os
|
| 11 |
import traceback
|
| 12 |
-
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 13 |
|
| 14 |
# Descomprimir el modelo si no se ha descomprimido a煤n
|
| 15 |
if not os.path.exists("saved_model"):
|
|
@@ -17,10 +17,12 @@ if not os.path.exists("saved_model"):
|
|
| 17 |
zip_ref.extractall("saved_model")
|
| 18 |
|
| 19 |
# Cargar modelo ISIC con TensorFlow desde el directorio correcto
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
-
model_isic =
|
| 22 |
except Exception as e:
|
| 23 |
-
print("\U0001F534 Error al cargar el modelo ISIC:", e)
|
| 24 |
raise
|
| 25 |
|
| 26 |
# Cargar modelos fastai
|
|
@@ -28,6 +30,7 @@ model_malignancy = load_learner("ada_learn_malben.pkl")
|
|
| 28 |
model_norm2000 = load_learner("ada_learn_skin_norm2000.pkl")
|
| 29 |
|
| 30 |
# Cargar modelo ViT
|
|
|
|
| 31 |
feature_extractor = AutoImageProcessor.from_pretrained("nateraw/vit-skin-cancer")
|
| 32 |
model_vit = AutoModelForImageClassification.from_pretrained("nateraw/vit-skin-cancer")
|
| 33 |
|
|
@@ -43,15 +46,16 @@ RISK_LEVELS = {
|
|
| 43 |
6: {"label": "vasc", "color": "#073B4C", "weight": 0.4},
|
| 44 |
}
|
| 45 |
|
|
|
|
| 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 |
def analizar_lesion_combined(img):
|
| 52 |
try:
|
| 53 |
img_fastai = PILImage.create(img)
|
| 54 |
-
|
| 55 |
inputs = feature_extractor(img, return_tensors="pt")
|
| 56 |
with torch.no_grad():
|
| 57 |
outputs = model_vit(**inputs)
|
|
@@ -65,17 +69,16 @@ def analizar_lesion_combined(img):
|
|
| 65 |
pred_fast_type, _, probs_fast_type = model_norm2000.predict(img_fastai)
|
| 66 |
|
| 67 |
x_isic = preprocess_image_isic(img)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
preds_isic = preds_isic_tensor[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
|
| 79 |
ax.set_title("Probabilidad ViT por tipo de lesi贸n")
|
| 80 |
ax.set_ylabel("Probabilidad (%)")
|
| 81 |
ax.set_xticks(np.arange(len(CLASSES)))
|
|
@@ -89,15 +92,17 @@ def analizar_lesion_combined(img):
|
|
| 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"""
|
|
|
|
| 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>
|
| 96 |
<tr><td>馃 ViT (transformer)</td><td><b>{pred_class_vit}</b></td><td>{confidence_vit:.1%}</td></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>{
|
| 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:
|
|
@@ -118,6 +123,7 @@ def analizar_lesion_combined(img):
|
|
| 118 |
traceback.print_exc()
|
| 119 |
return f"<b>Error interno:</b> {str(e)}", ""
|
| 120 |
|
|
|
|
| 121 |
demo = gr.Interface(
|
| 122 |
fn=analizar_lesion_combined,
|
| 123 |
inputs=gr.Image(type="pil", label="Sube una imagen de la lesi贸n"),
|
|
@@ -127,6 +133,7 @@ demo = gr.Interface(
|
|
| 127 |
flagging_mode="never"
|
| 128 |
)
|
| 129 |
|
|
|
|
| 130 |
if __name__ == "__main__":
|
| 131 |
demo.launch()
|
| 132 |
|
|
|
|
| 6 |
import io
|
| 7 |
from fastai.vision.all import *
|
| 8 |
import tensorflow as tf
|
| 9 |
+
from tensorflow import keras
|
| 10 |
import zipfile
|
| 11 |
import os
|
| 12 |
import traceback
|
|
|
|
| 13 |
|
| 14 |
# Descomprimir el modelo si no se ha descomprimido a煤n
|
| 15 |
if not os.path.exists("saved_model"):
|
|
|
|
| 17 |
zip_ref.extractall("saved_model")
|
| 18 |
|
| 19 |
# Cargar modelo ISIC con TensorFlow desde el directorio correcto
|
| 20 |
+
from keras.layers import TFSMLayer
|
| 21 |
+
|
| 22 |
try:
|
| 23 |
+
model_isic = TFSMLayer("saved_model/saved_model", call_endpoint="serving_default")
|
| 24 |
except Exception as e:
|
| 25 |
+
print("\U0001F534 Error al cargar el modelo ISIC con TFSMLayer:", e)
|
| 26 |
raise
|
| 27 |
|
| 28 |
# Cargar modelos fastai
|
|
|
|
| 30 |
model_norm2000 = load_learner("ada_learn_skin_norm2000.pkl")
|
| 31 |
|
| 32 |
# Cargar modelo ViT
|
| 33 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 34 |
feature_extractor = AutoImageProcessor.from_pretrained("nateraw/vit-skin-cancer")
|
| 35 |
model_vit = AutoModelForImageClassification.from_pretrained("nateraw/vit-skin-cancer")
|
| 36 |
|
|
|
|
| 46 |
6: {"label": "vasc", "color": "#073B4C", "weight": 0.4},
|
| 47 |
}
|
| 48 |
|
| 49 |
+
# Preprocesado para TensorFlow ISIC
|
| 50 |
def preprocess_image_isic(pil_image):
|
| 51 |
image = pil_image.resize((224, 224))
|
| 52 |
array = np.array(image) / 255.0
|
| 53 |
return np.expand_dims(array, axis=0)
|
| 54 |
|
| 55 |
+
# Funci贸n de an谩lisis
|
| 56 |
def analizar_lesion_combined(img):
|
| 57 |
try:
|
| 58 |
img_fastai = PILImage.create(img)
|
|
|
|
| 59 |
inputs = feature_extractor(img, return_tensors="pt")
|
| 60 |
with torch.no_grad():
|
| 61 |
outputs = model_vit(**inputs)
|
|
|
|
| 69 |
pred_fast_type, _, probs_fast_type = model_norm2000.predict(img_fastai)
|
| 70 |
|
| 71 |
x_isic = preprocess_image_isic(img)
|
| 72 |
+
preds_isic_dict = model_isic(x_isic)
|
| 73 |
+
key = list(preds_isic_dict.keys())[0]
|
| 74 |
+
preds_isic = preds_isic_dict[key].numpy()[0]
|
|
|
|
| 75 |
pred_idx_isic = int(np.argmax(preds_isic))
|
| 76 |
pred_class_isic = CLASSES[pred_idx_isic]
|
| 77 |
confidence_isic = preds_isic[pred_idx_isic]
|
| 78 |
|
| 79 |
colors_bars = [RISK_LEVELS[i]['color'] for i in range(7)]
|
| 80 |
fig, ax = plt.subplots(figsize=(8, 3))
|
| 81 |
+
ax.bar(CLASSES, probs_vit*100, color=colors_bars)
|
| 82 |
ax.set_title("Probabilidad ViT por tipo de lesi贸n")
|
| 83 |
ax.set_ylabel("Probabilidad (%)")
|
| 84 |
ax.set_xticks(np.arange(len(CLASSES)))
|
|
|
|
| 92 |
img_b64 = base64.b64encode(img_bytes).decode("utf-8")
|
| 93 |
html_chart = f'<img src="data:image/png;base64,{img_b64}" style="max-width:100%"/>'
|
| 94 |
|
| 95 |
+
informe = f"""
|
| 96 |
+
<div style="font-family:sans-serif; max-width:800px; margin:auto">
|
| 97 |
<h2>馃И Diagn贸stico por 4 modelos de IA</h2>
|
| 98 |
<table style="border-collapse: collapse; width:100%; font-size:16px">
|
| 99 |
<tr><th style="text-align:left">馃攳 Modelo</th><th>Resultado</th><th>Confianza</th></tr>
|
| 100 |
<tr><td>馃 ViT (transformer)</td><td><b>{pred_class_vit}</b></td><td>{confidence_vit:.1%}</td></tr>
|
| 101 |
<tr><td>馃К Fast.ai (clasificaci贸n)</td><td><b>{pred_fast_type}</b></td><td>N/A</td></tr>
|
| 102 |
+
<tr><td>鈿狅笍 Fast.ai (malignidad)</td><td><b>{'Maligno' if prob_malignant > 0.5 else 'Benigno'}</b></td><td>{prob_malignant:.1%}</td></tr>
|
| 103 |
<tr><td>馃敩 ISIC TensorFlow</td><td><b>{pred_class_isic}</b></td><td>{confidence_isic:.1%}</td></tr>
|
| 104 |
+
</table><br><b>馃 Recomendaci贸n autom谩tica:</b><br>
|
| 105 |
+
"""
|
| 106 |
|
| 107 |
cancer_risk_score = sum(probs_vit[i] * RISK_LEVELS[i]['weight'] for i in range(7))
|
| 108 |
if prob_malignant > 0.7 or cancer_risk_score > 0.6:
|
|
|
|
| 123 |
traceback.print_exc()
|
| 124 |
return f"<b>Error interno:</b> {str(e)}", ""
|
| 125 |
|
| 126 |
+
# INTERFAZ
|
| 127 |
demo = gr.Interface(
|
| 128 |
fn=analizar_lesion_combined,
|
| 129 |
inputs=gr.Image(type="pil", label="Sube una imagen de la lesi贸n"),
|
|
|
|
| 133 |
flagging_mode="never"
|
| 134 |
)
|
| 135 |
|
| 136 |
+
# LANZAMIENTO
|
| 137 |
if __name__ == "__main__":
|
| 138 |
demo.launch()
|
| 139 |
|