- Published on
Using LLMs with Hugging Face
544 words3 min read–––
Views
A simple guide for using pre-trained language models with Hugging Face Transformers:
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
# 1. Basic text generation with a small model
model_name = "distilgpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Generate text
prompt = "The future of AI is"
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(
inputs.input_ids,
max_length=50,
num_return_sequences=1,
temperature=0.7,
top_p=0.9,
do_sample=True
)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(generated_text)
# 2. Using a pipeline for simpler interface
generator = pipeline('text-generation', model=model_name)
result = generator(prompt, max_length=50, num_return_sequences=1)
print(result[0]['generated_text'])
# 3. Using a different task - sentiment analysis
sentiment_analyzer = pipeline('sentiment-analysis')
result = sentiment_analyzer("I really enjoyed this movie!")
print(result) # [{'label': 'POSITIVE', 'score': 0.9998}]
# 4. For more powerful models (with appropriate hardware)
# Uncomment and use as needed:
'''
# For models requiring more memory, use 8-bit quantization
from transformers import BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(
load_in_8bit=True,
llm_int8_threshold=6.0
)
# Load a larger model in 8-bit precision
model_name = "meta-llama/Llama-2-7b-chat-hf" # Requires access permissions
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
device_map="auto",
quantization_config=quantization_config
)
# Generate with the larger model
prompt = "Write a short poem about coding"
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(
inputs.input_ids,
max_length=200,
temperature=0.7,
do_sample=True
)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(generated_text)
'''
Installation:
pip install transformers torch
# For advanced features:
pip install accelerate bitsandbytes
This snippet demonstrates how to use Hugging Face Transformers for basic text generation, sentiment analysis, and working with larger models. The library provides access to thousands of pre-trained models for various NLP tasks.