SagarPuniyani commited on
Commit
37511d2
·
verified ·
1 Parent(s): 84957f9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import gradio as gr
2
+ # from fastai.vision.all import *
3
+ # import timm
4
+
5
+ # # Load the exported model
6
+ # learn = load_learner('./efficientnet_b3_model.pkl', cpu=True) # Using cpu=True for compatibility
7
+
8
+ # learn.export('./efficientnet_b3_model.pkl') # export_model(learn, 'efficientnet_b3_model.pkl')
9
+
10
+
11
+ # # Define the prediction function
12
+ # def classify_image(image):
13
+ # pred, idx, probs = learn.predict(image)
14
+ # # Return the top 3 predictions with their probabilities
15
+ # return {learn.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}
16
+
17
+ # # Set up the Gradio interface
18
+ # interface = gr.Interface(
19
+ # fn=classify_image, # Function to make predictions
20
+ # inputs=gr.Image(type="pil"), # Input as an image in PIL format
21
+ # outputs=gr.Label(num_top_classes=3), # Output shows top 3 predicted classes
22
+ # title="EfficientNet B3 Image Classifier",
23
+ # description="Upload an image to classify using the trained EfficientNet B3 model."
24
+ # )
25
+
26
+ # # Launch the Gradio app
27
+ # if __name__ == "__main__":
28
+ # interface.launch(share=True) # `share=True` makes the app publicly accessible
29
+
30
+ from pathlib import Path
31
+ from fastai.vision.all import *
32
+ import gradio as gr
33
+
34
+ # Correctly format the path for Windows
35
+ model_path = Path(r'efficientnet_b3_model.pkl')
36
+
37
+ # Load the model
38
+ learn = load_learner(model_path, cpu=True)
39
+
40
+ # Define the prediction function
41
+ def classify_image(image):
42
+ pred, idx, probs = learn.predict(image)
43
+ return {learn.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}
44
+
45
+ # Set up the Gradio interface
46
+ interface = gr.Interface(
47
+ fn=classify_image,
48
+ inputs=gr.Image(type="pil"),
49
+ outputs=gr.Label(num_top_classes=3),
50
+ title="EfficientNet B3 Image Classifier",
51
+ description="Upload an image to classify using the trained EfficientNet B3 model."
52
+ )
53
+
54
+ # Launch the app
55
+ if __name__ == "__main__":
56
+ interface.launch(share=True)