What is Hugging Face?
The Hugging Face Hub is a collaboration platform that hosts a huge collection of open-source models and datasets for machine learning — think of it like Github for ML. The hub facilitates sharing and collaboration by making it easy for you to discover, learn, and interact with useful ML assets from the open-source community. The hub integrates with the Transformers library, from which deployed models are downloaded.
Concepts around Hugging Face
Hugging Face Transformers
Hugging Face Transformers is an open-source Python library that provides access to thousands of pre-trained Transformers models for NLP, computer vision, audio tasks, and more. It simplifies the process of implementing Transformer models by abstracting away the complexity of training or deploying models in lower-level ML frameworks like PyTorch, TensorFlow and JAX.
Hugging Face Spaces
Spaces from Hugging Face is a service available on the Hub that provides an easy-to-use GUI for building and deploying web-hosted ML demos and apps. The service allows you to quickly build ML demos, upload your own apps to be hosted, or even select a number of pre-configured ML applications to deploy instantly.
Let’s deploy an ML application on Hugging Face
Step 1: create a Hugging Face account
Go to hf.co, click Sign Up and create an account. Add your billing information: Settings > Billing, add your credit card to the payment information section.
We need an NVIDIA T4 small to run the summarization model in this tutorial.
Step 2: create the space
Once logged in, in the Spaces tab, create a new Streamlit space with NVIDIA T4 small hardware. Choose a sleep time to avoid billing when your application is idle. Make the space public or private. Once the space is created (hello-world in this example), clone it with git.
git clone [email protected]:spaces/doveaia/hello-world
The README.md in the repo contains a YAML header block describing the space:
---
title: Hello World
emoji: 🐢
colorFrom: red
colorTo: gray
sdk: streamlit
sdk_version: 1.41.1
app_file: app.py
pinned: false
---
Key takeaway: app_file: app.py.
Step 3: the application code
import streamlit as st
from transformers import pipeline
st.title("💬 Chatbot")
if "messages" not in st.session_state:
st.session_state["messages"] = [{"role": "assistant", "content": "Hello. I can summarize text."}]
for msg in st.session_state.messages:
st.chat_message(msg["role"]).write(msg["content"])
if prompt := st.chat_input():
summarizer = pipeline("summarization")
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
summary = summarizer(prompt)
msg = summary[0]["summary_text"]
st.session_state.messages.append({"role": "assistant", "content": msg})
st.chat_message("assistant").write(msg)
We use pipeline from the Transformers library to load the summarization model. The Python dependencies are listed in requirements.txt.
Step 4: test the app
To test the application, go to the space URL: https://huggingface.co/spaces/doveaia/hello-world.
Conclusion
Hugging Face has revolutionized the way developers and researchers access and deploy machine learning models. By providing a robust platform for collaboration, easy-to-use tools like the Transformers library, and hosting solutions through Hugging Face Spaces, it has become a cornerstone for the ML community.