How to publish model on Huggingface ??

Shashi Vishwakarma
3 min readOct 20, 2023

If you are new to Huggingface and wants to know how can you publish your model to huggingface , so you are at right place. We will learn today how can you publish your very first fine tuned model to huggingface.

Photo by Fahmi Fakhrudin on Unsplash

Pre-requisite : Make sure you have huggingface account to publish your mode.

I am going to use Google Collab to run all code shown in blog but you can have your own local setup to run.

Lets install required libraries for project.

! pip install huggingface_hub
! pip install transformers
! pip install sentencepiece

Now, in order to publish a model on Hugging Face, I’ll begin by reusing an existing model available on Hugging Face and then proceed with the publishing process.

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# Download Sample Model from Hugging Face to Publish Again
tokenizer = AutoTokenizer.from_pretrained("ShashiVish/t5-small-fine-tune-cover-letter")
model = AutoModelForSeq2SeqLM.from_pretrained("ShashiVish/t5-small-fine-tune-cover-letter")

Lets save this model locally and will upload locally save folder directly into huggingface.

# Local Path of Model 
model_path = 't5-fine-tune-save-example'
model.save_pretrained(model_path)

You should see a folder getting created on your local system something like below.

Perfect . Now its time to login into huggingface account using your jupyter notebook and upload your model.

#Logging to HuggingFace
from huggingface_hub import login
login()

It will prompt you for Token to after running above code. You can get token from your huggingface Profile > Setting > Access Token. If you had not created token earlier , create new token with write access.

After successful login , we just need final step where we will specify model repository name to be created on huggingface and publish your very first model.

from huggingface_hub import HfApi

api = HfApi()
model_repo_name = "<Your Profile Name>/t5-fine-tune-save-example" # Format of Input <Profile Name > / <Model Repo Name>

#Create Repo in Hugging Face
api.create_repo(repo_id=model_repo_name)

#Upload Model folder from Local to HuggingFace
api.upload_folder(
folder_path=model_path,
repo_id=model_repo_name
)

# Publish Model Tokenizer on Hugging Face
tokenizer.push_to_hub(model_repo_name)

Congratulations. You have successfully published your very first model on Huggingface.

Keep Learning …!! Keep Sharing ..!!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Shashi Vishwakarma
Shashi Vishwakarma

Written by Shashi Vishwakarma

Senior Software/AI Engineer , Technical Writer

Responses (3)

Write a response

it worked . appreciated it.

1

Exactly what i was looking for, thanks.

1

Great article!

1