Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,36 +2,36 @@ from fastai.vision.all import *
|
|
| 2 |
import gradio as gr
|
| 3 |
from pathlib import Path
|
| 4 |
import zipfile
|
|
|
|
| 5 |
|
| 6 |
-
# Extraer
|
| 7 |
with zipfile.ZipFile("dataset.zip", 'r') as zip_ref:
|
| 8 |
-
zip_ref.extractall(".") # crea
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
# Crear DataLoaders
|
| 14 |
dls = ImageDataLoaders.from_folder(
|
| 15 |
-
|
| 16 |
valid_pct=0.2,
|
| 17 |
seed=42,
|
| 18 |
item_tfms=Resize(224)
|
| 19 |
)
|
| 20 |
|
| 21 |
-
# Cargar
|
| 22 |
learn = vision_learner(dls, resnet34)
|
| 23 |
-
learn.load("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,3 +41,4 @@ demo = gr.Interface(
|
|
| 41 |
|
| 42 |
demo.launch()
|
| 43 |
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from pathlib import Path
|
| 4 |
import zipfile
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Extraer zip
|
| 8 |
with zipfile.ZipFile("dataset.zip", 'r') as zip_ref:
|
| 9 |
+
zip_ref.extractall(".") # crea dataset/
|
| 10 |
|
| 11 |
+
# Asegurarse de que existe la carpeta correcta
|
| 12 |
+
dataset_path = Path("dataset")
|
| 13 |
+
if not dataset_path.exists():
|
| 14 |
+
raise FileNotFoundError("La carpeta 'dataset' no existe después de descomprimir el zip. Revisa la estructura del zip.")
|
| 15 |
|
| 16 |
+
# Crear DataLoaders
|
| 17 |
dls = ImageDataLoaders.from_folder(
|
| 18 |
+
dataset_path,
|
| 19 |
valid_pct=0.2,
|
| 20 |
seed=42,
|
| 21 |
item_tfms=Resize(224)
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Cargar modelo
|
| 25 |
learn = vision_learner(dls, resnet34)
|
| 26 |
+
learn.load("model_lab")
|
| 27 |
|
|
|
|
| 28 |
labels = learn.dls.vocab
|
| 29 |
|
|
|
|
| 30 |
def predict(img):
|
| 31 |
img = PILImage.create(img)
|
| 32 |
_, _, probs = learn.predict(img)
|
| 33 |
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 34 |
|
|
|
|
| 35 |
demo = gr.Interface(
|
| 36 |
fn=predict,
|
| 37 |
inputs=gr.Image(type="filepath"),
|
|
|
|
| 41 |
|
| 42 |
demo.launch()
|
| 43 |
|
| 44 |
+
|