How to use Hugging Face models in your project.

Shubhendu ghosh
4 min readMay 7, 2023

--

image credit : https://medium.com/@s_eschweiler

Hugging Face is a library where you can find machine learning models for your project. It consists of numerous transformers models that are used in Natural language processing. You can also find computer vision and audio models there. In this article we will see how you can use them in your project.

https://tenor.com/search/work-boss-gifs

If you are building a machine learning project or adding a new feature to your existing product and you don’t want to train your own model then Hugging Face has got your back. Hugging Face has a lot of pretrained machine learning or deep learning models that you can use in your project or product. Models ranges from natural language processing task to computer vision all the way to reinforcement learning. In this article i will share with you a step by step guide on how to use a hugging Face model in your project. So let’s start.

Step 1. Understand your requirement.

https://giphy.com/explore/what-do-you-want

The first question is what is your purpose to use an AI model. Is it text-to-speech? is it object detection? or is it text generation. Hugging Face has a model for almost everything. So you need to determine what is it that you want to accomplish. Once you are solid confirmed with that we can move on to the next step.

Step 2. Choose the model

Go to Hugging Face models section. Here you will be presented with lots of pretrained models for various tasks. Here is a sneak peak…

I had my reading model on

Let’s say your task is language translation. Here in the models section you click on the translation tag to get the specific models that were trained for translation.

only choosed tag will be highlighted.

At the right side you will get the models that you can use. Now click on each model to check if the model meets your requirements.

step 3. Inspect the model

click on any model to know more about it. I clicked on t5-base.

go to https://huggingface.co/t5-base

You can also train or fine tune this model to your ease. On the right side you can see that one box that says </> Use in Transformer which is very important. We will talk about this in the following steps.

If you scroll down you can see more information about this. How its trained, what are its uses, what are the data that’s been used to train the model and much more.

step 4. See how other people used it

https://media.tenor.com/AKS0zwKDvMcAAAAd/mr-bean-exams.gif

Scroll down a bit and on the right side you will see something like this

There are lot of people/company using the model already for their own purpose. You would definitely want to see how they used it in their context. It can help you recognize the power of the model and if it is usefull for you or not. I strongly recommend this. Click on the boxes and read the code and if anything else is there.

This might look like copying but if your learning or just want to know the potential of the model then it’s fine. You will build your product/feature unique anyway. There is no wrong in this.

Okay we are ready for the next step..

step 5. learn about Pipeline

So apparantly hugging Face provides a function called pipeline. Through pipeline you can access almost every model, you don’t need to download each model separately.

As i told before there is one box as </> Use in Transformer. This box will have the code to use this model in transformer pipeline.

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("t5-base")

model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")

Now if you want to use this in your project in pipeline function then the code is

def translate(text, src_lang, tgt_lang):
translator = pipeline(
"translation",
model=model,
tokenizer=tokenizer,
src_lang=src_lang,
tgt_lang=tgt_lang,
)
translated_text = translator(text)
return translated_text

Now this particular code may or may not work for you but you get the idea.

You can read the documentation to impliment the model.

Here are some usefull resources that you can read to know more.

Follow me for more of such content

Twitter, Linkedin, Github

Bye!

BYE!
https://www.icegif.com/wp-content/uploads/baby-yoda-bye-bye-icegif.gif

--

--