A text generation implementation using Google's FLAN-T5-small model via Hugging Face Transformers for text completion and basic chatbot functionality.
This project contains a Jupyter notebook (SimpleTextGeneration.ipynb) that demonstrates text generation capabilities using a pre-trained FLAN-T5 model. The notebook shows basic text completion and simple chatbot interactions.
- PyTorch: Deep learning framework for tensor operations and model handling
- Hugging Face Transformers: Provides pre-trained models and tokenizers
- FLAN-T5-small: Google's instruction-tuned T5 model for text-to-text generation
- AutoModelForSeq2SeqLM: Hugging Face's automatic model loading for sequence-to-sequence tasks
- AutoTokenizer: Automatic tokenizer selection matching the model
- Pipeline: High-level interface for text2text-generation tasks
- MPS Backend: Utilizes Apple Metal Performance Shaders for GPU acceleration (mps:0)
- torch: PyTorch core library
- torchvision: Computer vision utilities (imported but not actively used)
- matplotlib: Plotting library (imported but not actively used)
- Model:
google/flan-t5-small - Type: Sequence-to-sequence (text-to-text) generation
- Architecture: T5 (Text-To-Text Transfer Transformer)
- Size: Small variant (efficient for basic tasks)
- Instruction-tuned: Optimized for following natural language instructions
prompt = "Complete this sentence: 'Taylor Swift is a'"
response = generator(prompt, max_length=50)
# Output: "singer"chatbot_prompt = "You are a friendly AI assistant. Answer the user's question with a helpful response."
messages = [{"role": "user", "content": "Tell me a fact about the Sun."}]
# Output: "the sun is a source of heat"messages = [{"role": "user", "content": "Tell me a fact about the Taylor Swift."}]
# Output: "Taylor Swift is an American singer."pip install torch torchvision transformers matplotlib- Open the Jupyter notebook:
jupyter notebook SimpleTextGeneration.ipynb- Run the cells sequentially to:
- Load the FLAN-T5-small model and tokenizer
- Create a text generation pipeline
- Test text completion functionality
- Demonstrate basic chatbot capabilities
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
model_name = "google/flan-t5-small"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer)prompt = "Your prompt here"
response = generator(prompt, max_length=50)
print(response[0]["generated_text"])chatbot_prompt = "You are a friendly AI assistant. Answer the user's question with a helpful response."
user_question = "Your question here"
response = generator(f"{chatbot_prompt} {user_question}", max_length=50)
print(response[0]["generated_text"])- max_length: Maximum length of generated text (set to 50)
- max_new_tokens: Default 256 tokens (takes precedence over max_length)
- device: Automatically uses MPS (Metal Performance Shaders) on compatible Apple devices
- MPS Support: Utilizes Apple Silicon GPU when available
- Device: Automatically detected and set to
mps:0
- Automatic Download: Model and tokenizer downloaded from Hugging Face Hub
- Local Caching: Models cached locally after first download
The notebook demonstrates these specific interactions:
-
Text Completion:
- Input: "Complete this sentence: 'Taylor Swift is a'"
- Output: "singer"
-
Sun Fact Query:
- Input: "Tell me a fact about the Sun."
- Output: "the sun is a source of heat"
-
Taylor Swift Fact Query:
- Input: "Tell me a fact about the Taylor Swift."
- Output: "Taylor Swift is an American singer."
- The notebook shows warnings about conflicting
max_new_tokensandmax_lengthparameters max_new_tokenstakes precedence when both are specified
- Uses Apple's MPS for GPU acceleration on compatible hardware
- Small model variant ensures fast inference times
- Efficient memory usage suitable for local development
The current implementation can be extended for:
- Longer Conversations: Maintaining conversation context
- Custom Prompting: Different instruction templates
- Batch Processing: Multiple text generation requests
- Fine-tuning: Adapting the model for specific domains
- Interactive Interface: Building a web-based chatbot
- Clone this repository
- Install required dependencies
- Open and run the SimpleTextGeneration.ipynb notebook
- The model will automatically download on first run
- Experiment with different prompts and questions
- Short Responses: FLAN-T5-small generates concise answers
- Context Window: Limited input length for prompts
- Domain Knowledge: Based on training data cutoff
- Instruction Following: Works best with clear, direct instructions