File size: 6,266 Bytes
af107f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# Deployment Guide for HuggingFace Spaces

## Prerequisites

1. **HuggingFace Account**: Sign up at https://huggingface.co/
2. **Git**: Installed on your local machine
3. **Git LFS**: For large file storage (optional)

## Step-by-Step Deployment

### 1. Create a New Space

1. Go to https://huggingface.co/spaces
2. Click "Create new Space"
3. Fill in the details:
   - **Space name**: `pdf-redaction-api` (or your preferred name)
   - **License**: MIT
   - **SDK**: Docker
   - **Hardware**: CPU Basic (free tier) or upgrade if needed
4. Click "Create Space"

### 2. Clone Your Space Repository

```bash
git clone https://huggingface.co/spaces/YOUR_USERNAME/pdf-redaction-api
cd pdf-redaction-api
```

### 3. Copy All Files to the Repository

Copy all files from this project to your cloned space:

```bash
# Copy all files
cp -r /path/to/pdf-redaction-api/* .

# Check the files
ls -la
```

You should see:
- `main.py`
- `app/`
- `Dockerfile`
- `requirements.txt`
- `README.md`
- `.gitignore`
- `.dockerignore`
- `uploads/` (with .gitkeep)
- `outputs/` (with .gitkeep)

### 4. Commit and Push

```bash
# Add all files
git add .

# Commit
git commit -m "Initial deployment of PDF Redaction API"

# Push to HuggingFace
git push
```

### 5. Monitor Deployment

1. Go to your Space URL: `https://huggingface.co/spaces/YOUR_USERNAME/pdf-redaction-api`
2. You'll see the build logs
3. Wait for the build to complete (usually 5-10 minutes)
4. Once complete, your API will be live!

### 6. Test Your Deployment

```bash
# Check health
curl https://YOUR_USERNAME-pdf-redaction-api.hf.space/health

# Test with a PDF
curl -X POST "https://YOUR_USERNAME-pdf-redaction-api.hf.space/redact" \
  -F "file=@test.pdf" \
  -F "dpi=300"
```

## Configuration Options

### Hardware Upgrades

For better performance, consider upgrading your Space hardware:

1. Go to Space Settings
2. Click on "Hardware"
3. Choose:
   - **CPU Basic** (Free): Good for testing, slower processing
   - **CPU Upgrade** (~$0.50/hour): Faster processing
   - **GPU** (~$0.60-3/hour): Best for large documents

### Environment Variables

Add environment variables in Space Settings if needed:

```bash
HF_HOME=/app/cache
PYTHONUNBUFFERED=1
```

### Persistent Storage

For persistent file storage:

1. Go to Space Settings
2. Enable "Persistent Storage"
3. This keeps uploaded/processed files between restarts

## Custom Domain (Optional)

To use a custom domain:

1. Go to Space Settings
2. Click "Domains"
3. Add your custom domain
4. Follow DNS configuration instructions

## Monitoring and Logs

### View Logs

1. Go to your Space page
2. Click on "Logs" tab
3. Monitor real-time logs

### Check Resource Usage

1. Click on "Insights" tab
2. View CPU/Memory usage
3. Monitor request patterns

## Security Considerations

### For Production Use

1. **Add Authentication**: 
   - Implement API key authentication
   - Use OAuth2 for user management

2. **Rate Limiting**:
   - Add rate limiting to prevent abuse
   - Use slowapi or similar libraries

3. **File Size Limits**:
   - Restrict upload file sizes
   - Implement timeout for long-running requests

4. **HTTPS Only**:
   - HuggingFace Spaces provides HTTPS by default
   - Ensure all requests use HTTPS

Example with API key authentication:

```python
from fastapi import Security, HTTPException, status
from fastapi.security import APIKeyHeader

API_KEY = "your-secret-key"
api_key_header = APIKeyHeader(name="X-API-Key")

def verify_api_key(api_key: str = Security(api_key_header)):
    if api_key != API_KEY:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid API Key"
        )
    return api_key

# Add to endpoints
@app.post("/redact")
async def redact_pdf(
    file: UploadFile = File(...),
    api_key: str = Security(verify_api_key)
):
    # Your code here
```

## Troubleshooting

### Build Fails

**Problem**: Docker build fails

**Solution**:
- Check Dockerfile syntax
- Ensure all dependencies are in requirements.txt
- Review build logs for specific errors

### Out of Memory

**Problem**: API crashes with OOM errors

**Solution**:
- Reduce default DPI to 200
- Upgrade to larger hardware
- Implement request queuing

### Slow Processing

**Problem**: Redaction takes too long

**Solution**:
- Lower DPI (150-200 for faster processing)
- Upgrade to GPU hardware
- Optimize batch processing

### Model Download Issues

**Problem**: Model fails to download

**Solution**:
- Check HuggingFace model availability
- Verify internet access in Space
- Pre-download model and include in Docker image

## Updating Your Space

To update your deployed API:

```bash
# Make changes locally
# Test changes

# Commit and push
git add .
git commit -m "Update: description of changes"
git push

# HuggingFace will automatically rebuild
```

## Cost Estimation

### Free Tier
- CPU Basic
- Limited to 2 CPU cores
- 16GB RAM
- Good for: Testing, low-traffic demos

### Paid Tiers
- CPU Upgrade: ~$0.50/hour (~$360/month if always on)
- GPU T4: ~$0.60/hour (~$432/month)
- GPU A10G: ~$1.50/hour (~$1,080/month)

**Recommendation**: Start with free tier, upgrade based on usage

## Alternative Deployment Options

### 1. Deploy on Your Own Server

```bash
# Build Docker image
docker build -t pdf-redaction-api .

# Run container
docker run -p 7860:7860 pdf-redaction-api
```

### 2. Deploy on Cloud Platforms

- **AWS ECS/Fargate**: For scalable production
- **Google Cloud Run**: Serverless container deployment
- **Azure Container Instances**: Easy container deployment
- **DigitalOcean App Platform**: Simple PaaS deployment

### 3. Deploy on Render.com

1. Connect your GitHub repo
2. Select "Docker" as environment
3. Deploy automatically

## Support

For issues:
1. Check HuggingFace Spaces documentation
2. Review logs in Space dashboard
3. Test locally with Docker first
4. Open issue on your repository

## Next Steps

After successful deployment:

1. βœ… Test all API endpoints
2. βœ… Set up monitoring
3. βœ… Configure custom domain (optional)
4. βœ… Add authentication for production
5. βœ… Implement rate limiting
6. βœ… Set up error tracking (e.g., Sentry)
7. βœ… Create API documentation with examples
8. βœ… Add usage analytics

Your API is now live and ready to use! πŸš€