File size: 1,935 Bytes
37511d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# import gradio as gr
# from fastai.vision.all import *
# import timm

# # Load the exported model
# learn = load_learner('./efficientnet_b3_model.pkl', cpu=True)  # Using cpu=True for compatibility

# learn.export('./efficientnet_b3_model.pkl')  # export_model(learn, 'efficientnet_b3_model.pkl')


# # Define the prediction function
# def classify_image(image):
#     pred, idx, probs = learn.predict(image)
#     # Return the top 3 predictions with their probabilities
#     return {learn.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}

# # Set up the Gradio interface
# interface = gr.Interface(
#     fn=classify_image,  # Function to make predictions
#     inputs=gr.Image(type="pil"),  # Input as an image in PIL format
#     outputs=gr.Label(num_top_classes=3),  # Output shows top 3 predicted classes
#     title="EfficientNet B3 Image Classifier",
#     description="Upload an image to classify using the trained EfficientNet B3 model."
# )

# # Launch the Gradio app
# if __name__ == "__main__":
#     interface.launch(share=True)  # `share=True` makes the app publicly accessible

from pathlib import Path
from fastai.vision.all import *
import gradio as gr

# Correctly format the path for Windows
model_path = Path(r'efficientnet_b3_model.pkl')

# Load the model
learn = load_learner(model_path, cpu=True)

# Define the prediction function
def classify_image(image):
    pred, idx, probs = learn.predict(image)
    return {learn.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}

# Set up the Gradio interface
interface = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=3),
    title="EfficientNet B3 Image Classifier",
    description="Upload an image to classify using the trained EfficientNet B3 model."
)

# Launch the app
if __name__ == "__main__":
    interface.launch(share=True)