Deployment - "Use via API"

I think I set up correctly my API on HuggingFace.

What I don’t understand is: am I supposed to be able to run it in the course notebook?

I copied the following:

from gradio_client import Client, handle_file

client = Client("aledesa/blip-image-captioning")
result = client.predict(
		input=handle_file('https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png'),
		api_name="/predict"
)
print(result)

And got the following:

ImportError: cannot import name 'handle_file' from 'gradio_client' (/usr/local/lib/python3.9/site-packages/gradio_client/__init__.py)

I see that the command is different from what provided in the course notebook which is:

from gradio_client import Client

client = Client("eddyS/blip-image-captioning-2")
result = client.predict(
        "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png",    # filepath  in 'input' Image component
        api_name="/predict"
)
print(result)

However, I tried to run this:

from gradio_client import Client #, handle_file

client = Client("aledesa/blip-image-captioning")
result = client.predict(
        # input=handle_file('https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png'),
    'https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png',
        api_name="/predict"
)
print(result)

And after the first check I got the following error:

Loaded as API: https://aledesa-blip-image-captioning.hf.space ✔
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
Cell In[13], line 3
      1 from gradio_client import Client #, handle_file
----> 3 client = Client("aledesa/blip-image-captioning")
      4 result = client.predict(
      5         # input=handle_file('https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png'),
      6     'https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png',
      7         api_name="/predict"
      8 )
      9 print(result)

File /usr/local/lib/python3.9/site-packages/gradio_client/client.py:149, in Client.__init__(self, src, hf_token, max_workers, serialize, output_dir, verbose, auth)
    147 self.reset_url = urllib.parse.urljoin(self.src, utils.RESET_URL)
    148 self.app_version = version.parse(self.config.get("version", "2.0"))
--> 149 self._info = self._get_api_info()
    150 self.session_hash = str(uuid.uuid4())
    152 endpoint_class = (
    153     Endpoint if self.protocol.startswith("sse") else EndpointV3Compatibility
    154 )

File /usr/local/lib/python3.9/site-packages/gradio_client/client.py:467, in Client._get_api_info(self)
    465 r = httpx.get(api_info_url, headers=self.headers, cookies=self.cookies)
    466 if r.is_success:
--> 467     info = r.json()
    468 else:
    469     raise ValueError(f"Could not fetch api info for {self.src}: {r.text}")

File /usr/local/lib/python3.9/site-packages/httpx/_models.py:762, in Response.json(self, **kwargs)
    761 def json(self, **kwargs: typing.Any) -> typing.Any:
--> 762     return jsonlib.loads(self.content, **kwargs)

File /usr/local/lib/python3.9/json/__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    341     s = s.decode(detect_encoding(s), 'surrogatepass')
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:
    348     cls = JSONDecoder

File /usr/local/lib/python3.9/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
    332 def decode(self, s, _w=WHITESPACE.match):
    333     """Return the Python representation of ``s`` (a ``str`` instance
    334     containing a JSON document).
    335 
    336     """
--> 337     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338     end = _w(s, end).end()
    339     if end != len(s):

File /usr/local/lib/python3.9/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
    353     obj, end = self.scan_once(s, idx)
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
    356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Also, at 5:54 the video refers to a temporary link that I can’t see in my space.

I thank you in advance.

The documentation:

says:

For providing files or URLs as inputs, you should pass in the filepath or URL to the file enclosed within gradio_client.handle_file(). This takes care of uploading the file to the Gradio server and ensures that the file is preprocessed correctly

So you can’t leave out the handle_file() call.

Furthermore

from gradio_client import Client, handle_file

should work as the handle_file() function is in file utils.py

Unless there is some problem with versioning, whereby we enter versioning [BAD PLACE FROM DANTE THAT I AM NOT ALLOWED TO NAME BY THIS SOFTWARE]. Other users sometimes note this error, for example here.

After some research with git, it turns out that handle_file() was added to gradio_client/utils.py on 2024-06-06.

The relevant version is tagged as 2024-06-06 gradio@4.35.0

The command is git for-each-ref --sort=creatordate --format '%(creatordate:short) %(refname:short)' refs/tags | less but that’s detail.

Conclusion:

You need a gradio version >= 4.35.0.

Your current version can be printed using

As code:

print(gradio.__version__) 

Or on the command line:

pip show gradio

Now I don’t know what version the notebook uses, but try the print to find out.

1 Like

Thanks for your reply.

That’s exacting the thing I guess:

print(gradio.__version__) 
4.16.0

Is this the version of notebook?

ChatGPT suggest the following code for old versions, I have no idea whether that works but it looks good:

import requests
from PIL import Image
from io import BytesIO
from gradio import Client
import gradio as gr

# Fetch the image from URL and open it
url = 'https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png'
response = requests.get(url)
img = Image.open(BytesIO(response.content))

# Initialize the client
client = Client("aledesa/blip-image-captioning")

# Perform prediction with the image input
result = client.predict(input=img, api_name="/predict")
print(result)

I cannot run it from the same environment of the course notebook as I get the error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[1], line 4
      2 from PIL import Image
      3 from io import BytesIO
----> 4 from gradio import Client
      5 import gradio as gr

ImportError: cannot import name 'Client' from 'gradio' (/usr/local/lib/python3.9/site-packages/gradio/__init__.py)

But that’s ok, I could try with a newer version of the packages on another environment.

Thanks.

1 Like