Issue with Lesson 2 deployment

I have deployed my app on hugging faces using this tutorial and I get the following error screen

This is my app.py code.

import gradio as gr
from fastai.vision.all import *
import skimage

learn = load_learner('model.pkl')

labels = learn.dls.vocab
def predict(img):
    img = PILImage.create(img)
    pred,pred_idx,probs = learn.predict(img)
    return {labels[i]: float(probs[i]) for i in range(len(labels))}

title = "Pet Breed Classifier"
description = "A pet breed classifier trained on the Oxford Pets dataset with fastai. Created as a demo for Gradio and HuggingFace Spaces."
article="<p style='text-align: center'><a href='https://tmabraham.github.io/blog/gradio_hf_spaces_tutorial' target='_blank'>Blog post</a></p>"
examples = ['cat.jpg']
interpretation='default'
enable_queue=True

gr.Interface(fn=predict,inputs=gr.inputs.Image(shape=(512, 512)),outputs=gr.outputs.Label(num_top_classes=3),title=title,description=description,article=article,examples=examples,interpretation=interpretation,enable_queue=enable_queue).launch()

Can someone please help me fix this?

It seems that you have an issue with your Learner so you need to check your model.pkl, can you post the code used to create that? It seems that you have an issue with the is_cat function.

Thanks for your response @Kamui. Here is my code:

!pip install -Uqq fastai
from fastai.vision.all import *
def is_cat(x): return x[0].isupper()
path = untar_data(URLs.PETS)/'images'
dls = ImageDataLoaders.from_name_func('.',
    get_image_files(path), valid_pct=0.2, seed=42,
    label_func=is_cat,
    item_tfms=Resize(192))
dls.show_batch()
learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(3)
learn.export('model.pkl')

I’ve tried creating a new space with a new model but I get this error message each time and I’m unable to push my repo onto huggingfaces. Any idea why I’m getting this error message? I’ve spent several hours but still haven’t been able to resolve this

Uploading LFS objects: 100% (1/1), 47 MB | 2.2 MB/s, done.
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 2.69 MiB | 960.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
remote: e[31m-------------------------------------------------------------------------e[0m        
remote: e[31mYour push was rejected because it contains binary files.e[0m        
remote: e[31mPlease use https://git-lfs.github.com/ to store binary files.e[0m        
remote: e[31mSee also: https://hf.co/docs/hub/repositories-getting-started#terminale[0m        
remote: e[31me[0m        
remote: e[31mOffending files:e[0m        
remote: e[31m  - siamese.jpg (ref: refs/heads/main)e[0m        
remote: e[31m-------------------------------------------------------------------------e[0m        
To https://huggingface.co/spaces/hamzautd7/abc
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://huggingface.co/spaces/hamzautd7/abc'

you need to install git-lfs git lfs install or just upload your file through the website (not with the command line).
It’s better to create a new space again and do every step from the beginning.
Also you must include a file named requirements.txt containing the imports you need for example:

fastai
torch
gradio
1 Like

I already initialised git lfs with github desktop and included a requirements file but I get this error. I have tried creating a new space and doing everything from the start but I get this error


I have tried using Github Desktop and cli but I get the same result.

manually uploading the model.pkl file via the wesbsite gives me the original error

I am sorry I am out of ideas :confused: .
Maybe @ilovescience could help you since you followed his tutorial.

1 Like

Thanks anyways for the quick responses :+1: :+1: much appreciated

2 Likes

Update: changing the app.py to this worked but I had to upload the files via the website. git LFS would not work no matter what I did even though it was working yesterday. Can’t understand what changed. Followed every tutorial lol. Even deleted my huggingface account

__all__ = ['learn', 'categories', 'image', 'label', 'examples', 'intf', 'is_cat', 'classify_image']

# %% app.ipynb 2
from fastai.vision.all import *
import gradio as gr

def is_cat(x): return x[0].isupper()

# %% app.ipynb 4
learn = load_learner('model.pkl')

# %% app.ipynb 6
categories = ('Dog', 'Cat')

def classify_image(img):
    pred, idx, probs = learn.predict(img)
    return dict(zip(categories, map(float, probs)))

# %% app.ipynb 8
from gradio.components import Image, Label

# %% app.ipynb 9
image =  Image(shape=(192,192))
label = Label()
examples = ['dog.jpg', 'cat.jpg']
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
intf.launch(inline=False)

There seems to be a bug with git lfs, that’s why I told you to repeat every step from scratch and upload only via the website. Glad you solved your problem!

For labelling you’re using is_cat function. When you export your model, you don’t export the functions used by the model, so you need to add an is_cat function to app.py

3 Likes

Haven’t fully analyzed this but off the bat seeing the error am thinking this is the issue

1 Like

Is anyone able to get the Jupyter notebooks to work locally on Mac? I have fastbook already installed on my machine and I cannot seem to import it. After watching the lesson I got worried about the different python environments for Mac/Unix systems. If the setup script got installed correctly will I not have to worry about the notebooks using the system default python?

Hi @onigirijack

If fastbook is indeed installed, it’s probably installed in a different environment from which you are running jupyter lab.

The easiest way to manage environments on a local machine is to use something like conda or mamba. You need to make sure that you run jupyter from the same environment as where you installed fastai, fastbook or any other dependency you want to use in your notebooks.

Hi guys , I am able to deploy my app using Hugging face spaces but when I click the “Use via API” option a blank screen appears. It would be great if someone can let me know if they have encountered the same issue , Thanks.