Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,27 +12,23 @@ 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
|
16 |
with zipfile.ZipFile("saved_model.zip", "r") as zip_ref:
|
17 |
-
zip_ref.extractall("
|
18 |
|
19 |
# Cargar modelo ISIC con TensorFlow desde el directorio correcto
|
20 |
from keras.layers import TFSMLayer
|
21 |
|
22 |
try:
|
23 |
-
model_isic = TFSMLayer("
|
24 |
except Exception as e:
|
25 |
print("\U0001F534 Error al cargar el modelo ISIC con TFSMLayer:", e)
|
26 |
raise
|
27 |
|
28 |
-
#
|
29 |
model_malignancy = load_learner("ada_learn_malben.pkl")
|
30 |
model_norm2000 = load_learner("ada_learn_skin_norm2000.pkl")
|
31 |
|
32 |
-
# Cargar modelos fastai
|
33 |
-
model_malignancy = load_learner("modelo_malignancy.pkl")
|
34 |
-
model_norm2000 = load_learner("modelo_norm2000.pkl")
|
35 |
-
|
36 |
# Cargar modelo ViT
|
37 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
38 |
feature_extractor = AutoImageProcessor.from_pretrained("nateraw/vit-skin-cancer")
|
@@ -56,7 +52,7 @@ def preprocess_image_isic(pil_image):
|
|
56 |
array = np.array(image) / 255.0
|
57 |
return np.expand_dims(array, axis=0)
|
58 |
|
59 |
-
# Funci贸n de an谩lisis
|
60 |
def analizar_lesion_combined(img):
|
61 |
try:
|
62 |
img_fastai = PILImage.create(img)
|
@@ -74,6 +70,7 @@ def analizar_lesion_combined(img):
|
|
74 |
|
75 |
x_isic = preprocess_image_isic(img)
|
76 |
preds_isic_dict = model_isic(x_isic)
|
|
|
77 |
key = list(preds_isic_dict.keys())[0]
|
78 |
preds_isic = preds_isic_dict[key].numpy()[0]
|
79 |
pred_idx_isic = int(np.argmax(preds_isic))
|
@@ -104,7 +101,7 @@ def analizar_lesion_combined(img):
|
|
104 |
<tr><td>馃К Fast.ai (clasificaci贸n)</td><td><b>{pred_fast_type}</b></td><td>N/A</td></tr>
|
105 |
<tr><td>鈿狅笍 Fast.ai (malignidad)</td><td><b>{"Maligno" if prob_malignant > 0.5 else "Benigno"}</b></td><td>{prob_malignant:.1%}</td></tr>
|
106 |
<tr><td>馃敩 ISIC TensorFlow</td><td><b>{pred_class_isic}</b></td><td>{confidence_isic:.1%}</td></tr>
|
107 |
-
</table><br><b
|
108 |
|
109 |
cancer_risk_score = sum(probs_vit[i] * RISK_LEVELS[i]['weight'] for i in range(7))
|
110 |
if prob_malignant > 0.7 or cancer_risk_score > 0.6:
|
@@ -120,7 +117,7 @@ def analizar_lesion_combined(img):
|
|
120 |
return informe, html_chart
|
121 |
|
122 |
except Exception as e:
|
123 |
-
print("
|
124 |
print(str(e))
|
125 |
traceback.print_exc()
|
126 |
return f"<b>Error interno:</b> {str(e)}", ""
|
@@ -138,3 +135,4 @@ demo = gr.Interface(
|
|
138 |
# LANZAMIENTO
|
139 |
if __name__ == "__main__":
|
140 |
demo.launch()
|
|
|
|
12 |
import traceback
|
13 |
|
14 |
# Descomprimir el modelo si no se ha descomprimido a煤n
|
15 |
+
if not os.path.exists("saved_model"):
|
16 |
with zipfile.ZipFile("saved_model.zip", "r") as zip_ref:
|
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
|
29 |
model_malignancy = load_learner("ada_learn_malben.pkl")
|
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")
|
|
|
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)
|
|
|
70 |
|
71 |
x_isic = preprocess_image_isic(img)
|
72 |
preds_isic_dict = model_isic(x_isic)
|
73 |
+
print("\U0001F50D Claves de salida de model_isic:", preds_isic_dict.keys())
|
74 |
key = list(preds_isic_dict.keys())[0]
|
75 |
preds_isic = preds_isic_dict[key].numpy()[0]
|
76 |
pred_idx_isic = int(np.argmax(preds_isic))
|
|
|
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 |
cancer_risk_score = sum(probs_vit[i] * RISK_LEVELS[i]['weight'] for i in range(7))
|
107 |
if prob_malignant > 0.7 or cancer_risk_score > 0.6:
|
|
|
117 |
return informe, html_chart
|
118 |
|
119 |
except Exception as e:
|
120 |
+
print("\U0001F534 ERROR en analizar_lesion_combined:")
|
121 |
print(str(e))
|
122 |
traceback.print_exc()
|
123 |
return f"<b>Error interno:</b> {str(e)}", ""
|
|
|
135 |
# LANZAMIENTO
|
136 |
if __name__ == "__main__":
|
137 |
demo.launch()
|
138 |
+
|