You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/hub/models-uploading.md
+77-5Lines changed: 77 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,13 @@ To upload models to the Hub, you'll need to create an account at [Hugging Face](
4
4
5
5
You can link repositories with an individual user, such as [osanseviero/fashion_brands_patterns](https://huggingface.co/osanseviero/fashion_brands_patterns), or with an organization, such as [facebook/bart-large-xsum](https://huggingface.co/facebook/bart-large-xsum). Organizations can collect models related to a company, community, or library! If you choose an organization, the model will be featured on the organization’s page, and every member of the organization will have the ability to contribute to the repository. You can create a new organization [here](https://huggingface.co/organizations/new).
6
6
7
-
There are several ways to upload models to the Hub, described below. We suggest adding a [Model Card](./model-cards) to your repo to document your model.
7
+
There are several ways to upload models to the Hub, described below.
8
+
9
+
- In case your model comes from a library that has [built-in support](#upload-from-a-library-with-built-in-support), one can use the existing methods.
10
+
- In case your model is a custom PyTorch model, the recommended way is to leverage the [huggingface_hub](#using-the-huggingface_hub-client-library) Python library as it allows to add `from_pretrained`, `push_to_hub` and [automated download metrics](https://huggingface.co/docs/hub/models-download-stats) capabilities to your models, just like models in the Transformers, Diffusers and Timm libraries.
11
+
- In addition to programmatic uploads, you can always use the [web interface](#using-the-web-interface).
12
+
13
+
Once your model is uploaded, we suggest adding a [Model Card](./model-cards) to your repo to document your model.
8
14
9
15
## Using the web interface
10
16
@@ -66,10 +72,76 @@ Any repository that contains TensorBoard traces (filenames that contain `tfevent
66
72
67
73
Models trained with 🤗 Transformers will generate [TensorBoard traces](https://huggingface.co/docs/transformers/main_classes/callback#transformers.integrations.TensorBoardCallback) by default if [`tensorboard`](https://pypi.org/project/tensorboard/) is installed.
68
74
69
-
## Using Git
70
75
71
-
Since model repos are just Git repositories, you can use Git to push your model files to the Hub. Follow the guide on [Getting Started with Repositories](repositories-getting-started) to learn about using the `git` CLI to commit and push your models.
76
+
## Upload from a library with built-in support
77
+
78
+
First check if your model is from a library that has built-in support to push to/load from the Hub, like Transformers, Diffusers, Timm, Asteroid, etc.: https://huggingface.co/docs/hub/models-libraries. Below we'll show how easy this is for a library like Transformers:
79
+
80
+
```python
81
+
from transformers import BertConfig, BertModel
82
+
83
+
config = BertConfig()
84
+
model = BertModel(config)
85
+
86
+
model.push_to_hub("nielsr/my-awesome-bert-model")
87
+
88
+
# reload
89
+
model = BertModel.from_pretrained("nielsr/my-awesome-bert-model")
90
+
```
91
+
92
+
## Upload a PyTorch model using `huggingface_hub`
93
+
94
+
In case your model is a (custom) PyTorch model, you can leverage the `PyTorchModelHubMixin`[class](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) available in the [huggingface_hub](https://github.com/huggingface/huggingface_hub) Python library. It is a minimal class which adds `from_pretrained` and `push_to_hub` capabilities to any `nn.Module`, along with download metrics.
95
+
96
+
Here is how to use it (assuming you have run `pip install huggingface_hub`):
model = MyModel.from_pretrained("username/my-awesome-model")
125
+
```
126
+
127
+
As can be seen, the only thing required is to define all hyperparameters regarding the model architecture (such as hidden size, number of classes, dropout probability, etc.) in a Python dictionary often called the `config`. Next, you can define a class which takes the `config` as keyword argument in its init.
128
+
129
+
This comes with automated download metrics, meaning that you'll be able to see how many times the model is downloaded, the same way they are available for models integrated natively in the Transformers, Diffusers or Timm libraries. With this mixin class, each separate checkpoint is stored on the Hub in a single repository consisting of 2 files:
130
+
131
+
- a `pytorch_model.bin` or `model.safetensors` file containing the weights
132
+
- a `config.json` file which is a serialized version of the model configuration. This class is used for counting download metrics: everytime a user calls `from_pretrained` to load a `config.json`, the count goes up by one. See [this guide](https://huggingface.co/docs/hub/models-download-stats) regarding automated download metrics.
133
+
134
+
It's recommended to add a model card to each checkpoint so that people can read what the model is about, have a link to the paper, etc.
<small> Example [repository](https://huggingface.co/LiheYoung/depth_anything_vitl14) that leverages PyTorchModelHubMixin. Downloads are shown on the right.</small>
140
+
141
+
Visit [the huggingface_hub's documentation](https://huggingface.co/docs/huggingface_hub/guides/integrations) to learn more.
142
+
143
+
Alternatively, one can also simply programmatically upload files or folders to the hub: https://huggingface.co/docs/huggingface_hub/guides/upload.
144
+
145
+
## Using Git
74
146
75
-
The rich feature set in the `huggingface_hub` library allows you to manage repositories, including creating repos and uploading models to the Model Hub. Visit [the client library's documentation](https://huggingface.co/docs/huggingface_hub/index) to learn more.
147
+
Finally, since model repos are just Git repositories, you can also use Git to push your model files to the Hub. Follow the guide on [Getting Started with Repositories](repositories-getting-started#adding-files-to-a-repository-terminalterminal) to learn about using the `git` CLI to commit and push your models.
0 commit comments