Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,31 @@
|
|
| 1 |
-
from fastai.vision.all import *
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 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')
|
| 22 |
-
|
| 23 |
-
# Define tus clases manualmente si no están en dls.vocab
|
| 24 |
-
labels = ['No Blindness', 'Blindness']
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
# ---------------------------------------------------------
|
| 36 |
-
gr.Interface(
|
| 37 |
fn=predict,
|
| 38 |
-
inputs=gr.Image(
|
| 39 |
-
outputs=gr.Label(num_top_classes=
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from transformers import AutoModelForImageClassification, AutoFeatureExtractor
|
| 6 |
|
| 7 |
+
# Cargar el modelo desde Hugging Face Hub
|
| 8 |
+
model = AutoModelForImageClassification.from_pretrained("AdrianRevi/Practica1Blindness")
|
| 9 |
+
extractor = AutoFeatureExtractor.from_pretrained("AdrianRevi/Practica1Blindness")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Preprocesamiento
|
| 12 |
+
def predict(img: Image.Image):
|
| 13 |
+
inputs = extractor(images=img, return_tensors="pt")
|
| 14 |
+
with torch.no_grad():
|
| 15 |
+
outputs = model(**inputs)
|
| 16 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
|
| 17 |
+
labels = model.config.id2label
|
| 18 |
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 19 |
|
| 20 |
+
# Interfaz Gradio
|
| 21 |
+
demo = gr.Interface(
|
|
|
|
|
|
|
| 22 |
fn=predict,
|
| 23 |
+
inputs=gr.Image(type="pil"),
|
| 24 |
+
outputs=gr.Label(num_top_classes=3),
|
| 25 |
+
examples=["examples/20068.jpg", "examples/20084.jpg"],
|
| 26 |
+
title="Blindness Detection",
|
| 27 |
+
description="Sube una imagen del ojo para detectar el grado de ceguera.",
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
demo.launch()
|