SagarPuniyani's picture
Update app.py
e2a1281 verified
raw
history blame
1.9 kB
# 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) + 'cancer stage',
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)