The Hugging Face Hub is the central place to discover and share ML models, datasets, and Spaces.
Start by installing the huggingface_hub library:
pip install --upgrade huggingface_hub
For optional features, install extra dependencies like:
pip install 'huggingface_hub[tensorflow]'
Use hf_hub_download() to download a specific file from a repo:
from huggingface_hub import hf_hub_download
hf_hub_download(repo_id="google/pegasus-xsum", filename="config.json")
For an entire repo, use snapshot_download():
from huggingface_hub import snapshot_download
snapshot_download(repo_id="lysandre/arxiv-nlp")
You'll need a Hugging Face account and a User Access Token for many actions.
huggingface-cli login
HF_TOKEN
.Use create_repo() to create a new repository:
from huggingface_hub import HfApi
api = HfApi()
api.create_repo(repo_id="my-new-model")
Set private=True
for a private repo.
Use upload_file() for a single file:
api.upload_file(
path_or_fileobj="/path/to/README.md",
path_in_repo="README.md",
repo_id="my-username/my-repo"
)
For a folder, use upload_folder().
Run accelerated inference on Hugging Face servers with InferenceClient:
from huggingface_hub import InferenceClient
client = InferenceClient()
# Text to Image
image = client.text_to_image("A cat wearing a hat")
image.save("cat_with_hat.png")
# Chat Completion
messages = [{"role": "user", "content": "Translate 'Hello' to Spanish."}]
response = client.chat_completion(messages, model="google/flan-t5-xl")
print(response.choices[0].message.content) # ยกHola!
Use the HfApi to manage your repositories:
Interact with the community using HfApi:
Organize models, datasets, Spaces, and papers with collections.
The huggingface_hub library caches downloaded files to speed up future access.
Create informative Model Cards to describe your models.
huggingface_hub.login(token, add_to_git_credential=False, new_session=True, write_permission=False)
.huggingface_hub.interpreter_login(new_session=True, write_permission=False)
.huggingface_hub.notebook_login(new_session=True, write_permission=False)
.huggingface_hub.logout()
.HF_HOME
.HF_HUB_CACHE
.HF_ASSETS_CACHE
.HF_TOKEN
.HF_HUB_OFFLINE
.HF_HUB_DISABLE_PROGRESS_BARS
.HF_HUB_DISABLE_TELEMETRY
.HfApi
Client ๐huggingface_hub.HfApi(endpoint=None, token=None, ...)
.api.create_repo(repo_id, token=None, private=False, repo_type=None, exist_ok=False, ...)
.api.delete_repo(repo_id, token=None, repo_type=None, missing_ok=False)
.api.upload_file(path_or_fileobj, path_in_repo, repo_id, token=None, repo_type=None, revision=None, ...)
.api.upload_folder(folder_path, path_in_repo=None, repo_id, token=None, repo_type=None, revision=None, ...)
.api.hf_hub_download(repo_id, filename, revision=None, cache_dir=None, force_download=False, ...)
.api.list_models(filter=None, author=None, search=None, sort=None, direction=None, limit=None, ...)
.api.list_datasets(filter=None, author=None, search=None, sort=None, direction=None, limit=None, ...)
.api.model_info(repo_id, revision=None, timeout=None, securityStatus=None, files_metadata=False, ...)
.api.dataset_info(repo_id, revision=None, timeout=None, files_metadata=False, ...)
.The InferenceClient object connects to inference services. ๐
model
: Model ID (e.g., "meta-llama/Meta-Llama-3-8B-Instruct"
) or Inference Endpoint URL. ๐token
: Hugging Face authentication token. ๐timeout
: Maximum wait time for server response. โฑ๏ธAn asynchronous version using asyncio and aiohttp. ๐ซ
pip install --upgrade huggingface_hub[inference]
await
. โณaudio_classification(audio, model)
: Classifies audio content into predefined categories. ๐ง
audio
: The audio content to classify (file path, bytes, URL).model
: The audio classification model to use.automatic_speech_recognition(audio, model)
: Transcribes audio to text (ASR). ๐ค
audio
: The audio content to transcribe (file path, bytes, URL).model
: The ASR model to use.chat_completion(messages, model, ...)
: Generates conversational responses in a chat-like format. ๐ฌ
messages
: A list of chat messages with roles (user, assistant, system).model
: The conversational model to use.max_tokens
, temperature
, etc.feature_extraction(text, model)
: Generates numerical representations (embeddings) of text. ๐ข
text
: The text to embed.model
: The text embedding model to use.image_classification(image, model)
: Classifies images into predefined categories. ๐ผ๏ธ
image
: The image to classify (file path, bytes, URL).model
: The image classification model to use.text_generation(prompt, model, ...)
: Generates text based on a given prompt. โ๏ธ
prompt
: The starting text for the generation.model
: The text generation model to use.max_new_tokens
, temperature
, etc.text_to_image(prompt, model, ...)
: Generates images from text descriptions. ๐จ
prompt
: The text description of the image to generate.model
: The text-to-image model to use.height
, width
, etc.translation(text, model, src_lang, tgt_lang)
: Translates text from one language to another. ๐
text
: The text to translate.model
: The translation model to use.src_lang
: The source language (optional).tgt_lang
: The target language (optional).from huggingface_hub import InferenceClient
Initialize the InferenceClient
client = InferenceClient(token="YOUR_HUGGING_FACE_TOKEN")
Generate an image
image = client.text_to_image(
prompt="A cat wearing a top hat riding a unicycle on a tightrope",
model="stabilityai/stable-diffusion-2-1", # Specify the text-to-image model
height=512, # Optional: Set image height
width=512, # Optional: Set image width
)
Save the image
image.save("cat_unicycle.png")
from huggingface_hub import InferenceClient
Initialize the InferenceClient
client = InferenceClient(token="YOUR_HUGGING_FACE_TOKEN")
Generate text
generated_text = client.text_generation(
prompt="Once upon a time, in a land far away, ",
model="gpt2", # Specify the text generation model
max_new_tokens=50, # Limit the length of generated text
temperature=0.7, # Control the randomness (higher = more random)
)
Print the generated text
print(generated_text)
Tokenizer.encode
.pip install tokenizers
.git clone
and compile.pip install transformers datasets evaluate accelerate
pipeline(task="task-name")
pipeline(task="sentiment-analysis")
pipeline(task="text-generation")
pipeline(task="summarization")
pipeline(task="image-classification")
pipeline(task="image-segmentation")
pipeline(task="object-detection")
pipeline(task="audio-classification")
pipeline(task="automatic-speech-recognition")
pipeline(task="vqa")
pip install transformers
pip install 'transformers[torch]'
pip install git+https://github.com/huggingface/transformers