A comprehensive project for working with Hugging Face Transformers library, including sentiment analysis, zero-shot classification, speech recognition, tokenization, and model loading capabilities.
- Prerequisites
- Quick Start
- Installation Methods
- Project Structure
- Usage
- Features
- Development
- Troubleshooting
- Python: 3.11 or higher
- Operating System: Linux (Ubuntu/Debian recommended), macOS, or Windows
- Docker (optional, for containerized deployment): Docker 20.10+
- Disk Space: At least 5GB free (for models and dependencies)
For audio processing (speech recognition):
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y ffmpeg
# macOS
brew install ffmpeg
# Windows
# Download from https://ffmpeg.org/download.html# Build and run with auto-reload
docker-compose build
docker-compose up
# Or use the helper script
./scripts/run-dev.sh# Run installation script
./scripts/install.sh
# Or manually
python3 -m venv venv
source venv/bin/activate
pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install -r requirements.txt
python src/main.pyFor detailed instructions, see INSTALL.md.
cd /path/to/transformerspython3 -m venv venv# Linux/macOS
source venv/bin/activate
# Windows
venv\Scripts\activate# Upgrade pip
pip install --upgrade pip
# Install PyTorch (CPU version - smaller download)
pip install torch --index-url https://download.pytorch.org/whl/cpu
# Install other dependencies
pip install -r requirements.txtpython -c "from transformers import pipeline; print('✓ Transformers installed successfully!')"
python -c "import torch; print(f'✓ PyTorch version: {torch.__version__}')"python src/main.pyDocker provides an isolated environment with all dependencies pre-configured.
Ubuntu/Debian:
# Install Docker via snap (easiest)
sudo snap install docker
# Or install via apt
sudo apt-get update
sudo apt-get install -y docker.io docker-composemacOS:
# Install Docker Desktop from https://www.docker.com/products/docker-desktopWindows:
# Install Docker Desktop from https://www.docker.com/products/docker-desktopIf using snap Docker:
# Enable removable-media access (if project is on external drive)
sudo snap connect docker:removable-media
# Fix socket permissions (if needed)
sudo chmod 666 /var/run/docker.sockOr add user to docker group:
sudo groupadd docker
sudo usermod -aG docker $USER
# Log out and log back in for changes to take effectcd /path/to/transformersdocker-compose buildThis will:
- Download Python 3.11 base image
- Install system dependencies (ffmpeg)
- Install PyTorch (CPU version)
- Install transformers and other Python packages
- Set up the working environment
Expected output:
[+] Building ...
[+] Building X.Xs (Y/Y) FINISHED
docker-compose upTo run in detached mode (background):
docker-compose up -dTo view logs:
docker-compose logs -fdocker-compose downtransformers/
├── src/ # Source code
│ ├── __init__.py # Package initialization
│ ├── main.py # Main entry point
│ └── utils.py # Utility functions for NLP tasks
├── scripts/ # Helper scripts
│ ├── install.sh # Automated installation
│ ├── run-dev.sh # Development mode runner
│ └── run-prod.sh # Production mode runner
├── data/ # Data files
│ └── mlk.flac # Sample audio file
├── models/ # Cached Hugging Face models (gitignored)
├── test/ # Test files
│ └── test_main.py # Unit tests
├── Dockerfile # Docker image configuration
├── docker-compose.yml # Docker Compose (production)
├── docker-compose.dev.yml # Docker Compose (development)
├── watch.py # File watcher for auto-reload
├── requirements.txt # Python dependencies
├── README.md # Main documentation
├── INSTALL.md # Installation guide
├── AUTO_RELOAD.md # Auto-reload documentation
└── PROJECT_STRUCTURE.md # Project structure details
For detailed structure information, see PROJECT_STRUCTURE.md.
Edit src/main.py to uncomment and use different functions:
import utils
# Sentiment Analysis
utils.sentiment_analysis([
"I am feeling very happy",
"I am feeling very sad"
])
# Zero-shot Classification
utils.zero_shot_classification("I am feeling very hopeless about my life")
# Speech Recognition (local file)
utils.speech_recognition("data/mlk.flac")
# Speech Recognition (from URL)
utils.speech_recognition("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac")
# Fill-mask (find masking words)
utils.find_masking_words()
# Tokenization
utils.tokenize_text("I am feeling very hopeless")
utils.tokenize_text_batch(["I am feeling very hopeless", "I am feeling very hopeful"])
# Load Model
model = utils.load_model("distilbert-base-uncased-finetuned-sst-2-english")
model, tokenizer = utils.load_model_and_tokenizer()Analyze the sentiment (positive/negative) of text using pre-trained models.
Classify text into custom categories without training.
Transcribe audio files to text using Whisper models.
Predict masked words in sentences using BERT models.
Tokenize text for model input using AutoTokenizer.
Load pre-trained models and tokenizers using AutoModel and AutoTokenizer.
The project includes automatic reload functionality when running in Docker. Any changes to Python files in src/ will automatically restart the application.
# Development mode with auto-reload (default)
docker-compose up
# Or use helper script
./scripts/run-dev.sh
# Production mode (no auto-reload)
./scripts/run-prod.shFor more details, see AUTO_RELOAD.md.
# Activate virtual environment
source venv/bin/activate
# Run tests
python -m pytest test/The project follows PEP 8 style guidelines. Consider using:
blackfor code formattingflake8for lintingmypyfor type checking
Solution:
# Make sure virtual environment is activated
source venv/bin/activate
# Install dependencies
pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install -r requirements.txtSolution:
- Ensure the file path is correct relative to project root
- For URLs, the function will download automatically
- Check file permissions
Solution:
# Fix Docker socket permissions
sudo chmod 666 /var/run/docker.sock
# Or use sudo
sudo docker-compose upSolution:
# Ubuntu/Debian
sudo apt-get install ffmpeg
# macOS
brew install ffmpegSolution:
- Check internet connection
- Verify Docker is running:
docker ps - Try pulling base image manually:
docker pull python:3.11-slim
Solution:
- Reduce
shm_sizeindocker-compose.yml(currently 16gb) - Use smaller models
- Process data in batches
Solution:
- Models are cached in
./modelsdirectory - First download may take time
- Subsequent runs use cached models
- Install NVIDIA Docker support:
# Ubuntu
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker- Update
Dockerfile:
# Replace CPU PyTorch with CUDA version
RUN pip3 install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121- Update
docker-compose.yml:
runtime: nvidia
environment:
- NVIDIA_VISIBLE_DEVICES=all- Install ROCm drivers on host system
- Update
Dockerfileto use ROCm PyTorch - Uncomment AMD GPU section in
docker-compose.yml
TRANSFORMERS_CACHE: Directory for caching models (default:/modelsin Docker)HF_HOME: Hugging Face home directory (default:/modelsin Docker)
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project uses the Hugging Face Transformers library, which is licensed under Apache 2.0.
For issues and questions:
- Check the Troubleshooting section
- Review Hugging Face Transformers documentation
- Open an issue on the project repository