Spaces:
Sleeping
π Hugging Face Spaces Deployment Guide (Docker + Streamlit)
This guide will walk you through deploying your RAG system to Hugging Face Spaces using Docker with Streamlit.
π Prerequisites
- Hugging Face account
- All files from the
huggingface_deploy/folder - Basic understanding of Docker (optional)
π― Step-by-Step Deployment
Step 1: Create a New Space
Go to Hugging Face Spaces:
- Visit https://huggingface.co/spaces
- Click "Create new Space"
Configure your Space:
- Owner: Choose your username or organization
- Space name: Choose a unique name (e.g.,
my-rag-system) - License: Choose appropriate license (e.g., MIT)
- SDK: Select Docker
- Visibility: Choose Public or Private
- Hardware: Select appropriate hardware (CPU is sufficient for basic usage)
Click "Create Space"
Step 2: Upload Files
Option A: Using Git (Recommended)
- Clone your Space repository:
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
cd YOUR_SPACE_NAME
- Copy files from the deployment folder:
cp -r ../huggingface_deploy/* .
- Commit and push:
git add .
git commit -m "Initial RAG system deployment with Docker"
git push
Option B: Using Web Interface
- Upload files manually:
- Go to your Space's "Files" tab
- Click "Add file" β "Upload files"
- Upload all files from the
huggingface_deploy/folder:app.pyrag_system.pypdf_processor.pyrequirements.txtDockerfile.dockerignoreREADME.md
Step 3: Configure the Space
Set up environment variables (optional):
- Go to your Space's "Settings" tab
- Add environment variables if needed:
EMBEDDING_MODEL=all-MiniLM-L6-v2 GENERATIVE_MODEL=Qwen/Qwen2.5-1.5B-Instruct
Configure hardware (if needed):
- Go to "Settings" β "Hardware"
- Select appropriate hardware based on your needs
Step 4: Deploy and Test
Wait for deployment:
- Hugging Face will automatically build and deploy your Docker container
- This may take 10-15 minutes for the first deployment (model downloads)
Test your application:
- Visit your Space URL:
https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME - Upload a PDF document
- Ask questions to test the RAG system
- Visit your Space URL:
π§ Docker Configuration
Dockerfile Features
- Base Image: Python 3.10 slim
- System Dependencies: build-essential, curl
- Health Check: Monitors Streamlit health endpoint
- Environment Variables: Configured for Streamlit
- Port: Exposes port 8501
Local Docker Testing
You can test the Docker build locally:
# Build the Docker image
docker build -t rag-system .
# Run the container
docker run -p 8501:8501 rag-system
# Or use docker-compose
docker-compose up --build
π§ Configuration Options
Environment Variables
You can customize your deployment by setting these environment variables in your Space settings:
# Model configuration
EMBEDDING_MODEL=all-MiniLM-L6-v2
GENERATIVE_MODEL=Qwen/Qwen2.5-1.5B-Instruct
# Chunk sizes
CHUNK_SIZES=100,400
# Vector store path
VECTOR_STORE_PATH=./vector_store
# Streamlit configuration
STREAMLIT_SERVER_PORT=8501
STREAMLIT_SERVER_ADDRESS=0.0.0.0
STREAMLIT_SERVER_HEADLESS=true
Hardware Options
- CPU: Sufficient for basic usage, slower inference
- T4: Good for faster inference, limited memory
- A10G: High performance, more memory
- A100: Maximum performance, highest cost
π Troubleshooting
Common Issues
Build Fails
- Check that all required files are uploaded
- Verify
requirements.txtandDockerfileare correct - Check the build logs for specific errors
Model Loading Errors
- Ensure internet connectivity for model downloads
- Check model names are correct
- Verify sufficient disk space
Memory Issues
- Use smaller models
- Reduce chunk sizes
- Upgrade to higher-tier hardware
Slow Performance
- Upgrade hardware tier
- Use smaller embedding models
- Optimize chunk sizes
Docker Build Issues
- Check
.dockerignoreexcludes unnecessary files - Verify Dockerfile syntax
- Check for missing dependencies
- Check
Debug Mode
To enable debug logging, add this to your app.py:
import logging
logging.basicConfig(level=logging.DEBUG)
π Monitoring
Space Metrics
- Build Status: Check if Docker build was successful
- Runtime Logs: Monitor application logs
- Resource Usage: Track CPU and memory usage
- Error Logs: Identify and fix issues
Docker Logs
Check Docker logs in your Space:
- Go to "Settings" β "Logs"
- Monitor build and runtime logs
- Look for error messages
π Security Considerations
File Upload:
- Validate PDF files before processing
- Implement file size limits
- Check file types
Model Access:
- Use appropriate model access tokens
- Consider private models for sensitive data
Data Privacy:
- Be aware that uploaded documents are processed
- Consider data retention policies
Docker Security:
- Use non-root user in Dockerfile
- Minimize attack surface
- Keep base images updated
π Scaling
For Production Use
Multiple Spaces:
- Create separate Spaces for different use cases
- Use different hardware tiers as needed
Custom Domains:
- Set up custom domains for your Spaces
- Use proper SSL certificates
Load Balancing:
- Consider multiple Space instances
- Implement proper caching strategies
π Success Checklist
- Space created successfully with Docker SDK
- All files uploaded (including Dockerfile)
- Docker build completed without errors
- Application loads correctly
- PDF upload works
- Question answering works
- Search results display correctly
- Performance is acceptable
π Support
If you encounter issues:
- Check the logs in your Space's "Logs" tab
- Review this guide for common solutions
- Search Hugging Face documentation
- Create an issue in the project repository
- Contact Hugging Face support for Space-specific issues
π Next Steps
After successful deployment:
- Test thoroughly with different document types
- Optimize performance based on usage patterns
- Add custom features as needed
- Share your Space with others
- Monitor usage and gather feedback
π Updates and Maintenance
Updating Your Space
- Make changes locally
- Test with Docker locally
- Push changes to your Space repository
- Monitor the rebuild process
Version Management
- Use specific versions in
requirements.txt - Tag your Docker images
- Keep track of model versions
Happy deploying with Docker! π³π