Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,53 @@
|
|
|
|
1 |
from huggingface_hub import from_pretrained_fastai
|
2 |
import gradio as gr
|
3 |
-
from fastai.vision.all import *
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
# repo_id = "YOUR_USERNAME/YOUR_LEARNER_NAME"
|
8 |
-
repo_id = "AdrianRevi/Practica1Blindness"
|
9 |
|
|
|
|
|
|
|
|
|
10 |
learner = from_pretrained_fastai(repo_id)
|
11 |
labels = learner.dls.vocab
|
12 |
|
13 |
-
#
|
|
|
|
|
14 |
def predict(img):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastai.vision.all import *
|
2 |
from huggingface_hub import from_pretrained_fastai
|
3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# -------------------------------
|
6 |
+
# 1. Cargar el modelo desde Hugging Face
|
7 |
+
# -------------------------------
|
8 |
+
repo_id = "AdrianRevi/Practica1Blindness" # Cambiar si es necesario
|
9 |
learner = from_pretrained_fastai(repo_id)
|
10 |
labels = learner.dls.vocab
|
11 |
|
12 |
+
# -------------------------------
|
13 |
+
# 2. Función de predicción
|
14 |
+
# -------------------------------
|
15 |
def predict(img):
|
16 |
+
try:
|
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 |
+
📌 El modelo fue entrenado en Google Colab y desplegado en Hugging Face Spaces mediante `from_pretrained_fastai`.
|
30 |
+
|
31 |
+
🔍 Puedes subir tu propia imagen o usar uno de los ejemplos de la galería.
|
32 |
+
|
33 |
+
📷 La imagen se redimensiona automáticamente a 128x128 píxeles (tamaño de entrada del modelo).
|
34 |
+
"""
|
35 |
+
examples = ['20068.jpg', '20084.jpg'] # Archivos locales en el Space
|
36 |
+
|
37 |
+
# -------------------------------
|
38 |
+
# 4. Crear y lanzar interfaz
|
39 |
+
# -------------------------------
|
40 |
+
demo = gr.Interface(
|
41 |
+
fn=predict,
|
42 |
+
inputs=gr.Image(type="pil", shape=(128, 128), label="Sube una imagen de retina"),
|
43 |
+
outputs=gr.Label(num_top_classes=3, label="Predicción (Top 3)"),
|
44 |
+
examples=examples,
|
45 |
+
title=title,
|
46 |
+
description=description,
|
47 |
+
allow_flagging="never",
|
48 |
+
live=False,
|
49 |
+
theme="default",
|
50 |
+
)
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
demo.launch(show_error=True)
|