Skip to content

Commit b80f880

Browse files
committed
docs
1 parent e05c393 commit b80f880

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

docs/source/en/guides/integrations.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,12 @@ your library, you should:
143143
model. The method must download the relevant files and load them.
144144
3. You are done!
145145

146-
The advantage of using [`ModelHubMixin`] is that once you take care of the serialization/loading of the files, you
147-
are ready to go. You don't need to worry about stuff like repo creation, commits, PRs, or revisions. All
148-
of this is handled by the mixin and is available to your users. The Mixin also ensures that public methods are well
149-
documented and type annotated.
146+
The advantage of using [`ModelHubMixin`] is that once you take care of the serialization/loading of the files, you are ready to go. You don't need to worry about stuff like repo creation, commits, PRs, or revisions. All of this is handled by the mixin and is available to your users. The Mixin also ensures that public methods are well documented and type annotated.
150147

151148
As a bonus, [`ModelHubMixin`] handles the model configuration for you. If your `__init__` method expects a `config` input, it will be automatically saved in the repo when calling `save_pretrained` and reloaded correctly by `load_pretrained`. Moreover, if the `config` input parameter is annotated with dataclass type (e.g. `config: Optional[MyConfigClass] = None`), then the `config` value will be correctly deserialized for you. Finally, all jsonable values passed at initialization will be also stored in the config file. This means you don't necessarily have to expect a `config` input to benefit from it. The big advantage of having a `config.json` file in your model repository is that it automatically enables the analytics on the Hub (e.g. the "downloads" count).
152149

150+
Finally, [`ModelHubMixin`] handles generating a model card for you. When inheriting from [`ModelHubMixin`], you can define metadata such as `library_name`, `tags`, `repo_url` and `docs_url`. Those fields will be reused to populate the modelcard of any model that use your class. This is very practical to make all models using your library easily searchable on the Hub and to provide some resource links for users landing on the Hub. If you want to extend the modelcard template, you can override the [`~ModelHubMixin.generate_model_card`] method.
151+
153152
### A concrete example: PyTorch
154153

155154
A good example of what we saw above is [`PyTorchModelHubMixin`], our integration for the PyTorch framework. This is a ready-to-use integration.
@@ -165,7 +164,15 @@ Here is how any user can load/save a PyTorch model from/to the Hub:
165164

166165

167166
# Define your Pytorch model exactly the same way you are used to
168-
>>> class MyModel(nn.Module, PyTorchModelHubMixin): # multiple inheritance
167+
>>> class MyModel(
168+
... nn.Module,
169+
... PyTorchModelHubMixin, # multiple inheritance
170+
... library_name="keras-nlp",
171+
... tags=["keras"],
172+
... repo_url="https://github.com/keras-team/keras-nlp",
173+
... docs_url="https://keras.io/keras_nlp/",
174+
... # ^ optional metadata to generate model card
175+
... ):
169176
... def __init__(self, hidden_size: int = 512, vocab_size: int = 30000, output_size: int = 4):
170177
... super().__init__()
171178
... self.param = nn.Parameter(torch.rand(hidden_size, vocab_size))
@@ -191,6 +198,14 @@ Here is how any user can load/save a PyTorch model from/to the Hub:
191198
>>> model = MyModel.from_pretrained("username/my-awesome-model")
192199
>>> model.config
193200
{"hidden_size": 128, "vocab_size": 30000, "output_size": 4}
201+
202+
# Model card has been correctly populated
203+
>>> from huggingface_hub import ModelCard
204+
>>> card = ModelCard.load("username/my-awesome-model")
205+
>>> card.data.tags
206+
["keras", "pytorch_model_hub_mixin", "model_hub_mixin"]
207+
>>> card.data.library_name
208+
"keras-nlp"
194209
```
195210

196211
#### Implementation

src/huggingface_hub/hub_mixin.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,15 @@ class ModelHubMixin:
7272
```python
7373
>>> from huggingface_hub import ModelHubMixin
7474
75-
# Inherit from ModelHubMixin (and optionally from your framework's model class)
76-
>>> class MyCustomModel(ModelHubMixin):
75+
# Inherit from ModelHubMixin
76+
>>> class MyCustomModel(
77+
... ModelHubMixin,
78+
... library_name="my-library",
79+
... tags=["x-custom-tag"],
80+
... repo_url="https://github.com/huggingface/my-cool-library",
81+
... docs_url="https://huggingface.co/docs/my-cool-library",
82+
... # ^ optional metadata to generate model card
83+
... ):
7784
... def __init__(self, size: int = 512, device: str = "cpu"):
7885
... # define how to initialize your model
7986
... super().__init__()
@@ -112,6 +119,14 @@ class ModelHubMixin:
112119
>>> reloaded_model = MyCustomModel.from_pretrained("username/my-awesome-model")
113120
>>> reloaded_model.config
114121
{"size": 256, "device": "gpu"}
122+
123+
# Model card has been correctly populated
124+
>>> from huggingface_hub import ModelCard
125+
>>> card = ModelCard.load("username/my-awesome-model")
126+
>>> card.data.tags
127+
["x-custom-tag", "pytorch_model_hub_mixin", "model_hub_mixin"]
128+
>>> card.data.library_name
129+
"my-library"
115130
```
116131
"""
117132

@@ -532,7 +547,14 @@ class PyTorchModelHubMixin(ModelHubMixin):
532547
>>> import torch.nn as nn
533548
>>> from huggingface_hub import PyTorchModelHubMixin
534549
535-
>>> class MyModel(nn.Module, PyTorchModelHubMixin):
550+
>>> class MyModel(
551+
... nn.Module,
552+
... PyTorchModelHubMixin,
553+
... library_name="keras-nlp",
554+
... repo_url="https://github.com/keras-team/keras-nlp",
555+
... docs_url="https://keras.io/keras_nlp/",
556+
... # ^ optional metadata to generate model card
557+
... ):
536558
... def __init__(self, hidden_size: int = 512, vocab_size: int = 30000, output_size: int = 4):
537559
... super().__init__()
538560
... self.param = nn.Parameter(torch.rand(hidden_size, vocab_size))

0 commit comments

Comments
 (0)