import gradio as gr
from fastai.vision.all import load_learner, PILImage
# Load your model once on startup
learner = load_learner('resnet50-streetfood.pkl') # put your model file in the same folder
def predict(image):
# image is a PIL.Image from Gradio input
pred_class, pred_idx, probs = learner.predict(PILImage.create(image))
confidence = probs[pred_idx].item()
return f"{pred_class} ({confidence:.2%})"
# Gradio interface
iface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs="text",
title="Street Food Classifier",
description="Upload a food image and get a prediction."
)
if __name__ == "__main__":
iface.launch()
when i upload a picture for prediction it gives this error and it keeps happening in here but not in google colab with another code
error
- Running on local URL: http:
- To create a public link, set
share=Trueinlaunch().
0.00% [0/1 00:00<?]
Traceback (most recent call last):
pred_class, pred_idx, probs = learner.predict(PILImage.create(image))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Could not infer dtype of numpy.uint8
google colab code
!pip install fastai
from google.colab import files
from fastai.vision.all import *
# Upload model file
print("Upload your resnet50-streetfood.pkl file:")
uploaded = files.upload()
# Upload image file
print("Upload your image file:")
img_files = files.upload()
model_file = list(uploaded.keys())[0] # Your .pkl file
image_file = list(img_files.keys())[0] # Your image file
learner = load_learner(model_file)
pred_class, pred_idx, probs = learner.predict(image_file)
print(f"🍔 Predicted food: {pred_class}")
print(f"📊 Confidence: {probs[pred_idx]:.2%}")
print("\nđź“‹ All predictions:")
for food, prob in zip(learner.dls.vocab, probs):
print(f"{food}: {prob:.2%}")
import matplotlib.pyplot as plt
from PIL import Image
img = Image.open(image_file)
plt.figure(figsize=(8, 6))
plt.imshow(img)
plt.title(f"Prediction: {pred_class} ({probs[pred_idx]:.2%} confidence)")
plt.axis('off')
plt.show()