Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,42 @@
|
|
1 |
from fastai.vision.all import *
|
2 |
-
from huggingface_hub import from_pretrained_fastai
|
3 |
import gradio as gr
|
4 |
|
5 |
-
#
|
6 |
-
# 1.
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
pred, pred_idx, probs = learner.predict(img)
|
18 |
-
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
19 |
-
except Exception as e:
|
20 |
-
return {"Error": str(e)}
|
21 |
-
|
22 |
-
# -------------------------------
|
23 |
-
# 3. Interfaz Gradio
|
24 |
-
# -------------------------------
|
25 |
-
title = "👁️ Clasificador de Ceguera con FastAI"
|
26 |
-
description = """
|
27 |
-
Esta aplicación utiliza un modelo de **Aprendizaje Profundo** entrenado con `fastai` para predecir el **grado de ceguera** en imágenes de retina.
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
36 |
|
37 |
-
#
|
38 |
-
# 4.
|
39 |
-
#
|
40 |
-
|
41 |
fn=predict,
|
42 |
-
inputs=gr.Image(
|
43 |
-
outputs=gr.Label(num_top_classes=
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
allow_flagging="never",
|
48 |
-
live=False,
|
49 |
-
theme="default",
|
50 |
-
)
|
51 |
-
|
52 |
-
if __name__ == "__main__":
|
53 |
-
demo.launch(show_error=True)
|
|
|
1 |
from fastai.vision.all import *
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
+
# ---------------------------------------------------------
|
5 |
+
# 1. Crea unos DataLoaders falsos para cargar el modelo (requerido)
|
6 |
+
# ---------------------------------------------------------
|
7 |
+
# Esto es solo para inicializar el modelo correctamente
|
8 |
+
dls = ImageDataLoaders.from_name_func(
|
9 |
+
path='.',
|
10 |
+
fnames=get_image_files('.'),
|
11 |
+
label_func=lambda x: 'placeholder',
|
12 |
+
valid_pct=0.2,
|
13 |
+
item_tfms=Resize(128),
|
14 |
+
bs=1
|
15 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
# ---------------------------------------------------------
|
18 |
+
# 2. Crea el learner y carga el modelo desde .pth
|
19 |
+
# ---------------------------------------------------------
|
20 |
+
learn = cnn_learner(dls, resnet18, metrics=accuracy)
|
21 |
+
learn.load('resnet18_blindness') # asegúrate de que el archivo .pth esté en la raíz o models/
|
22 |
|
23 |
+
# Define tus clases manualmente si no están en dls.vocab
|
24 |
+
labels = ['No Blindness', 'Blindness']
|
25 |
|
26 |
+
# ---------------------------------------------------------
|
27 |
+
# 3. Define la función de predicción
|
28 |
+
# ---------------------------------------------------------
|
29 |
+
def predict(img):
|
30 |
+
pred, idx, probs = learn.predict(img)
|
31 |
+
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
32 |
|
33 |
+
# ---------------------------------------------------------
|
34 |
+
# 4. Lanza la app con Gradio
|
35 |
+
# ---------------------------------------------------------
|
36 |
+
gr.Interface(
|
37 |
fn=predict,
|
38 |
+
inputs=gr.Image(shape=(128, 128)),
|
39 |
+
outputs=gr.Label(num_top_classes=2),
|
40 |
+
title="Clasificador de Ceguera",
|
41 |
+
description="Sube una imagen de retina y predice si hay ceguera o no."
|
42 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|