Movatterモバイル変換


[0]ホーム

URL:


Hugging Face's logoHugging Face

Hub documentation

Uploading models

Hub

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

Collaborate on models, datasets and Spaces
Faster examples with accelerated inference
Switch between documentation themes

to get started

Uploading models

To upload models to the Hub, you’ll need to create an account atHugging Face. Models on the Hub areGit-based repositories, which give you versioning, branches, discoverability and sharing features, integration with dozens of libraries, and more! You have control over what you want to upload to your repository, which could include checkpoints, configs, and any other files.

You can link repositories with an individual user, such asosanseviero/fashion_brands_patterns, or with an organization, such asfacebook/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 organizationhere.

NOTE: Models do NOT need to be compatible with the Transformers/Diffusers libraries to get download metrics. Any custom model is supported. Read more below!

There are several ways to upload models for them to be nicely integrated into the Hub and getdownload metrics, described below.

  • In case your model is designed for a library that hasbuilt-in support, you can use the methods provided by the library. Custom models that usetrust_remote_code=True can also leverage these methods.
  • In case your model is a custom PyTorch model, one can leverage thePyTorchModelHubMixin class as it allows to addfrom_pretrained,push_to_hub to anynn.Module class, just like models in the Transformers, Diffusers and Timm libraries.
  • In addition to programmatic uploads, you can always use theweb interface orthe git command line.

Once your model is uploaded, we suggest adding aModel Card to your repo to document your model and make it more discoverable.

drawingExample [repository](https://huggingface.co/LiheYoung/depth_anything_vitl14) that leverages [PyTorchModelHubMixin](#upload-a-pytorch-model-using-huggingfacehub). Downloads are shown on the right.

Using the web interface

To create a brand new model repository, visithuggingface.co/new. Then follow these steps:

  1. In the “Files and versions” tab, select “Add File” and specify “Upload File”:
  1. From there, select a file from your computer to upload and leave a helpful commit message to know what you are uploading:
  1. Afterwards, clickCommit changes to upload your model to the Hub!

  2. Inspect files and history

You can check your repository with all the recently added files!

The UI allows you to explore the model files and commits and to see the diff introduced by each commit:

  1. Add metadata

You can add metadata to your model card. You can specify:

  • the type of task this model is for, enabling widgets and the Inference API.
  • the used library (transformers,spaCy, etc.)
  • the language
  • the dataset
  • metrics
  • license
  • a lot more!

Read more about model tagshere.

  1. Add TensorBoard traces

Any repository that contains TensorBoard traces (filenames that containtfevents) is categorized with theTensorBoard tag. As a convention, we suggest that you save traces under theruns/ subfolder. The “Training metrics” tab then makes it easy to review charts of the logged variables, like the loss or the accuracy.

Models trained with 🤗 Transformers will generateTensorBoard traces by default iftensorboard is installed.

Upload from a library with built-in support

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:

from transformersimport BertConfig, BertModelconfig = BertConfig()model = BertModel(config)model.push_to_hub("nielsr/my-awesome-bert-model")# reloadmodel = BertModel.from_pretrained("nielsr/my-awesome-bert-model")

Some libraries, like Transformers, support loadingcode from the Hub. This is a way to make your model work with Transformers using thetrust_remote_code=True flag. You may want to consider this option instead of a full-fledged library integration.

Upload a PyTorch model using huggingface_hub

In case your model is a (custom) PyTorch model, you can leverage thePyTorchModelHubMixinclass available in thehuggingface_hub Python library. It is a minimal class which addsfrom_pretrained andpush_to_hub capabilities to anynn.Module, along with download metrics.

Here is how to use it (assuming you have runpip install huggingface_hub):

import torchimport torch.nnas nnfrom huggingface_hubimport PyTorchModelHubMixinclassMyModel(    nn.Module,    PyTorchModelHubMixin,# optionally, you can add metadata which gets pushed to the model card    repo_url="your-repo-url",    pipeline_tag="text-to-image",    license="mit",):def__init__(self, num_channels:int, hidden_size:int, num_classes:int):super().__init__()        self.param = nn.Parameter(torch.rand(num_channels, hidden_size))        self.linear = nn.Linear(hidden_size, num_classes)defforward(self, x):return self.linear(x + self.param)# create modelconfig = {"num_channels":3,"hidden_size":32,"num_classes":10}model = MyModel(**config)# save locallymodel.save_pretrained("my-awesome-model")# push to the hubmodel.push_to_hub("your-hf-username/my-awesome-model")# reloadmodel = MyModel.from_pretrained("your-hf-username/my-awesome-model")

As you can see, the only requirement is that your model inherits fromPyTorchModelHubMixin. All instance attributes will be automatically serialized to aconfig.json file. Note that theinit method can only take arguments which are JSON serializable. Python dataclasses are supported.

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:

  • apytorch_model.bin ormodel.safetensors file containing the weights
  • aconfig.json file which is a serialized version of the model configuration. This class is used for counting download metrics: everytime a user callsfrom_pretrained to load aconfig.json, the count goes up by one. Seethis guide regarding automated download metrics.

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.

Visitthe huggingface_hub’s documentation to learn more.

Alternatively, one can also simply programmatically upload files or folders to the hub:https://huggingface.co/docs/huggingface_hub/guides/upload.

Using Git

Finally, since model repos are just Git repositories, you can also use Git to push your model files to the Hub. Follow the guide onGetting Started with Repositories to learn about using thegit CLI to commit and push your models.

Update on GitHub


[8]ページ先頭

©2009-2026 Movatter.jp