Text-to-Document-Generation/PDF-Redaction-API
0
1# Deployment Guide for HuggingFace Spaces2 3## Prerequisites4 51. **HuggingFace Account**: Sign up at https://huggingface.co/62. **Git**: Installed on your local machine73. **Git LFS**: For large file storage (optional)8 9## Step-by-Step Deployment10 11### 1. Create a New Space12 131. Go to https://huggingface.co/spaces142. Click "Create new Space"153. Fill in the details:16 - **Space name**: `pdf-redaction-api` (or your preferred name)17 - **License**: MIT18 - **SDK**: Docker19 - **Hardware**: CPU Basic (free tier) or upgrade if needed204. Click "Create Space"21 22### 2. Clone Your Space Repository23 24```bash25git clone https://huggingface.co/spaces/YOUR_USERNAME/pdf-redaction-api26cd pdf-redaction-api27```28 29### 3. Copy All Files to the Repository30 31Copy all files from this project to your cloned space:32 33```bash34# Copy all files35cp -r /path/to/pdf-redaction-api/* .36 37# Check the files38ls -la39```40 41You should see:42- `main.py`43- `app/`44- `Dockerfile`45- `requirements.txt`46- `README.md`47- `.gitignore`48- `.dockerignore`49- `uploads/` (with .gitkeep)50- `outputs/` (with .gitkeep)51 52### 4. Commit and Push53 54```bash55# Add all files56git add .57 58# Commit59git commit -m "Initial deployment of PDF Redaction API"60 61# Push to HuggingFace62git push63```64 65### 5. Monitor Deployment66 671. Go to your Space URL: `https://huggingface.co/spaces/YOUR_USERNAME/pdf-redaction-api`682. You'll see the build logs693. Wait for the build to complete (usually 5-10 minutes)704. Once complete, your API will be live!71 72### 6. Test Your Deployment73 74```bash75# Check health76curl https://YOUR_USERNAME-pdf-redaction-api.hf.space/health77 78# Test with a PDF79curl -X POST "https://YOUR_USERNAME-pdf-redaction-api.hf.space/redact" \80 -F "file=@test.pdf" \81 -F "dpi=300"82```83 84## Configuration Options85 86### Hardware Upgrades87 88For better performance, consider upgrading your Space hardware:89 901. Go to Space Settings912. Click on "Hardware"923. Choose:93 - **CPU Basic** (Free): Good for testing, slower processing94 - **CPU Upgrade** (~$0.50/hour): Faster processing95 - **GPU** (~$0.60-3/hour): Best for large documents96 97### Environment Variables98 99Add environment variables in Space Settings if needed:100 101```bash102HF_HOME=/app/cache103PYTHONUNBUFFERED=1104```105 106### Persistent Storage107 108For persistent file storage:109 1101. Go to Space Settings1112. Enable "Persistent Storage"1123. This keeps uploaded/processed files between restarts113 114## Custom Domain (Optional)115 116To use a custom domain:117 1181. Go to Space Settings1192. Click "Domains"1203. Add your custom domain1214. Follow DNS configuration instructions122 123## Monitoring and Logs124 125### View Logs126 1271. Go to your Space page1282. Click on "Logs" tab1293. Monitor real-time logs130 131### Check Resource Usage132 1331. Click on "Insights" tab1342. View CPU/Memory usage1353. Monitor request patterns136 137## Security Considerations138 139### For Production Use140 1411. **Add Authentication**: 142 - Implement API key authentication143 - Use OAuth2 for user management144 1452. **Rate Limiting**:146 - Add rate limiting to prevent abuse147 - Use slowapi or similar libraries148 1493. **File Size Limits**:150 - Restrict upload file sizes151 - Implement timeout for long-running requests152 1534. **HTTPS Only**:154 - HuggingFace Spaces provides HTTPS by default155 - Ensure all requests use HTTPS156 157Example with API key authentication:158 159```python160from fastapi import Security, HTTPException, status161from fastapi.security import APIKeyHeader162 163API_KEY = "your-secret-key"164api_key_header = APIKeyHeader(name="X-API-Key")165 166def verify_api_key(api_key: str = Security(api_key_header)):167 if api_key != API_KEY:168 raise HTTPException(169 status_code=status.HTTP_401_UNAUTHORIZED,170 detail="Invalid API Key"171 )172 return api_key173 174# Add to endpoints175@app.post("/redact")176async def redact_pdf(177 file: UploadFile = File(...),178 api_key: str = Security(verify_api_key)179):180 # Your code here181```182 183## Troubleshooting184 185### Build Fails186 187**Problem**: Docker build fails188 189**Solution**:190- Check Dockerfile syntax191- Ensure all dependencies are in requirements.txt192- Review build logs for specific errors193 194### Out of Memory195 196**Problem**: API crashes with OOM errors197 198**Solution**:199- Reduce default DPI to 200200- Upgrade to larger hardware201- Implement request queuing202 203### Slow Processing204 205**Problem**: Redaction takes too long206 207**Solution**:208- Lower DPI (150-200 for faster processing)209- Upgrade to GPU hardware210- Optimize batch processing211 212### Model Download Issues213 214**Problem**: Model fails to download215 216**Solution**:217- Check HuggingFace model availability218- Verify internet access in Space219- Pre-download model and include in Docker image220 221## Updating Your Space222 223To update your deployed API:224 225```bash226# Make changes locally227# Test changes228 229# Commit and push230git add .231git commit -m "Update: description of changes"232git push233 234# HuggingFace will automatically rebuild235```236 237## Cost Estimation238 239### Free Tier240- CPU Basic241- Limited to 2 CPU cores242- 16GB RAM243- Good for: Testing, low-traffic demos244 245### Paid Tiers246- CPU Upgrade: ~$0.50/hour (~$360/month if always on)247- GPU T4: ~$0.60/hour (~$432/month)248- GPU A10G: ~$1.50/hour (~$1,080/month)249 250**Recommendation**: Start with free tier, upgrade based on usage251 252## Alternative Deployment Options253 254### 1. Deploy on Your Own Server255 256```bash257# Build Docker image258docker build -t pdf-redaction-api .259 260# Run container261docker run -p 7860:7860 pdf-redaction-api262```263 264### 2. Deploy on Cloud Platforms265 266- **AWS ECS/Fargate**: For scalable production267- **Google Cloud Run**: Serverless container deployment268- **Azure Container Instances**: Easy container deployment269- **DigitalOcean App Platform**: Simple PaaS deployment270 271### 3. Deploy on Render.com272 2731. Connect your GitHub repo2742. Select "Docker" as environment2753. Deploy automatically276 277## Support278 279For issues:2801. Check HuggingFace Spaces documentation2812. Review logs in Space dashboard2823. Test locally with Docker first2834. Open issue on your repository284 285## Next Steps286 287After successful deployment:288 2891. ✅ Test all API endpoints2902. ✅ Set up monitoring2913. ✅ Configure custom domain (optional)2924. ✅ Add authentication for production2935. ✅ Implement rate limiting2946. ✅ Set up error tracking (e.g., Sentry)2957. ✅ Create API documentation with examples2968. ✅ Add usage analytics297 298Your API is now live and ready to use! 🚀299 