Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastai.vision.all import *
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
# Montar Drive si quieres usar imágenes de Drive en la app (opcional)
|
| 6 |
+
from google.colab import drive
|
| 7 |
+
# drive.mount('/content/drive') # normalmente NO se monta en Hugging Face, solo local
|
| 8 |
+
|
| 9 |
+
# Rutas relativas al espacio
|
| 10 |
+
path = Path(".") # aquí se suben las carpetas de clases
|
| 11 |
+
|
| 12 |
+
dls = ImageDataLoaders.from_folder(
|
| 13 |
+
path,
|
| 14 |
+
valid_pct=0.2,
|
| 15 |
+
seed=42,
|
| 16 |
+
item_tfms=Resize(224)
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
learn = vision_learner(dls, resnet34)
|
| 20 |
+
learn.load("model_lab")
|
| 21 |
+
|
| 22 |
+
labels = learn.dls.vocab
|
| 23 |
+
|
| 24 |
+
def predict(img):
|
| 25 |
+
img = PILImage.create(img)
|
| 26 |
+
_, _, probs = learn.predict(img)
|
| 27 |
+
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 28 |
+
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn=predict,
|
| 31 |
+
inputs=gr.Image(type="filepath"),
|
| 32 |
+
outputs=gr.Label(num_top_classes=3),
|
| 33 |
+
title="Lab Utensils Classifier"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
demo.launch()
|