Huggingface-hub

Latest version: v0.23.0

Safety actively analyzes 623465 Python packages for vulnerabilities to keep your Python projects secure.

Scan your dependencies

Page 1 of 13

1.0

You are about to delete tag v1.0 on model Wauplin/my-cool-model
Proceed? [Y/n] y
Tag v1.0 deleted on Wauplin/my-cool-model


For more details, check out the [CLI guide](https://huggingface.co/docs/huggingface_hub/main/en/guides/cli#huggingface-cli-tag).

* CLI Tag Functionality by bilgehanertan in 2172

🧩 ModelHubMixin

This `ModelHubMixin` got a set of nice improvement to generate model cards and handle custom data types in the `config.json` file. More info in the [integration guide](https://huggingface.co/docs/huggingface_hub/main/en/guides/integrations#advanced-usage).

* `ModelHubMixin`: more metadata + arbitrary config types + proper guide by Wauplin in 2230
* Fix ModelHubMixin when class is a dataclass by Wauplin in 2159
* Do not document private attributes of ModelHubMixin by Wauplin in 2216
* Add support for pipeline_tag in ModelHubMixin by Wauplin in 2228

βš™οΈ Other

In a shared environment, it is now possible to set a custom path `HF_TOKEN_PATH` as environment variable so that each user of the cluster has their own access token.

* Support `HF_TOKEN_PATH` as environment variable by Wauplin in 2185

Thanks to Y4suyuki and lappemic, most custom errors defined in `huggingface_hub` are now aggregated in the same module. This makes it very easy to import them from `from huggingface_hub.errors import ...`.

* Define errors in errors.py by Y4suyuki in 2170
* Define errors in errors file by lappemic in 2202

Fixed `HFSummaryWriter` (class to seamlessly log tensorboard events to the Hub) to work with either `tensorboardX` or `torch.utils` implementation, depending on the user setup.

* Import SummaryWriter from either tensorboardX or torch.utils by Wauplin in 2205

Speed to list files using `HfFileSystem` has been drastically improved, thanks to awgr. The values returned from the cache are not deep-copied anymore, which was unfortunately the part taking the most time in the process. If users want to modify values returned by `HfFileSystem`, they would need to copy them before-hand. This is expected to be a very limited drawback.

* fix: performance of _ls_tree by awgr in 2103

Progress bars in `huggingface_hub` got some flexibility!
It is now possible to provide a name to a tqdm bar (similar to `logging.getLogger`) and to enable/disable only some progress bars. More details in [this guide](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/utilities#configure-progress-bars).

py
>>> from huggingface_hub.utils import tqdm, disable_progress_bars
>>> disable_progress_bars("peft.foo")

No progress bars for `peft.boo.bar`
>>> for _ in tqdm(range(5), name="peft.foo.bar"):
... pass

But for `peft` yes
>>> for _ in tqdm(range(5), name="peft"):
... pass
100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 5/5 [00:00<00:00, 117817.53it/s]


* Implement hierarchical progress bar control in huggingface_hub by lappemic in 2217

πŸ’” Breaking changes

`--local-dir-use-symlink` and `--resume-download`

As part of the download process revamp, some breaking changes have been introduced. However we believe that the benefits outweigh the change cost. Breaking changes include:
- a `.cache/huggingface/` folder is not present at the root of the local dir. It only contains file locks, metadata and partially downloaded files. If you need to, you can safely delete this folder without corrupting the data inside the root folder. However, you should expect a longer recovery time if you try to re-run your download command.
- `--local-dir-use-symlink` is not in used anymore and will be ignored. It is not possible anymore to symlinks your local dir with the cache directory. Thanks to the `.cache/huggingface/` folder, it shouldn't be needed anyway.
- `--resume-download` has been deprecated and will be ignored. Resuming failed downloads is now activated by default all the time. If you need to force a new download, use `--force-download`.

Inference Types

As part of 2237 (Grammar and Tools support), we've updated the return value from `InferenceClient.chat_completion` and `InferenceClient.text_generation` to match exactly TGI output. The attributes of the returned objects did not change but the classes definition themselves yes. Expect errors if you've previously had `from huggingface_hub import TextGenerationOutput` in your code. This is however not the common usage since those objects are already instantiated by `huggingface_hub` directly.

Expected breaking changes

Some other breaking changes were expected (and announced since 0.19.x):
- `list_files_info` is definitively removed in favor of `get_paths_info` and `list_repo_tree`
- `WebhookServer.run` is definitively removed in favor of `WebhookServer.launch`
- `api_endpoint` in ModelHubMixin `push_to_hub`'s method is definitively removed in favor of the `HF_ENDPOINT` environment variable

Check 2156 for more details.

Small fixes and maintenance

βš™οΈ CI optimization

βš™οΈ fixes
* Fix HF_ENDPOINT not handled correctly by Wauplin in 2155
* Fix proxy if dynamic endpoint by Wauplin (direct commit on main)
* Update the note message when logging in to make it easier to understand and clearer by lh0x00 in 2163
* Fix URL when uploading to proxy by Wauplin in 2167
* Fix SafeTensorsInfo initialization by Wauplin in 2190
* Doc cli download timeout by zioalex in 2198
* Fix Typos in CONTRIBUTION.md and Formatting in README.md by lappemic in 2201
* change default model card by Wauplin (direct commit on main)
* Add returns documentation for save_pretrained by alexander-soare in 2226
* Update cli.md by QuinnPiers in 2242
* add warning tip that list_deployed_models only searches over cache by MoritzLaurer in 2241
* Respect default timeouts in `hf_file_system` by Wauplin in 2253
* Update harmonized token param desc and type def by lappemic in 2252
* Better document download attribute by Wauplin in 2250
* Correctly check inference endpoint is ready by Wauplin in 2229
* Add support for `updatedRefs` in WebhookPayload by Wauplin in 2169

βš™οΈ internal
* prepare for 0.23 by Wauplin in 2156
* lint by Wauplin (direct commit on main)
* quick fix by Wauplin (direct commit on main)
* Fix CI (inference tests, dataset viewer user, mypy) by Wauplin in 2208
* link by Wauplin (direct commit on main)
* Fix circular imports in eager mode? by Wauplin in 2211
* Drop generic from InferenceAPI framework list by Wauplin in 2240
* Remove test sort by acsending likes by Wauplin in 2243
* Delete legacy tests in `TestHfHubDownloadRelativePaths` + implicit delete folder is ok by Wauplin in 2259
* small doc clarification by julien-c [2261](https://github.com/huggingface/huggingface_hub/pull/2261)

Significant community contributions

The following contributors have made significant changes to the library over the last release:

* lappemic
* Fix Typos in CONTRIBUTION.md and Formatting in README.md ([2201](https://github.com/huggingface/huggingface_hub/pull/2201))
* Define errors in errors file ([2202](https://github.com/huggingface/huggingface_hub/pull/2202))
* [wip] Implement hierarchical progress bar control in huggingface_hub ([2217](https://github.com/huggingface/huggingface_hub/pull/2217))
* Update harmonized token param desc and type def ([2252](https://github.com/huggingface/huggingface_hub/pull/2252))
* bilgehanertan
* User API endpoints ([2147](https://github.com/huggingface/huggingface_hub/pull/2147))
* CLI Tag Functionality ([2172](https://github.com/huggingface/huggingface_hub/pull/2172))
* cjfghk5697
* 🌐 [i18n-KO] Translated `guides/repository.md` to Korean ([2124](https://github.com/huggingface/huggingface_hub/pull/2124))
* 🌐 [i18n-KO] Translated `package_reference/inference_client.md` to Korean ([2178](https://github.com/huggingface/huggingface_hub/pull/2178))
* 🌐 [i18n-KO] Translated `package_reference/utilities.md` to Korean ([2196](https://github.com/huggingface/huggingface_hub/pull/2196))
* SeungAhSon
* 🌐 [i18n-KO] Translated `guides/model_cards.md` to Korean" ([2128](https://github.com/huggingface/huggingface_hub/pull/2128))
* 🌐 [i18n-KO] Translated `reference/login.md` to Korean ([2151](https://github.com/huggingface/huggingface_hub/pull/2151))
* 🌐 [i18n-KO] Translated package_reference/hf_file_system.md to Korean ([2174](https://github.com/huggingface/huggingface_hub/pull/2174))
* seoulsky-field
* 🌐 [i18n-KO] Translated `guides/community.md` to Korean ([2126](https://github.com/huggingface/huggingface_hub/pull/2126))
* Y4suyuki
* Define errors in errors.py ([2170](https://github.com/huggingface/huggingface_hub/pull/2170))
* harheem
* 🌐 [i18n-KO] Translated `guides/cli.md` to Korean ([2131](https://github.com/huggingface/huggingface_hub/pull/2131))
* 🌐 [i18n-KO] Translated `reference/inference_endpoints.md` to Korean ([2180](https://github.com/huggingface/huggingface_hub/pull/2180))
* seoyoung-3060
* 🌐 [i18n-KO] Translated `guides/search.md` to Korean ([2134](https://github.com/huggingface/huggingface_hub/pull/2134))
* 🌐 [i18n-KO] Translated `package_reference/file_download.md` to Korean ([2184](https://github.com/huggingface/huggingface_hub/pull/2184))
* 🌐 [i18n-KO] Translated package_reference/serialization.md to Korean ([2233](https://github.com/huggingface/huggingface_hub/pull/2233))
* boyunJang
* 🌐 [i18n-KO] Translated `guides/inference.md` to Korean ([2130](https://github.com/huggingface/huggingface_hub/pull/2130))
* 🌐 [i18n-KO] Translated `package_reference/collections.md` to Korean ([2214](https://github.com/huggingface/huggingface_hub/pull/2214))
* 🌐 [i18n-KO] Translated `package_reference/space_runtime.md` to Korean ([2213](https://github.com/huggingface/huggingface_hub/pull/2213))
* 🌐 [i18n-KO] Translated `guides/manage-spaces.md` to Korean ([2220](https://github.com/huggingface/huggingface_hub/pull/2220))
* nuatmochoi
* 🌐 [i18n-KO] Translated `guides/webhooks_server.md` to Korean ([2145](https://github.com/huggingface/huggingface_hub/pull/2145))
* 🌐 [i18n-KO] Translated `package_reference/cache.md` to Korean ([2191](https://github.com/huggingface/huggingface_hub/pull/2191))
* fabxoe
* 🌐 [i18n-KO] Translated `package_reference/tensorboard.md` to Korean ([2173](https://github.com/huggingface/huggingface_hub/pull/2173))
* 🌐 [i18n-KO] Translated `package_reference/inference_types.md` to Korean ([2171](https://github.com/huggingface/huggingface_hub/pull/2171))
* 🌐 [i18n-KO] Translated `package_reference/hf_api.md` to Korean ([2165](https://github.com/huggingface/huggingface_hub/pull/2165))
* 🌐 [i18n-KO] Translated `package_reference/mixins.md` to Korean ([2166](https://github.com/huggingface/huggingface_hub/pull/2166))
* junejae
* 🌐 [i18n-KO] Translated `guides/upload.md` to Korean ([2139](https://github.com/huggingface/huggingface_hub/pull/2139))
* 🌐 [i18n-KO] Translated `reference/repository.md` to Korean ([2189](https://github.com/huggingface/huggingface_hub/pull/2189))
* heuristicwave
* 🌐 [i18n-KO] Translating `guides/hf_file_system.md` to Korean ([2146](https://github.com/huggingface/huggingface_hub/pull/2146))
* usr-bin-ksh
* 🌐 [i18n-KO] Translated `guides/inference_endpoints.md` to Korean ([2164](https://github.com/huggingface/huggingface_hub/pull/2164))

0.999

_update_metadata_model_index(existing_results, new_results, overwrite=True)


[{'dataset': {'name': 'IMDb', 'type': 'imdb'},
'metrics': [{'name': 'Accuracy', 'type': 'accuracy', 'value': 0.999}],
'task': {'name': 'Text Classification', 'type': 'text-classification'}}]


2. Add new metric to existing result

py
new_results = deepcopy(existing_results)
new_results[0]["metrics"][0]["name"] = "Recall"
new_results[0]["metrics"][0]["type"] = "recall"


[{'dataset': {'name': 'IMDb', 'type': 'imdb'},
'metrics': [{'name': 'Accuracy', 'type': 'accuracy', 'value': 0.995},
{'name': 'Recall', 'type': 'recall', 'value': 0.995}],
'task': {'name': 'Text Classification', 'type': 'text-classification'}}]


3. Add new result
py
new_results = deepcopy(existing_results)
new_results[0]["dataset"] = {'name': 'IMDb-2', 'type': 'imdb_2'}


[{'dataset': {'name': 'IMDb', 'type': 'imdb'},
'metrics': [{'name': 'Accuracy', 'type': 'accuracy', 'value': 0.995}],
'task': {'name': 'Text Classification', 'type': 'text-classification'}},
{'dataset': ({'name': 'IMDb-2', 'type': 'imdb_2'},),
'metrics': [{'name': 'Accuracy', 'type': 'accuracy', 'value': 0.995}],
'task': {'name': 'Text Classification', 'type': 'text-classification'}}]


* ENH Add update metadata to repocard by lvwerra in 844

Improvements and bug fixes

* Keras: Saving history in a JSON file by merveenoyan in 861
* space after uri by leondz in 866

0.23.0

πŸ“ Seamless download to local dir

The `0.23.0` release comes with a big revamp of the download process, especially when it comes to downloading to a local directory. Previously the process was still involving the cache directory and symlinks which led to misconceptions and a suboptimal user experience. The new workflow involves a `.cache/huggingface/` folder, similar to the `.git/` one, that keeps track of the progress of a download. The main features are:
- no symlinks
- no local copy
- don't re-download when not necessary
- same behavior on both Unix and Windows
- unrelated to cache-system

Example to download q4 GGUF file for [microsoft/Phi-3-mini-4k-instruct-gguf](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-gguf):

sh
Download q4 GGUF file from
huggingface-cli download microsoft/Phi-3-mini-4k-instruct-gguf Phi-3-mini-4k-instruct-q4.gguf --local-dir=data/phi3


With this addition, **interrupted downloads are now resumable**! This applies both for downloads in local and cache directories which should greatly improve UX for users with slow/unreliable connections. In this regard, the `resume_download` parameter is now deprecated (not relevant anymore).

* Revamp download to local dir process by Wauplin in 2223
* Rename `.huggingface/` folder to `.cache/huggingface/` by Wauplin in 2262

πŸ’‘ Grammar and Tools in `InferenceClient`

It is now possible to provide a list of tools when chatting with a model using the `InferenceClient`! This major improvement has been made possible thanks to TGI that handle them natively.

py
>>> from huggingface_hub import InferenceClient

Ask for weather in the next days using tools
>>> client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct")
>>> messages = [
... {"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."},
... {"role": "user", "content": "What's the weather like the next 3 days in San Francisco, CA?"},
... ]
>>> tools = [
... {
... "type": "function",
... "function": {
... "name": "get_current_weather",
... "description": "Get the current weather",
... "parameters": {
... "type": "object",
... "properties": {
... "location": {
... "type": "string",
... "description": "The city and state, e.g. San Francisco, CA",
... },
... "format": {
... "type": "string",
... "enum": ["celsius", "fahrenheit"],
... "description": "The temperature unit to use. Infer this from the users location.",
... },
... },
... "required": ["location", "format"],
... },
... },
... },
... ...
... ]
>>> response = client.chat_completion(
... model="meta-llama/Meta-Llama-3-70B-Instruct",
... messages=messages,
... tools=tools,
... tool_choice="auto",
... max_tokens=500,
... )
>>> response.choices[0].message.tool_calls[0].function
ChatCompletionOutputFunctionDefinition(
arguments={
'location': 'San Francisco, CA',
'format': 'fahrenheit',
'num_days': 3
},
name='get_n_day_weather_forecast',
description=None
)


It is also possible to provide grammar rules to the `text_generation` task. This ensures that the output follows a precise JSON Schema specification or matches a regular expression. For more details about it, check out the [Guidance guide](https://huggingface.co/docs/text-generation-inference/conceptual/guidance) from Text-Generation-Inference docs.

* Add support for Grammar/Tools + TGI-based specs in InferenceClient by Wauplin in 2237

βš™οΈ Other

Mention more `chat-completion` task instead of `conversation` in documentation.

* Add `chat_completion` and remove `conversational` from Inference guide by Wauplin in 2215

`chat-completion` relies on server-side rendering in all cases, including when model is `transformers`-backed. Previously it was only the case for TGI-backed models and templates were rendered client-side otherwise.

* Render chat-template server-side for transformers-backed models by Wauplin in 2258

Improved logic to determine whether a model is served via TGI or `transformers`.

* Raise error in chat completion when unprocessable by Wauplin in 2257

* Document more chat_completion by Wauplin in 2260

🌐 πŸ“š Korean community is on fire!

The [PseudoLab](https://pseudo-lab.com/) team is a non-profit dedicated to make AI more accessible in the Korean-speaking community. In the past few weeks, their team of contributors managed to translated (almost) entirely the `huggingface_hub` documentation. Huge shout-out to the coordination on this task! Documentation can be accessed [here](https://huggingface.co/docs/huggingface_hub/main/ko/index).

* 🌐 [i18n-KO] Translated `guides/webhooks_server.md` to Korean by nuatmochoi in 2145
* 🌐 [i18n-KO] Translated `reference/login.md` to Korean by SeungAhSon in 2151
* 🌐 [i18n-KO] Translated `package_reference/tensorboard.md` to Korean by fabxoe in 2173
* 🌐 [i18n-KO] Translated `package_reference/inference_client.md` to Korean by cjfghk5697 in 2178
* 🌐 [i18n-KO] Translated `reference/inference_endpoints.md` to Korean by harheem in 2180
* 🌐 [i18n-KO] Translated `package_reference/file_download.md` to Korean by seoyoung-3060 in 2184
* 🌐 [i18n-KO] Translated `package_reference/cache.md` to Korean by nuatmochoi in 2191
* 🌐 [i18n-KO] Translated `package_reference/collections.md` to Korean by boyunJang in 2214
* 🌐 [i18n-KO] Translated `package_reference/inference_types.md` to Korean by fabxoe in 2171
* 🌐 [i18n-KO] Translated `guides/upload.md` to Korean by junejae in 2139
* 🌐 [i18n-KO] Translated `reference/repository.md` to Korean by junejae in 2189
* 🌐 [i18n-KO] Translated `package_reference/space_runtime.md` to Korean by boyunJang in 2213
* 🌐 [i18n-KO] Translated `guides/repository.md` to Korean by cjfghk5697 in 2124
* 🌐 [i18n-KO] Translated `guides/model_cards.md` to Korean" by SeungAhSon in 2128
* 🌐 [i18n-KO] Translated `guides/community.md` to Korean by seoulsky-field in 2126
* 🌐 [i18n-KO] Translated `guides/cli.md` to Korean by harheem in 2131
* 🌐 [i18n-KO] Translated `guides/search.md` to Korean by seoyoung-3060 in 2134
* 🌐 [i18n-KO] Translated `guides/inference.md` to Korean by boyunJang in 2130
* 🌐 [i18n-KO] Translated `guides/manage-spaces.md` to Korean by boyunJang in 2220
* 🌐 [i18n-KO] Translating `guides/hf_file_system.md` to Korean by heuristicwave in 2146
* 🌐 [i18n-KO] Translated `package_reference/hf_api.md` to Korean by fabxoe in 2165
* 🌐 [i18n-KO] Translated `package_reference/mixins.md` to Korean by fabxoe in 2166
* 🌐 [i18n-KO] Translated `guides/inference_endpoints.md` to Korean by usr-bin-ksh in 2164
* 🌐 [i18n-KO] Translated `package_reference/utilities.md` to Korean by cjfghk5697 in 2196
* fix ko docs by Wauplin (direct commit on main)
* 🌐 [i18n-KO] Translated package_reference/serialization.md to Korean by seoyoung-3060 in [2233](https://github.com/huggingface/huggingface_hub/pull/2233)
* 🌐 [i18n-KO] Translated package_reference/hf_file_system.md to Korean by SeungAhSon in [2174](https://github.com/huggingface/huggingface_hub/pull/2174)

πŸ› οΈ Misc improvements

User API

bilgehanertan added support for 2 new routes:
- [`get_user_overview`](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/hf_api#huggingface_hub.HfApi.get_user_overview) to retrieve high-level information about a user: username, avatar, number of models/datasets/Spaces, number of likes and upvotes, number of interactions in discussion, etc.

* User API endpoints by bilgehanertan in 2147

CLI tag

bilgehanertan added a new command to the CLI to handle tags. It is now possible to:
- tag a repo

0.22.2

**Full Changelog**: https://github.com/huggingface/huggingface_hub/compare/v0.22.1...v0.22.2

See https://github.com/huggingface/huggingface_hub/pull/2155 and https://github.com/huggingface/huggingface_hub/pull/2167 for more details.

0.22.1

Fixed a bug breaking the SetFit integration.

What's Changed
* Fix use other chat completion providers by Wauplin in https://github.com/huggingface/huggingface_hub/pull/2153
* Fix ModelHubMixin when class is a dataclass by Wauplin in https://github.com/huggingface/huggingface_hub/pull/2159


**Full Changelog**: https://github.com/huggingface/huggingface_hub/compare/v0.22.0...v0.22.1

0.22.0

Discuss about the release in our [Community Tab](https://huggingface.co/spaces/Wauplin/huggingface_hub/discussions/5). Feedback is welcome!! πŸ€—

✨ InferenceClient

Support for inference tools continues to improve in `huggingface_hub`. At the menu in this release? A new `chat_completion` API and fully typed inputs/outputs!

Chat-completion API!

A long-awaited API has just landed in `huggingface_hub`! `InferenceClient.chat_completion` follows most of OpenAI's API, making it much easier to integrate with existing tools.

Technically speaking it uses the same backend as the `text-generation` task but requires a preprocessing step to format the list of messages into a single text prompt. The chat template is rendered server-side when models are powered by [TGI](https://huggingface.co/docs/text-generation-inference/index), which is the case for most LLMs: Llama, Zephyr, Mistral, Gemma, etc. Otherwise, the templating happens client-side which requires `minijinja` package to be installed. We are actively working on bridging this gap, aiming at rendering all templates server-side in the future.

py
>>> from huggingface_hub import InferenceClient
>>> messages = [{"role": "user", "content": "What is the capital of France?"}]
>>> client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")

Batch completion
>>> client.chat_completion(messages, max_tokens=100)
ChatCompletionOutput(
choices=[
ChatCompletionOutputChoice(
finish_reason='eos_token',
index=0,
message=ChatCompletionOutputChoiceMessage(
content='The capital of France is Paris. The official name of the city is "Ville de Paris" (City of Paris) and the name of the country\'s governing body, which is located in Paris, is "La République française" (The French Republic). \nI hope that helps! Let me know if you need any further information.'
)
)
],
created=1710498360
)

Stream new tokens one by one
>>> for token in client.chat_completion(messages, max_tokens=10, stream=True):
... print(token)
ChatCompletionStreamOutput(choices=[ChatCompletionStreamOutputChoice(delta=ChatCompletionStreamOutputDelta(content='The', role='assistant'), index=0, finish_reason=None)], created=1710498504)
ChatCompletionStreamOutput(choices=[ChatCompletionStreamOutputChoice(delta=ChatCompletionStreamOutputDelta(content=' capital', role='assistant'), index=0, finish_reason=None)], created=1710498504)
(...)
ChatCompletionStreamOutput(choices=[ChatCompletionStreamOutputChoice(delta=ChatCompletionStreamOutputDelta(content=' may', role='assistant'), index=0, finish_reason=None)], created=1710498504)
ChatCompletionStreamOutput(choices=[ChatCompletionStreamOutputChoice(delta=ChatCompletionStreamOutputDelta(content=None, role=None), index=0, finish_reason='length')], created=1710498504)


* Implement `InferenceClient.chat_completion` + use new types for text-generation by Wauplin in [2094](https://github.com/huggingface/huggingface_hub/pull/2094)
* Fix InferenceClient.text_generation for non-tgi models by Wauplin in [2136](https://github.com/huggingface/huggingface_hub/pull/2136)
* https://github.com/huggingface/huggingface_hub/pull/2153 by Wauplin in [#2153](https://github.com/huggingface/huggingface_hub/pull/2153)

Inference types

We are currently working towards more consistency in tasks definitions across the Hugging Face ecosystem. This is no easy job but a major milestone has recently been achieved! All inputs and outputs of the main ML tasks are now fully specified as JSONschema objects. This is the first brick needed to have consistent expectations when running inference across our stack: transformers (Python), transformers.js (Typescript), Inference API (Python), Inference Endpoints (Python), Text Generation Inference (Rust), Text Embeddings Inference (Rust), InferenceClient (Python), Inference.js (Typescript), etc.

Integrating those definitions will require more work but `huggingface_hub` is one of the first tools to integrate them. As a start, **all `InferenceClient` return values are now typed dataclasses.** Furthermore, typed dataclasses have been generated for all tasks' inputs and outputs. This means you can now integrate them in your own library to ensure consistency with the Hugging Face ecosystem. Specifications are open-source (see [here](https://github.com/huggingface/huggingface.js/tree/main/packages/tasks/src/tasks)) meaning anyone can access and contribute to them. Python's generated classes are documented [here](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_types).

Here is a short example showcasing the new output types:

py
>>> from huggingface_hub import InferenceClient
>>> client = InferenceClient()
>>> client.object_detection("people.jpg"):
[
ObjectDetectionOutputElement(
score=0.9486683011054993,
label='person',
box=ObjectDetectionBoundingBox(xmin=59, ymin=39, xmax=420, ymax=510)
),
...
]


Note that those dataclasses are backward-compatible with the dict-based interface that was previously in use. In the example above, both `ObjectDetectionBoundingBox(...).xmin` and `ObjectDetectionBoundingBox(...)["xmin"]` are correct, even though the former should be the preferred solution from now on.

* Generate inference types + start using output types by Wauplin in [2036](https://github.com/huggingface/huggingface_hub/pull/2036)
* Add = None at optional parameters by LysandreJik in [2095](https://github.com/huggingface/huggingface_hub/pull/2095)
* Fix inference types shared between tasks by Wauplin in [2125](https://github.com/huggingface/huggingface_hub/pull/2125)

🧩 ModelHubMixin

[`ModelHubMixin`](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/mixins#huggingface_hub.ModelHubMixin) is an object that can be used as a parent class for the objects in your library in order to provide built-in serialization methods to upload and download pretrained models from the Hub. This mixin is adapted into a [`PyTorchHubMixin`](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) that can serialize and deserialize any Pytorch model. The 0.22 release brings its share of improvements to these classes:
1. Better support of init values. If you instantiate a model with some custom arguments, the values will be automatically stored in a config.json file and restored when reloading the model from pretrained weights. This should unlock integrations with external libraries in a much smoother way.
2. Library authors integrating the hub mixin can now define custom metadata for their library: library name, tags, document url and repo url. These are to be defined only once when integrating the library. Any model pushed to the Hub using the library will then be easily discoverable thanks to those tags.
3. A base modelcard is generated for each saved model. This modelcard includes default tags (e.g. `model_hub_mixin`) and custom tags from the library (see 2.). You can extend/modify this modelcard by overwriting the `generate_model_card` method.

python
>>> import torch
>>> import torch.nn as nn
>>> from huggingface_hub import PyTorchModelHubMixin


Define your Pytorch model exactly the same way you are used to
>>> class MyModel(
... nn.Module,
... PyTorchModelHubMixin, multiple inheritance
... library_name="keras-nlp",
... tags=["keras"],
... repo_url="https://github.com/keras-team/keras-nlp",
... docs_url="https://keras.io/keras_nlp/",
... ^ optional metadata to generate model card
... ):
... def __init__(self, hidden_size: int = 512, vocab_size: int = 30000, output_size: int = 4):
... super().__init__()
... self.param = nn.Parameter(torch.rand(hidden_size, vocab_size))
... self.linear = nn.Linear(output_size, vocab_size)

... def forward(self, x):
... return self.linear(x + self.param)

1. Create model
>>> model = MyModel(hidden_size=128)

Config is automatically created based on input + default values
>>> model._hub_mixin_config
{"hidden_size": 128, "vocab_size": 30000, "output_size": 4}

2. (optional) Save model to local directory
>>> model.save_pretrained("path/to/my-awesome-model")

3. Push model weights to the Hub
>>> model.push_to_hub("my-awesome-model")

4. Initialize model from the Hub => config has been preserved
>>> model = MyModel.from_pretrained("username/my-awesome-model")
>>> model._hub_mixin_config
{"hidden_size": 128, "vocab_size": 30000, "output_size": 4}

Model card has been correctly populated
>>> from huggingface_hub import ModelCard
>>> card = ModelCard.load("username/my-awesome-model")
>>> card.data.tags
["keras", "pytorch_model_hub_mixin", "model_hub_mixin"]
>>> card.data.library_name
"keras-nlp"


For more details on how to integrate these classes, check out the [integration guide](https://huggingface.co/docs/huggingface_hub/main/en/guides/integrations#a-more-complex-approach-class-inheritance).

* Fix `ModelHubMixin`: pass config when `__init__` accepts **kwargs by Wauplin in [2058](https://github.com/huggingface/huggingface_hub/pull/2058)
* [PyTorchModelHubMixin] Fix saving model with shared tensors by NielsRogge in [2086](https://github.com/huggingface/huggingface_hub/pull/2086)
* Correctly inject config in `PytorchModelHubMixin` by Wauplin in [2079](https://github.com/huggingface/huggingface_hub/pull/2079)
* Fix passing kwargs in PytorchHubMixin by Wauplin in [2093](https://github.com/huggingface/huggingface_hub/pull/2093)
* Generate modelcard in `ModelHubMixin` by Wauplin in [2080](https://github.com/huggingface/huggingface_hub/pull/2080)
* Fix ModelHubMixin: save config only if doesn't exist by Wauplin in [2105](https://github.com/huggingface/huggingface_hub/pull/2105)
* Fix ModelHubMixin - kwargs should be passed correctly when reloading by Wauplin in [2099](https://github.com/huggingface/huggingface_hub/pull/2099)
* Fix ModelHubMixin when kwargs and config are both passed by Wauplin in [2138](https://github.com/huggingface/huggingface_hub/pull/2138)
* ModelHubMixin overwrite config if preexistant by Wauplin in [2142](https://github.com/huggingface/huggingface_hub/pull/2142)

πŸ› οΈ Misc improvements

`HfFileSystem` download speed was limited by some internal logic in `fsspec`. We've now updated the `get_file` and `read` implementations to improve their download speed to a level similar to `hf_hub_download`.

* Fast download in hf file system by Wauplin in [2143](https://github.com/huggingface/huggingface_hub/pull/2143)

We are aiming at moving all errors raised by `huggingface_hub` into a single module `huggingface_hub.errors` to ease the developer experience. This work has been started as a community contribution from Y4suyuki.

* Start defining custom errors in one place by Y4suyuki in [2122](https://github.com/huggingface/huggingface_hub/pull/2122)

`HfApi` class now accepts a `headers` parameters that is then passed to every HTTP call made to the Hub.

* Allow passing custom headers to HfApi by Wauplin in [2098](https://github.com/huggingface/huggingface_hub/pull/2098)

πŸ“š More documentation in Korean!

* [i18n-KO] Translated `package_reference/overview.md` to Korean by jungnerd in [2113](https://github.com/huggingface/huggingface_hub/pull/2113)

πŸ’” Breaking changes

- The new types returned by `InferenceClient` methods should be backward compatible, especially to access values either as attributes (`.my_field`) or as items (i.e. `["my_field"]`). However, dataclasses and dicts do not always behave exactly the same so might notice some breaking changes. Those breaking changes should be very limited.

- `ModelHubMixin` internals changed quite a bit, breaking *some* use cases. We don't think those use cases were in use and changing them should really benefit 99% of integrations. If you witness any inconsistency or error in your integration, please let us know and we will do our best to mitigate the problem. One of the biggest change is that the config values are not attached to the mixin instance as `instance.config` anymore but as `instance._model_hub_mixin`. The `.config` attribute has been mistakenly introduced in `0.20.x` so we hope it has not been used much yet.

- `huggingface_hub.file_download.http_user_agent` has been removed in favor of the officially document `huggingface_hub.utils.build_hf_headers`. It was a deprecated method since `0.18.x`.

Small fixes and maintenance

βš™οΈ CI optimization

The CI pipeline has been greatly improved, especially thanks to the efforts from bmuskalla. Most tests are now passing in under 3 minutes, against 8 to 10 minutes previously. Some long-running tests have been greatly simplified and all tests are now ran in parallel with `python-xdist`, thanks to a complete decorrelation between them.

We are now also using the great [`uv`](https://github.com/astral-sh/uv) installer instead of `pip` in our CI, which saves around 30-40s per pipeline.

* More optimized tests by Wauplin in [2054](https://github.com/huggingface/huggingface_hub/pull/2054)
* Enable `python-xdist` on all tests by bmuskalla in [2059](https://github.com/huggingface/huggingface_hub/pull/2059)
* do not list all models by Wauplin in [2061](https://github.com/huggingface/huggingface_hub/pull/2061)
* update ruff by Wauplin in [2071](https://github.com/huggingface/huggingface_hub/pull/2071)
* Use uv in CI to speed-up requirements install by Wauplin in [2072](https://github.com/huggingface/huggingface_hub/pull/2072)


βš™οΈ fixes
* Fix Space variable when updatedAt is missing by Wauplin in [2050](https://github.com/huggingface/huggingface_hub/pull/2050)
* Fix tests involving temp directory on macOS by bmuskalla in [2052](https://github.com/huggingface/huggingface_hub/pull/2052)
* fix glob no magic by lhoestq in [2056](https://github.com/huggingface/huggingface_hub/pull/2056)
* Point out that the token must have write scope by bmuskalla in [2053](https://github.com/huggingface/huggingface_hub/pull/2053)
* Fix commonpath in read-only filesystem by stevelaskaridis in [2073](https://github.com/huggingface/huggingface_hub/pull/2073)
* rm unnecessary early makedirs by poedator in [2092](https://github.com/huggingface/huggingface_hub/pull/2092)
* Fix unhandled filelock issue by Wauplin in [2108](https://github.com/huggingface/huggingface_hub/pull/2108)
* Handle .DS_Store files in _scan_cache_repos by sealad886 in [2112](https://github.com/huggingface/huggingface_hub/pull/2112)
* Fix REPO_API_REGEX by Wauplin in [2119](https://github.com/huggingface/huggingface_hub/pull/2119)
* Fix uploading to HF proxy by Wauplin in [2120](https://github.com/huggingface/huggingface_hub/pull/2120)
* Fix --delete in huggingface-cli upload command by Wauplin in [2129](https://github.com/huggingface/huggingface_hub/pull/2129)
* Explicitly fail on Keras3 by Wauplin in [2107](https://github.com/huggingface/huggingface_hub/pull/2107)
* Fix serverless naming by Wauplin in [2137](https://github.com/huggingface/huggingface_hub/pull/2137)

βš™οΈ internal
* tag as 0.22.0.dev + remove deprecated code by Wauplin in [2049](https://github.com/huggingface/huggingface_hub/pull/2049)
* Some cleaning by Wauplin in [2070](https://github.com/huggingface/huggingface_hub/pull/2070)
* Fix test test_delete_branch_on_missing_branch_fails by Wauplin in [2088](https://github.com/huggingface/huggingface_hub/pull/2088)


Significant community contributions

The following contributors have made significant changes to the library over the last release:

* Y4suyuki
* Start defining custom errors in one place ([2122](https://github.com/huggingface/huggingface_hub/pull/2122))
* bmuskalla
* Enable `python-xdist` on all tests by bmuskalla in [2059](https://github.com/huggingface/huggingface_hub/pull/2059)

Page 1 of 13

Β© 2024 Safety CLI Cybersecurity Inc. All Rights Reserved.