ashfaqfardin commited on
Commit
23a72e0
·
verified ·
1 Parent(s): 43a8ae7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -5
app.py CHANGED
@@ -1,8 +1,20 @@
1
  import gradio as gr
2
- from fastai.vision.all import load_learner, PILImage
3
-
4
-
5
- learn = load_learner('pkg_recognizer_v1.pkl')
 
 
 
 
 
 
 
 
 
 
 
 
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(type="pil")
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 = [