Text-to-Document-Generation/PDF-Redaction-API
0
1# Quick Start Guide ๐2 3## Local Development (5 minutes)4 5### 1. Install System Dependencies6 7**Ubuntu/Debian:**8```bash9sudo apt-get update10sudo apt-get install -y tesseract-ocr poppler-utils11```12 13**macOS:**14```bash15brew install tesseract poppler16```17 18**Windows:**19- Download Tesseract: https://github.com/UB-Mannheim/tesseract/wiki20- Download Poppler: https://github.com/oschwartz10612/poppler-windows/releases21 22### 2. Install Python Dependencies23 24```bash25pip install -r requirements.txt26```27 28### 3. Run the Server29 30```bash31python main.py32```33 34The API will be available at: `http://localhost:7860`35 36### 4. Test with cURL37 38```bash39# Health check40curl http://localhost:7860/health41 42# Redact a PDF43curl -X POST "http://localhost:7860/redact" \44 -F "file=@your_document.pdf" \45 -F "dpi=300"46```47 48### 5. Access API Documentation49 50Open in browser: `http://localhost:7860/docs`51 52## Using Docker (3 minutes)53 54### 1. Build Image55 56```bash57docker build -t pdf-redaction-api .58```59 60### 2. Run Container61 62```bash63docker run -p 7860:7860 pdf-redaction-api64```65 66### 3. Test67 68```bash69curl http://localhost:7860/health70```71 72## Deploy to HuggingFace Spaces (10 minutes)73 74### 1. Create Space75 761. Go to https://huggingface.co/spaces772. Click "Create new Space"783. Name: `pdf-redaction-api`794. SDK: **Docker**805. Click "Create Space"81 82### 2. Push Code83 84```bash85# Clone your space86git clone https://huggingface.co/spaces/YOUR_USERNAME/pdf-redaction-api87cd pdf-redaction-api88 89# Copy all project files90cp -r /path/to/project/* .91 92# Commit and push93git add .94git commit -m "Initial deployment"95git push96```97 98### 3. Wait for Build99 100Monitor at: `https://huggingface.co/spaces/YOUR_USERNAME/pdf-redaction-api`101 102### 4. Test Your Deployed API103 104```bash105curl https://YOUR_USERNAME-pdf-redaction-api.hf.space/health106```107 108## Example Usage109 110### Python Client111 112```python113import requests114 115# Upload and redact116files = {"file": open("document.pdf", "rb")}117response = requests.post(118 "http://localhost:7860/redact",119 files=files,120 params={"dpi": 300}121)122 123result = response.json()124job_id = result["job_id"]125 126# Download redacted PDF127redacted = requests.get(f"http://localhost:7860/download/{job_id}")128with open("redacted.pdf", "wb") as f:129 f.write(redacted.content)130 131print(f"Redacted {len(result['entities'])} entities")132```133 134### JavaScript/Node.js135 136```javascript137const FormData = require('form-data');138const fs = require('fs');139const axios = require('axios');140 141async function redactPDF() {142 const form = new FormData();143 form.append('file', fs.createReadStream('document.pdf'));144 145 // Upload and redact146 const response = await axios.post(147 'http://localhost:7860/redact',148 form,149 {150 headers: form.getHeaders(),151 params: { dpi: 300 }152 }153 );154 155 const { job_id } = response.data;156 157 // Download redacted PDF158 const redacted = await axios.get(159 `http://localhost:7860/download/${job_id}`,160 { responseType: 'arraybuffer' }161 );162 163 fs.writeFileSync('redacted.pdf', redacted.data);164 console.log('Redaction complete!');165}166 167redactPDF();168```169 170### cURL Advanced171 172```bash173# Redact only specific entity types174curl -X POST "http://localhost:7860/redact" \175 -F "file=@document.pdf" \176 -F "dpi=300" \177 -F "entity_types=PER,ORG"178 179# Get statistics180curl http://localhost:7860/stats181 182# Download specific file183curl -O -J http://localhost:7860/download/JOB_ID_HERE184```185 186## Common Use Cases187 188### 1. Redact All Personal Information189 190```python191response = requests.post(192 "http://localhost:7860/redact",193 files={"file": open("resume.pdf", "rb")},194 params={"dpi": 300}195)196```197 198### 2. Redact Only Names and Organizations199 200```python201response = requests.post(202 "http://localhost:7860/redact",203 files={"file": open("contract.pdf", "rb")},204 params={205 "dpi": 300,206 "entity_types": "PER,ORG"207 }208)209```210 211### 3. Fast Processing (Lower Quality)212 213```python214response = requests.post(215 "http://localhost:7860/redact",216 files={"file": open("large_doc.pdf", "rb")},217 params={"dpi": 150} # Faster but less accurate218)219```220 221### 4. High Quality (Slower)222 223```python224response = requests.post(225 "http://localhost:7860/redact",226 files={"file": open("important.pdf", "rb")},227 params={"dpi": 600} # Best quality, slowest228)229```230 231## Troubleshooting232 233### "Model not loaded"234**Problem**: NER model failed to load 235**Solution**: Check internet connection, wait for model download236 237### "Tesseract not found"238**Problem**: OCR engine not installed 239**Solution**: Install tesseract-ocr system package240 241### "Poppler not found"242**Problem**: PDF converter not installed 243**Solution**: Install poppler-utils system package244 245### Slow processing246**Problem**: Redaction takes too long 247**Solution**: Lower DPI to 150-200248 249### Out of memory250**Problem**: Large PDF crashes the API 251**Solution**: 252- Process one page at a time253- Increase container memory254- Lower DPI255 256## Next Steps257 258- โ
Read full [README.md](README.md) for API details259- โ
Check [DEPLOYMENT.md](DEPLOYMENT.md) for production setup260- โ
Review [STRUCTURE.md](STRUCTURE.md) for code organization261- โ
Run tests: `pytest tests/`262- โ
Add authentication for production use263- โ
Set up monitoring and logging264 265## Support266 267- ๐ API Docs: `http://localhost:7860/docs`268- ๐ Issues: Create on your repository269- ๐ฌ HuggingFace: Community forums270 271Happy redacting! ๐272 