Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,16 @@
|
|
| 1 |
from fastai.vision.all import *
|
| 2 |
import gradio as gr
|
| 3 |
from pathlib import Path
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
| 9 |
dls = ImageDataLoaders.from_folder(
|
| 10 |
path,
|
| 11 |
valid_pct=0.2,
|
|
@@ -13,17 +18,20 @@ dls = ImageDataLoaders.from_folder(
|
|
| 13 |
item_tfms=Resize(224)
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# Cargar
|
| 17 |
learn = vision_learner(dls, resnet34)
|
| 18 |
-
learn.load("model_lab") # si
|
| 19 |
|
| 20 |
-
|
|
|
|
| 21 |
|
|
|
|
| 22 |
def predict(img):
|
| 23 |
img = PILImage.create(img)
|
| 24 |
_, _, probs = learn.predict(img)
|
| 25 |
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 26 |
|
|
|
|
| 27 |
demo = gr.Interface(
|
| 28 |
fn=predict,
|
| 29 |
inputs=gr.Image(type="filepath"),
|
|
@@ -33,3 +41,4 @@ demo = gr.Interface(
|
|
| 33 |
|
| 34 |
demo.launch()
|
| 35 |
|
|
|
|
|
|
| 1 |
from fastai.vision.all import *
|
| 2 |
import gradio as gr
|
| 3 |
from pathlib import Path
|
| 4 |
+
import zipfile
|
| 5 |
|
| 6 |
+
# Extraer dataset.zip al inicio
|
| 7 |
+
with zipfile.ZipFile("dataset.zip", 'r') as zip_ref:
|
| 8 |
+
zip_ref.extractall(".") # crea la carpeta dataset
|
| 9 |
|
| 10 |
+
# Ruta relativa al dataset extraído
|
| 11 |
+
path = Path("dataset")
|
| 12 |
+
|
| 13 |
+
# Crear DataLoaders desde las carpetas de clases
|
| 14 |
dls = ImageDataLoaders.from_folder(
|
| 15 |
path,
|
| 16 |
valid_pct=0.2,
|
|
|
|
| 18 |
item_tfms=Resize(224)
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# Cargar el modelo .pth
|
| 22 |
learn = vision_learner(dls, resnet34)
|
| 23 |
+
learn.load("model_lab") # si está en models/, usar "models/model_lab"
|
| 24 |
|
| 25 |
+
# Tomar automáticamente las clases desde los DataLoaders
|
| 26 |
+
labels = learn.dls.vocab
|
| 27 |
|
| 28 |
+
# Función de predicción
|
| 29 |
def predict(img):
|
| 30 |
img = PILImage.create(img)
|
| 31 |
_, _, probs = learn.predict(img)
|
| 32 |
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 33 |
|
| 34 |
+
# Interfaz Gradio
|
| 35 |
demo = gr.Interface(
|
| 36 |
fn=predict,
|
| 37 |
inputs=gr.Image(type="filepath"),
|
|
|
|
| 41 |
|
| 42 |
demo.launch()
|
| 43 |
|
| 44 |
+
|