Help needed _lesson2_trouble with_prediction code locally

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=True in launch().

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()

The RuntimeError: Could not infer dtype of numpy.uint8 occurs because the fastai library is not correctly interpreting the image data type from the Gradio input. To fix this, you must explicitly convert the PIL Image object into a NumPy array before passing it to PILImage.create(). By adding img_array = np.array(image) at the beginning of your predict function, you ensure the data is in a format that fastai can reliably process, which resolves the runtime error.

This problem has something to do with version conflicts between numpy and fastai, try upgrade these libraries to their latest versions in your repository’s requirements.txt.