Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastai.vision.all import load_learner,
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
categories = learn.dls.vocab
|
| 8 |
|
|
@@ -11,7 +23,7 @@ def recognize_image(img):
|
|
| 11 |
|
| 12 |
return dict(zip(categories, map(float, probs)))
|
| 13 |
|
| 14 |
-
image = gr.Image(
|
| 15 |
label = gr.Label()
|
| 16 |
|
| 17 |
examples = [
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastai.vision.all import load_learner, Learner
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Try to load the model using the safer Learner.load method first
|
| 6 |
+
# If that fails, fall back to load_learner with explicit file handling
|
| 7 |
+
model_path = 'pkg_recognizer_v1'
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
# First try: Use the safer Learner.load method (expects .pth file)
|
| 11 |
+
learn = Learner.load(model_path)
|
| 12 |
+
except (FileNotFoundError, RuntimeError):
|
| 13 |
+
try:
|
| 14 |
+
# Second try: Use load_learner for pickle files (.pkl)
|
| 15 |
+
learn = load_learner(model_path + '.pkl')
|
| 16 |
+
except Exception as e:
|
| 17 |
+
raise RuntimeError(f"Failed to load model. Ensure 'pkg_recognizer_v1.pth' or 'pkg_recognizer_v1.pkl' exists. Error: {e}")
|
| 18 |
|
| 19 |
categories = learn.dls.vocab
|
| 20 |
|
|
|
|
| 23 |
|
| 24 |
return dict(zip(categories, map(float, probs)))
|
| 25 |
|
| 26 |
+
image = gr.Image()
|
| 27 |
label = gr.Label()
|
| 28 |
|
| 29 |
examples = [
|