03 Mar How to download a model from Hugging Face
Downloading models from Hugging Face is simple and can be done using the Transformers library or directly from the Hugging Face Hub. Below is a step-by-step guide to help you download and use models from Hugging Face.
Step 1: Install the Transformers Library
If you haven’t already installed the transformers library, you can do so using pip. On Google Colab, use the following command to install:
1 2 3 |
!pip install transformers |
Step 2: Download a Model Using the Transformers Library
You can download a model using the from_pretrained method. This method downloads the model weights, configuration, and tokenizer (if applicable) from the Hugging Face Hub.
Example: Download a Pre-Trained BERT Model
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from transformers import AutoModel, AutoTokenizer # Download the model and tokenizer model_name = "bert-base-uncased" model = AutoModel.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) # Use the model and tokenizer inputs = tokenizer("Hello, Hugging Face!", return_tensors="pt") outputs = model(**inputs) print(outputs.last_hidden_state.shape) # Example output shape |
Step 3: Download a Model for a Specific Task
Hugging Face provides task-specific models (e.g., for text classification, question answering, etc.). You can download these models using the appropriate class.
Example: Download a Text Classification Model
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer # Download a model for text classification model_name = "distilbert-base-uncased-finetuned-sst-2-english" model = AutoModelForSequenceClassification.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) # Use the model for inference inputs = tokenizer("I love Hugging Face!", return_tensors="pt") outputs = model(**inputs) print(outputs.logits) # Output logits for classification |
Step 4: Download a Model from the Hugging Face Hub Website
If you prefer to download models manually, you can do so from the Hugging Face Hub website:
- Go to the Hugging Face Hub: https://huggingface.co/models.
- Search for the model you want (e.g., bert-base-uncased).
- Click on the model to open its page.
- Download the model files directly from the “Files” tab.
Step 5: Use a Downloaded Model Locally
If you’ve downloaded a model manually, you can load it from a local directory:
1 2 3 4 5 6 7 |
from transformers import AutoModel, AutoTokenizer # Load the model and tokenizer from a local directory model = AutoModel.from_pretrained("path/to/local/model") tokenizer = AutoTokenizer.from_pretrained("path/to/local/tokenizer") |
Step 6: Download a Model with Custom Configurations
Some models have multiple configurations or variants. You can specify the configuration when downloading the model.
Example: Download a Multilingual BERT Model
1 2 3 4 5 6 7 8 |
from transformers import AutoModel, AutoTokenizer # Download a multilingual BERT model model_name = "bert-base-multilingual-cased" model = AutoModel.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) |
Step 7: Download a Model with a Specific Framework
You can specify the framework (PyTorch, TensorFlow, or JAX) when downloading a model.
Example: Download a TensorFlow Model
1 2 3 4 5 6 7 8 |
from transformers import TFAutoModel, AutoTokenizer # Download a TensorFlow model model_name = "bert-base-uncased" model = TFAutoModel.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) |
Step 8: Use the Model for Inference
Once the model is downloaded, you can use it for inference. Here’s an example of using a text classification model:
1 2 3 4 5 6 7 8 9 10 |
from transformers import pipeline # Load a text classification pipeline classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english") # Perform inference result = classifier("I love Hugging Face!") print(result) |
Step 9: Save a Downloaded Model Locally
You can save a downloaded model and tokenizer to a local directory for future use:
1 2 3 4 5 |
# Save the model and tokenizer model.save_pretrained("path/to/save/model") tokenizer.save_pretrained("path/to/save/tokenizer") |
To load the saved model later:
1 2 3 4 5 6 7 |
from transformers import AutoModel, AutoTokenizer # Load the model and tokenizer from a local directory model = AutoModel.from_pretrained("path/to/save/model") tokenizer = AutoTokenizer.from_pretrained("path/to/save/tokenizer") |
We used the following commands above:
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
- RAG Tutorial
- Generative AI Tutorial
- Machine Learning Tutorial
- Deep Learning Tutorial
- Ollama Tutorial
- Retrieval Augmented Generation (RAG) Tutorial
- Copilot Tutorial
- ChatGPT Tutorial
No Comments