Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support asyncio with AsyncInferenceClient #1524

Merged
merged 47 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
7fcb534
prepare for async
Wauplin Jun 21, 2023
15003b2
more stuff
Wauplin Jun 21, 2023
6b1fc86
comments
Wauplin Jun 21, 2023
497fca4
add aiohttp to deps
Wauplin Jun 21, 2023
19f2ae6
prepare script to generate AsyncInferenceClient
Wauplin Jun 21, 2023
cfee5aa
some progress
Wauplin Jun 21, 2023
8daba37
more progress
Wauplin Jun 21, 2023
098893a
make methods async
Wauplin Jun 21, 2023
4b420ca
await post calls
Wauplin Jun 21, 2023
37ce4b4
remove examples in AsyncClient
Wauplin Jun 21, 2023
c60f79e
working generating script?
Wauplin Jun 21, 2023
34bb2e3
Merge branch 'main' into inference-async-client
Wauplin Jun 27, 2023
a3d35cd
make async
Wauplin Jun 27, 2023
4d630b6
explicit regexes
Wauplin Jun 27, 2023
ba064d4
working AsyncInferenceClient
Wauplin Jun 27, 2023
f421fdf
moving further
Wauplin Jun 27, 2023
020cf17
closer to a solution
Wauplin Jun 27, 2023
514e656
more explicit?
Wauplin Jun 27, 2023
130554d
fix typing
Wauplin Jun 27, 2023
9736aaf
fix tests
Wauplin Jun 27, 2023
0556631
fix tests for real
Wauplin Jun 27, 2023
1dfb0db
Examples in async client
Wauplin Jun 28, 2023
6ddbcd0
Merge branch 'main' into inference-async-client
Wauplin Jun 28, 2023
520bd91
adapt async text generation code
Wauplin Jun 28, 2023
be7a50f
better handling of errors
Wauplin Jun 28, 2023
19a5b20
first async tests
Wauplin Jun 28, 2023
deae6ae
fix tests for python3.7
Wauplin Jun 28, 2023
e1ccda7
add sentence_similarity tests
Wauplin Jun 28, 2023
cffb9e5
reference
Wauplin Jun 28, 2023
8dcf411
docs
Wauplin Jun 28, 2023
5d4ce6c
fix setup.py
Wauplin Jun 28, 2023
803f0b5
Merge branch 'main' into inference-async-client
Wauplin Jun 29, 2023
e45e01a
docs
Wauplin Jun 29, 2023
1fff6bb
fix styling
Wauplin Jun 29, 2023
4ac38de
fix tests
Wauplin Jun 29, 2023
866da17
Merge branch 'main' into inference-async-client
Wauplin Jun 29, 2023
0fec22e
Merge branch 'main' into inference-async-client
Wauplin Jun 29, 2023
04a033d
fix zero-shot task
Wauplin Jun 29, 2023
9386c8a
Merge branch 'main' into inference-async-client
Wauplin Jun 29, 2023
b1ee580
fix zero class
Wauplin Jun 29, 2023
ec1104d
Merge branch 'main' into inference-async-client
Wauplin Jul 4, 2023
750759e
Move _async_client.py to a _generated/ folder
Wauplin Jul 4, 2023
42feafc
add copyright to tests
Wauplin Jul 4, 2023
c321ec0
fix imports
Wauplin Jul 4, 2023
3730075
fix import
Wauplin Jul 4, 2023
153e2d9
fix async text_generation return type when stream=True
Wauplin Jul 4, 2023
adb970d
test async methods signatures
Wauplin Jul 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/python-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
- run: ruff tests src contrib
- run: python utils/check_contrib_list.py
- run: python utils/check_static_imports.py
- run: python utils/generate_async_inference_client.py

# Run type checking at least on huggingface_hub root file to check all modules
# that can be lazy-loaded actually exist.
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ quality:
mypy src
python utils/check_contrib_list.py
python utils/check_static_imports.py
python utils/generate_async_inference_client.py

style:
black $(check_dirs)
ruff $(check_dirs) --fix
python utils/check_contrib_list.py --update
python utils/check_static_imports.py --update
python utils/generate_async_inference_client.py --update

repocard:
python utils/push_repocard_examples.py
Expand Down
30 changes: 30 additions & 0 deletions docs/source/guides/inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,36 @@ image. [`InferenceClient.post`] is also useful to handle tasks that are not yet
b'...'
```

## Async client

An async version of the client is also provided, based on `asyncio` and `aiohttp`. You can either install `aiohttp`
directly or use the `[inference]` extra:

```sh
pip install --upgrade huggingface_hub[inference]
# or
# pip install aiohttp
```

After installation all async API endpoints are available via [`AsyncInferenceClient`]. Its initialization and APIs are
strictly the same as the sync-only version.

```py
# Code must be run in a asyncio concurrent context.
# $ python -m asyncio
>>> from huggingface_hub import AsyncInferenceClient
>>> client = AsyncInferenceClient()

>>> image = await client.text_to_image("An astronaut riding a horse on the moon.")
>>> image.save("astronaut.png")

>>> async for token in await client.text_generation("The Huggingface Hub is", stream=True):
... print(token, end="")
a platform for sharing and discussing ML-related content.
```

For more information about the `asyncio` module, please refer to the [official documentation](https://docs.python.org/3/library/asyncio.html).

## Advanced tips

In the above section, we saw the main aspects of [`InferenceClient`]. Let's dive into some more advanced tips.
Expand Down
13 changes: 13 additions & 0 deletions docs/source/package_reference/inference_client.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ for more information on how to use it.

[[autodoc]] InferenceClient

## Async Inference Client

An async version of the client is also provided, based on `asyncio` and `aiohttp`.
To use it, you can either install `aiohttp` directly or use the `[inference]` extra:

```sh
pip install --upgrade huggingface_hub[inference]
# or
# pip install aiohttp
```

[[autodoc]] AsyncInferenceClient

### InferenceTimeoutError

[[autodoc]] InferenceTimeoutError
Expand Down
39 changes: 24 additions & 15 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def get_version() -> str:
# Note: installs `prompt-toolkit` in the background
]

extras["inference"] = [
"aiohttp", # for AsyncInferenceClient
"pydantic", # for text-generation-inference
]

extras["torch"] = [
"torch",
]
Expand All @@ -42,21 +47,25 @@ def get_version() -> str:
extras["tensorflow"] = ["tensorflow", "pydot", "graphviz"]


extras["testing"] = extras["cli"] + [
"jedi",
"Jinja2",
"pytest",
"pytest-cov",
"pytest-env",
"pytest-xdist",
"pytest-vcr", # to mock Inference
"urllib3<2.0", # VCR.py broken with urllib3 2.0 (see https://urllib3.readthedocs.io/en/stable/v2-migration-guide.html)
"soundfile",
"Pillow",
"gradio", # to test webhooks
"numpy", # for embeddings
"pydantic", # for text-generation-inference
]
extras["testing"] = (
extras["cli"]
+ extras["inference"]
+ [
"jedi",
"Jinja2",
"pytest",
"pytest-cov",
"pytest-env",
"pytest-xdist",
"pytest-vcr", # to mock Inference
"pytest-asyncio", # for AsyncInferenceClient
"urllib3<2.0", # VCR.py broken with urllib3 2.0 (see https://urllib3.readthedocs.io/en/stable/v2-migration-guide.html)
"soundfile",
"Pillow",
"gradio", # to test webhooks
"numpy", # for embeddings
]
)

# Typing extra dependencies list is duplicated in `.pre-commit-config.yaml`
# Please make sure to update the list there when adding a new typing dependency.
Expand Down
4 changes: 4 additions & 0 deletions src/huggingface_hub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@
"InferenceClient",
"InferenceTimeoutError",
],
"inference._generated._async_client": [
"AsyncInferenceClient",
],
"inference_api": [
"InferenceApi",
],
Expand Down Expand Up @@ -503,6 +506,7 @@ def __dir__():
InferenceClient, # noqa: F401
InferenceTimeoutError, # noqa: F401
)
from .inference._generated._async_client import AsyncInferenceClient # noqa: F401
from .inference_api import InferenceApi # noqa: F401
from .keras_mixin import (
KerasModelHubMixin, # noqa: F401
Expand Down
Loading