Prepare Python environment to download Hugging Face models

This guide will help you install the necessary Hugging Face packages and tools to download and use models from the Hugging Face Hub.

Follow this guide, or return to the main page if needed.


1. Install transformers Package

The transformers package is used to load and use Hugging Face models in Python.

Installation

Run the following command in your terminal or command prompt:

bash pip install transformers

Verify Installation

To confirm the installation was successful, run:

bash python -c "from transformers import pipeline; print('Installation successful!')"


2. Install huggingface_hub Package

The huggingface_hub package provides the huggingface-cli tool for interacting with the Hugging Face Hub (e.g., downloading models, uploading files, etc.).

Installation

Run the following command:

bash pip install huggingface_hub

Verify Installation

After installation, check if the huggingface-cli is available:

bash huggingface-cli --help


3. Using huggingface-cli

The huggingface-cli tool allows you to interact with the Hugging Face Hub directly from the command line.

Common Commands

Log in to Hugging Face

To log in to your Hugging Face account:

bash huggingface-cli login

Download a Model

To download a model (e.g., gpt2):

bash huggingface-cli download gpt2

List Available Commands

To see all available commands and options:

bash huggingface-cli --help


4. Example: Download and Use a Model

Here’s an example of downloading and using a model in Python:

from transformers import pipeline

# Download and load a model
generator = pipeline("text-generation", model="gpt2")

# Generate text
output = generator("Hello, how are you?", max_length=50)
print(output)

5. Summary of Commands

Command Description
pip install transformers Install the transformers package.
pip install huggingface_hub Install the huggingface_hub package.
huggingface-cli --help List all available huggingface-cli commands.
huggingface-cli login Log in to your Hugging Face account.
huggingface-cli download gpt2 Download the gpt2 model.

Now you’re ready to use Hugging Face models and tools in Python! 🚀

Proceed to next step

Proceed to next step.