brxerq commited on
Commit
19a3b21
·
verified ·
1 Parent(s): 28bfaae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -2,25 +2,37 @@
2
 
3
  __all__ = ['bird', 'learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
4
 
5
- # Load categories from label.txt
6
- with open('labels.txt', 'r') as file:
7
  categories = [line.strip() for line in file.readlines()]
8
 
9
- # Load the model
10
  from fastai.vision.all import *
11
  import gradio as gr
12
 
13
- learn = load_learner('model.pkl')
 
 
 
 
14
 
15
- # Define the classification function
 
 
 
 
 
 
 
 
16
  def classify_image(img):
17
  preds, idx, probs = learn.predict(img)
18
  return dict(zip(categories, map(float, probs)))
19
 
20
- # Gradio components
21
- image = gr.components.Image()
22
- label = gr.components.Label(num_top_classes=3)
23
 
24
- # Create and launch the interface
25
  intf = gr.Interface(fn=classify_image, inputs=image, outputs=label)
26
- intf.launch(inline=False, share=True)
 
2
 
3
  __all__ = ['bird', 'learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
4
 
5
+ # Load categories from labels.txt
6
+ with open("labels.txt", "r") as file:
7
  categories = [line.strip() for line in file.readlines()]
8
 
9
+ # Import FastAI and Gradio
10
  from fastai.vision.all import *
11
  import gradio as gr
12
 
13
+ # ✅ Define Model Architecture (Must match original model)
14
+ def create_model():
15
+ model = resnet50(pretrained=False) # Change if your model used a different architecture
16
+ model[1] = torch.nn.Linear(model[1].in_features, len(categories)) # Adjust output layer
17
+ return model
18
 
19
+ # ✅ Load Model Safely
20
+ model = create_model()
21
+ model.load_state_dict(torch.load("model_weights.pth", map_location="cpu"))
22
+ model.eval() # Set to evaluation mode
23
+
24
+ # Wrap in a FastAI Learner
25
+ learn = Learner(dls=None, model=model)
26
+
27
+ # ✅ Define the classification function
28
  def classify_image(img):
29
  preds, idx, probs = learn.predict(img)
30
  return dict(zip(categories, map(float, probs)))
31
 
32
+ # ✅ Gradio components
33
+ image = gr.Image()
34
+ label = gr.Label(num_top_classes=3)
35
 
36
+ # ✅ Create and launch the interface
37
  intf = gr.Interface(fn=classify_image, inputs=image, outputs=label)
38
+ intf.launch(inline=False, share=True)