tmabraham / blog_old

Blog
Apache License 2.0
6 stars 7 forks source link

Gradio + HuggingFace Spaces: A Tutorial | Tanishq Abraham’s blog #24

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

Gradio + HuggingFace Spaces: A Tutorial | Tanishq Abraham’s blog

Learn about easy ML app development

https://tmabraham.github.io/blog/gradio_hf_spaces_tutorial

truthdead commented 2 years ago

Thanks for this awesome article, Tanishq. If it helps anyone, when trying to install git-lfs i ran into this error with apt-get install git-lfs :

E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied) E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

Using sudo apt-get install git-lfs worked for me, apparently specifying that I have root access. (it asked for my password which is the one I used when setting up Ubuntu on my windows machine (wsl))

dennis-sell commented 2 years ago

Great tutorial, Tanishq. Thanks for explaining the git/HF steps.

In case it helps anybody reading this later, the examples should ideally be a list of lists. I had a few examples in a typical list format, and that worked locally but broke when uploaded to HF - with an unhelpful Gradio error. So anyway, examples should look more like this instead: examples = [['siamese.jpg'], ['other_cat.jpg']]

Not sure why this error is happening now and not for you. Maybe because you use only one example, or maybe there has been a gradio library change.

aldrinjenson commented 2 years ago

Hey Tanishq, thanks a lot for this article. It was really helpful to get my model deployed quickly!

jasondotparse commented 1 year ago

This was super helpful! I actually wish I found this before I read chapter 2 of fast.ai since this instruction set is way more up to date. Thanks for the post!

edwinhung commented 1 year ago

Thanks for a easy-to-follow blog post!

galuzan8 commented 1 year ago

When I try to import fast ai i am getting an error Traceback (most recent call last): File "app.py", line 1, in from fastai.vision.all import * ModuleNotFoundError: No module named 'fastai'

nakranivaibhav commented 1 year ago

Thank you for clear explanations! You made the process a lot easier. i would recommend a edit.

"git lfs install git lfs track "*.pkl" git add .gitattributes git commit -m "update .gitattributes so git lfs will track .pkl files"

after this please mention that you need to do this only once for a file type. When the file type changes you have to run this process again. If i am right!

alexworden commented 1 year ago

It would be useful if you showed folks how to install Gradio in their notebooks. Otherwise, you get an error "ModuleNotFoundError: No module named 'gradio'"

rec5885 commented 1 year ago

Many errors occurred just while attempting to upload a .pkl file, and it still cannot be uploaded. ChatGPT cannot help.

Missing or invalid credentials. Error: connect ENOENT /run/user/1000/vscode-git-e3d04bfe6f.sock at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) { errno: -2, code: 'ENOENT', syscall: 'connect', address: '/run/user/1000/vscode-git-e3d04bfe6f.sock' } remote: Invalid username or password.

thinkOfaNumber commented 1 year ago

I found your post from the fast.ai course - great stuff, thanks!

It looks like the API links have changed. https://hf.space/embed/tmabraham/fastai_pet_classifier/api now returns a "not found" message.

When I look at the current api in hf spaces, there is no definition of the json schema anymore - it wants me to install gradio/client. That's fine, but what if I want to use something other than python or javascript to consume it?

Do you know where the raw api definition is?

Chuhao95 commented 1 year ago

I'm a Fast.ai student. Regularly come back to this post when need to create a new HF space. Thanks for sharing it!

PilleOtp commented 1 year ago

If you get ModuleNotFoundError: No module named 'fastai' You need to add a requirements.txt

IR1DO commented 11 months ago

The code related to gradio is outdated. gr.inputs was deprecated in gradio 4.0. Now just use gr.Interface(fn=predict,inputs="image",outputs="label").launch()

RoyShubham420 commented 10 months ago

Thanks for the article!

!pip install gradio

Add this before import gradio as gr

hosseinkamyab commented 1 week ago

Summary of the Issue

  1. Gradio’s gr.inputs and gr.outputs Deprecation: In recent versions, gr.inputs.Image and gr.outputs.Label are no longer valid. Instead, you should use gr.Image and gr.Label directly.

  2. Removed shape Argument: The shape parameter in gr.Image, which was previously used to set a fixed input image size (e.g., 512x512), is no longer supported. Attempting to use it results in a TypeError.

Solution

  1. Direct Component Usage: Replace gr.inputs.Image and gr.outputs.Label with gr.Image and gr.Label.

  2. Manual Resizing in Function: To ensure images are resized to 512x512, handle resizing within the predict function. Set type="pil" in gr.Image to pass a PIL image to the function, then resize it in the predict function using img.resize((512, 512)).

Final Code Snippet

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

learn = load_learner('export.pkl')
labels = learn.dls.vocab

def predict(img):
    img = img.resize((512, 512))  # Resize to 512x512
    img = PILImage.create(img)
    pred, pred_idx, probs = learn.predict(img)
    return {labels[i]: float(probs[i]) for i in range(len(labels))}

gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", image_mode="RGB"),  # Updated component, compatible with PIL
    outputs=gr.Label(num_top_classes=2)
).launch(share=True)

This solution keeps images displayed and processed at 512x512 without errors.