How to use Huggingface models with Python locally

Huggingface models in local Python environment

You can use Hugging Face models locally in Python with the transformers library. Here’s how:

1. Install Required Libraries

Make sure you have the required packages installed:

bash
pip install transformers torch

If you plan to use TensorFlow, install it as well:

bash
pip install tensorflow

2. Load a Model Locally

Hugging Face models can be used for tasks like text generation, classification, and translation.

a) Load a Pretrained Model

python
from transformers import pipeline # Load a text generation model generator = pipeline("text-generation", model="gpt2") # Generate text output = generator("Once upon a time", max_length=50) print(output)

b) Download and Load a Model Manually

You can manually download the model for offline use.

python
from transformers import AutoModelForCausalLM, AutoTokenizer model_name = "gpt2" # Download model and tokenizer tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name) # Save locally model.save_pretrained("./models/gpt2") tokenizer.save_pretrained("./models/gpt2") # Load from local directory local_model = AutoModelForCausalLM.from_pretrained("./models/gpt2") local_tokenizer = AutoTokenizer.from_pretrained("./models/gpt2")

c) Inference with Tokenizer

python
text = "Hello, how are you?" inputs = tokenizer(text, return_tensors="pt") outputs = local_model.generate(**inputs) generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) print(generated_text)

3. Running the Model on GPU

If you have a GPU, you can use it for faster inference.

python
import torch device = "cuda" if torch.cuda.is_available() else "cpu" model.to(device) inputs = tokenizer("Hello, world!", return_tensors="pt").to(device) outputs = model.generate(**inputs) print(tokenizer.decode(outputs[0], skip_special_tokens=True))

Let me know if you need help with a specific use case! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *