If you've been wondering where to find a model or dataset that's perfect for your tasks, Hugging Face is the perfect solution! Hugging Face is one of the most popular platforms among AI practitioners.
In this topic, you will get familiar with Hugging Face and learn about its ecosystem.
The Hugging Face Hub overview
The Hugging Face Hub is a platform that provides access to pre-trained ML models, datasets, metrics, demo apps, and other artifacts. You can download models or dump your own pre-trained or fine-tuned models, and also, you can (serverless-ly) interact with the models via an API. Models and datasets on HF can be private or public. Private models/datasets are suitable if you work on some project and want to save intermediate results, or if you wish to share your model with only a few people. HF offers documentation and several open courses on various subjects, such as NLP, Deep RL and ML for 3D.
The Hugging Face Hub has several advantages for practitioners working with ML models:
The Hugging Face Hub supports version control, allowing users to manage different versions of resources (such as models and datasets). This helps with collaboration among researchers and reproducibility of experiments.
The Hugging Face Hub integrates with the Transformers library, making it straightforward to download and use models within code. The library provides a unified API to load and utilize models from the Hub, simplifying the development and deployment process.
Hugging Face has a large community of ML enthusiasts, researchers, and developers who actively contribute to the Hub. The community provides support, shares resources, and collaborates on improving existing models and developing new ones.
The Hugging Face Hub is an ecosystem for model development, sharing, and collaboration, making it easier for practitioners to access state-of-the-art models, datasets, and evaluation metrics while fostering a community-driven approach to advancements in AI.
Let's look at what you can find on Hugging Face!
Hugging Face's content
The Hugging Face Hub contains a lot of tools for ML practitioners. You can find and filter the models by many parameters. There are models for text, audio, images, videos, and structured data processing, so you can choose the most suitable model for your tasks. Here is a brief overview:
Datasets. The HF Hub contains many datasets suitable for model training, which can also be filtered by parameters.
Apps. This site allows models to be used as attachments, which can be handy for presenting them. Ready apps can be found in the Spaces section.
Libraries for using transformers. The Transformers library may be used for fine-tuning models as well as for using ready-to-go NLP models. The Accelerate library allows to make PyTorch code run on different hardware setups and configurations and supports several training optimizations to make the code run faster.
Metrics. You can evaluate the quality of the model you trained using the
evaluatemodule from the transformers library. This module contains the most popular metrics for evaluating the quality of various models.HF Serverless Inference API. A lot of models on HF have an inference endpoint which you can you for prototyping without having to download the models locally.
The following diagram shows the majority of the services Hugging Face provides:
So, you already know a lot about the functionality of Hugging Face. Now it's time to join this community!
Log in and connect with IDE
Let's create an account on Hugging Face and connect it with IDE (this is needed to tinker with models locally). For this example, we will link the account to Jupyter Notebook. First, you need to create an account on HF.
After registering and signing in, click on the top right icon (your account icon) and choose "Access tokens". You will see a page with the list of your tokens. A token is like a password, and it is confidential. Generally, each token is created for an individual task, but you can use one for all. Click on "New token" and pick a name for it and the token's role:
read — if you are going to load a model only,
write — if you want to upload your model as well,
fine-grained — where you can combine various rights.
To log in to HF via Jupyter Notebook, use the following code:
from huggingface_hub import notebook_login
notebook_login()Enter your token in the blank space. If the token is correct, you will see the message:
Token is valid.
Your token has been saved in your configured git credential helpers…
Your token has been saved to …
Login successfulUsing the HF Inference API
Alternatively, you can access the HF Inference API with a token to interact with the hosted models without downloading them. As an example, lets call the QwQ-32B-Preview model for chat completion (you can access this via the 'Deploy' drop down menu on the model card):
Note that here we paste the HF token in the code itself for demo purposes, but this should be avoided in shared code. Use .env files for secure key management instead.
from huggingface_hub import InferenceClient
client = InferenceClient(api_key="<Your HF token here>")
messages = [
{
"role": "user",
"content": "Could you explain gradient descent?"
}
]
completion = client.chat.completions.create(
model="Qwen/QwQ-32B-Preview",
messages=messages,
max_tokens=500
)
print(completion.choices[0].message)Which will output something similar to:
ChatCompletionOutputMessage(role='assistant', content="Sure, I'll try to explain gradient descent in a way that's easy to understand. So, gradient descent is an optimization algorithm ...", tool_calls=None)Currently, the API is limited to 1000 requests per day for the non-paying users, so it's suitable for personal projects and quick prototyping.
Conclusion
In this topic, you have learned:
About The Hugging Face Hub and its advantages;
The Hugging Face Hub stores models and datasets and has a rich ecosystem for ML-related tasks;
How to connect your Jupyter Notebooks with The Hugging Face Hub and how to access the serverless Inference API.