API endpoint doesn't work

I tried to call the API of a space from a different space. This is the recommended use:

from gradio_client import Client

client = Client(“https://my-private-space.hf.space/”)
result = client.predict(
“Howdy!”, # str in ‘parameter_2’ Textbox component
“null”, # str (filepath to JSON file) in ‘parameter_1’ Chatbot component

So I called as follows:

_, _ = client.predict(‘ok’, ‘ok’, fn_index=2)

But got an error: Any ideas?

Traceback (most recent call last):
File “/home/user/.local/lib/python3.9/site-packages/gradio/routes.py”, line 437, in run_predict
output = await app.get_blocks().process_api(
File “/home/user/.local/lib/python3.9/site-packages/gradio/blocks.py”, line 1352, in process_api
result = await self.call_function(
File “/home/user/.local/lib/python3.9/site-packages/gradio/blocks.py”, line 1077, in call_function
prediction = await anyio.to_thread.run_sync(
File “/home/user/.local/lib/python3.9/site-packages/anyio/to_thread.py”, line 33, in run_sync
return await get_asynclib().run_sync_in_worker_thread(
File “/home/user/.local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py”, line 877, in run_sync_in_worker_thread
return await future
File “/home/user/.local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py”, line 807, in run
result = context.run(func, *args)
File “/home/user/app/app.py”, line 34, in respond
chat_history = ask_question(message, chat_history)
File “/home/user/app/app.py”, line 10, in ask_question
_, _ = client.predict(“ok”, “ok”, fn_index=2)
File “/home/user/.local/lib/python3.9/site-packages/gradio_client/client.py”, line 272, in predict
return self.submit(*args, api_name=api_name, fn_index=fn_index).result()
File “/home/user/.local/lib/python3.9/site-packages/gradio_client/client.py”, line 973, in result
return super().result(timeout=timeout)
File “/usr/local/lib/python3.9/concurrent/futures/_base.py”, line 446, in result
return self.__get_result()
File “/usr/local/lib/python3.9/concurrent/futures/_base.py”, line 391, in __get_result
raise self._exception
File “/usr/local/lib/python3.9/concurrent/futures/thread.py”, line 58, in run
result = self.fn(*self.args, **self.kwargs)
File “/home/user/.local/lib/python3.9/site-packages/gradio_client/client.py”, line 629, in _inner
data = self.serialize(*data)
File “/home/user/.local/lib/python3.9/site-packages/gradio_client/client.py”, line 799, in serialize
o = tuple([s.serialize(d) for s, d in zip(self.serializers, data)])
File “/home/user/.local/lib/python3.9/site-packages/gradio_client/client.py”, line 799, in
o = tuple([s.serialize(d) for s, d in zip(self.serializers, data)])
File “/home/user/.local/lib/python3.9/site-packages/gradio_client/serializing.py”, line 420, in serialize
return utils.file_to_json(Path(load_dir) / x)
File “/home/user/.local/lib/python3.9/site-packages/gradio_client/utils.py”, line 430, in file_to_json
with open(file_path) as f:
FileNotFoundError: [Errno 2] No such file or directory: ‘ok’

hi @JingpuShi ,
Have you tried passing your hftoken? since it lookes like it’s a private, unpublised Space? you could also try the serialize option

client = Client("https://my-private-space.hf.space/",hf_token="hf...", serialize=False)

serialize=False fixed it. Thanks a lot for your help @radames !!!

1 Like