A production-ready FastAPI application that uses zero-shot classification with the Facebook BART-large-MNLI model to categorize YouTube channel descriptions into predefined categories.
# Build and run with Docker Compose
docker-compose up --build
# Or build and run standalone
docker build -t category-classifier .
docker run -p 5680:5680 category-classifier# Install dependencies
pip install -r requirements.txt
# Run the application
uvicorn classify:app --host 0.0.0.0 --port 5680- Overview
- Architecture
- Features
- API Documentation
- Installation
- Usage
- Model Information
- Logging
- Deployment
- Performance
- Contributing
- License
This application provides a RESTful API for automatic categorization of YouTube channel descriptions using state-of-the-art zero-shot learning. Instead of training a model on specific categories, it leverages the BART-large-MNLI model's understanding of language to classify text into any set of predefined categories.
- YouTube channel categorization
- Content recommendation systems
- Content moderation
- Market research and analysis
- Content discovery platforms
graph TB
A[Client Request] --> B[FastAPI Application]
B --> C[Request Validation]
C --> D[Logging Middleware]
D --> E[Zero-Shot Classifier]
E --> F[BART-large-MNLI Model]
F --> G[Category Prediction]
G --> H[Response Generation]
H --> I[Logging]
I --> J[Client Response]
K[Docker Container] --> B
L[Model Files] --> F
M[Log Volume] --> I
style A fill:#e1f5fe
style F fill:#fff3e0
style K fill:#f3e5f5
style L fill:#e8f5e8
- Web Framework: FastAPI (Python 3.9+)
- ML Framework: Transformers (Hugging Face)
- Model: Facebook BART-large-MNLI
- Containerization: Docker & Docker Compose
- ASGI Server: Uvicorn
- Validation: Pydantic
- Zero-Shot Learning: Classify into any category without retraining
- Production Ready: Docker containerization with proper logging
- RESTful API: Clean, documented endpoints
- Comprehensive Logging: Request/response logging with timestamps
- Error Handling: Graceful error responses
- Auto-Documentation: Swagger UI and ReDoc available
- Volume Persistence: Persistent log storage across container restarts
http://localhost:5680
Classify a channel description into one of the predefined categories.
Request Body:
{
"description": "string"
}Response:
{
"description": "string",
"predicted_category": "string"
}Example Request:
curl -X POST "http://localhost:5680/classify/" \
-H "Content-Type: application/json" \
-d '{"description": "We share educational videos about programming and technology"}'Example Response:
{
"description": "We share educational videos about programming and technology",
"predicted_category": "Technology"
}- Swagger UI: http://localhost:5680/docs
- ReDoc: http://localhost:5680/redoc
- Docker & Docker Compose (recommended)
- Python 3.9+ (for local development)
- 4GB+ RAM (for model loading)
-
Clone the repository:
git clone https://github.com/devtitus/Project_zero_shot_classification.git cd Project_zero_shot_classification -
Build and run:
docker-compose up --build
-
Verify installation:
curl http://localhost:5680/docs
-
Clone and setup:
git clone https://github.com/devtitus/Project_zero_shot_classification.git cd Project_zero_shot_classification python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Run the application:
uvicorn classify:app --host 0.0.0.0 --port 5680
- Model Type: Sequence-to-Sequence Transformer
- Architecture: BART (Bidirectional and Auto-Regressive Transformers)
- Pre-training: Masked Language Modeling + Denoising
- Fine-tuning: Multi-Genre Natural Language Inference (MNLI)
- Parameters: ~406M
- Model Size: ~1.6GB
The model classifies descriptions into 24 predefined categories:
- Technology - Programming, software, hardware, AI/ML
- Education - Learning, tutorials, courses, academic content
- Gaming - Video games, game reviews, gameplay
- Lifestyle - Daily life, routines, personal development
- Comedy - Humor, jokes, entertainment
- Music - Songs, music videos, covers
- Finance - Investing, personal finance, business
- News - Current events, journalism, reporting
- Health & Fitness - Exercise, wellness, medical content
- Travel - Destinations, travel tips, vlogs
- Sports - Athletic events, sports analysis
- Food & Cooking - Recipes, cooking tutorials
- Beauty & Fashion - Makeup, style, fashion tips
- Entertainment - Movies, TV shows, pop culture
- DIY & Crafts - Handmade projects, tutorials
- Science - Scientific content, research, explanations
- Automotive - Cars, vehicles, automotive content
- Pets & Animals - Animal care, pet videos
- Vlogging - Personal vlogs, daily content
- Reaction & Commentary - Reactions, reviews, commentary
- ASMR & Relaxation - ASMR, relaxation content
- Business & Entrepreneurship - Business advice, startups
- True Crime - Crime stories, investigations
- Spirituality & Religion - Religious content, spiritual guidance
The application provides comprehensive logging for monitoring and debugging:
- INFO: Request/response logging, classification results
- ERROR: System errors and exceptions
- Console: Real-time logging to stdout
- Files: Daily log files in
/app/logs/directory - Format:
api_YYYY-MM-DD.log
Docker Compose creates a persistent volume category_logs that survives container restarts.
# Docker Compose
docker-compose logs -f
# Docker (standalone)
docker logs <container_id>
# Direct file access
docker exec -it category-classifier cat /app/logs/api_2024-01-01.log-
Build production image:
docker build -t category-classifier:production . -
Deploy with Docker Compose:
docker-compose -f docker-compose.yml up -d
-
Monitor deployment:
docker-compose logs -f
- Model Loading: Model is loaded once at startup
- Memory Requirements: ~2GB RAM for model + application
- Concurrency: FastAPI handles multiple requests concurrently
- Load Balancing: Use reverse proxy (nginx) for multiple instances
Currently, the application uses default configurations. For production:
- PORT: Change default port (5680)
- LOG_LEVEL: Adjust logging verbosity
- MODEL_PATH: Custom model location
- Cold Start: ~30-60 seconds for model loading
- Memory Usage: ~1.6GB for model weights
- CPU/GPU: Works on CPU, GPU acceleration available
- Latency: ~1-3 seconds per classification
- Throughput: ~10-20 requests/second (CPU)
- Batching: Single request processing
- GPU Acceleration: Use CUDA-enabled containers
- Model Quantization: Reduce model size for faster inference
- Caching: Cache frequent classifications
- Load Balancing: Distribute load across multiple instances
# Test classification endpoint
curl -X POST "http://localhost:5680/classify/" \
-H "Content-Type: application/json" \
-d '{"description": "Learn Python programming with our comprehensive tutorials"}'
# Test API documentation
curl http://localhost:5680/docsSuccess Response:
{
"description": "Learn Python programming with our comprehensive tutorials",
"predicted_category": "Education"
}Error Response:
{
"detail": [
{
"type": "missing",
"loc": ["body", "description"],
"msg": "Field required",
"input": {}
}
]
}The docker-compose.yml file configures:
- Service Name:
category-classifier - Port Mapping: 5680:5680
- Volume Mapping: Local directory + persistent logs
- Restart Policy: Unless stopped
The classify.py file contains:
- Categories: 24 predefined classification categories
- Model: BART-large-MNLI from Hugging Face
- Logging: File and console logging setup
- Middleware: Request/response logging
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make changes and test locally
- Commit changes:
git commit -m 'Add feature' - Push to branch:
git push origin feature-name - Create Pull Request
- Follow PEP 8 Python style guidelines
- Add tests for new features
- Update documentation for changes
- Use meaningful commit messages
This project is licensed under the MIT License - see the LICENSE file for details.
Model Loading Errors:
- Ensure sufficient memory (4GB+ recommended)
- Check model files in
models/bart-large-mnli/directory
Docker Issues:
- Verify Docker is running
- Check port 5680 is available
- Ensure proper file permissions
API Errors:
- Verify JSON format in request body
- Check description field is present
- Review logs for detailed error messages
- Issues: Report bugs and feature requests on GitHub
- Documentation: Check this README and inline code comments
- Logs: Review application logs for error details
For questions and support, please contact the project maintainer or create an issue on GitHub.
Note: This is a production-ready application for zero-shot text classification. Ensure proper resource allocation and monitoring in production environments.