File size: 6,313 Bytes
1a7ee60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Hugging Face Spaces Deployment Guide

Complete guide to deploy the IDP API on Hugging Face Spaces.

## Prerequisites

- Hugging Face account
- Trained model weights (classifier and NER)
- Git installed locally

## Step 1: Create a New Space

1. Go to [Hugging Face Spaces](https://huggingface.co/spaces)
2. Click **"Create new Space"**
3. Configure:
   - **Space name**: `idp-api` (or your preferred name)
   - **License**: Apache 2.0 (or your choice)
   - **Select SDK**: Docker
   - **Hardware**: CPU Basic (free) or upgrade to GPU if needed

4. Click **"Create Space"**

## Step 2: Prepare Your Files

Create a `Dockerfile` in your project root:

```dockerfile

FROM python:3.10-slim



# Install system dependencies

RUN apt-get update && apt-get install -y \

    poppler-utils \

    libgomp1 \

    libglib2.0-0 \

    libsm6 \

    libxext6 \

    libxrender-dev \

    libgl1-mesa-glx \

    && rm -rf /var/lib/apt/lists/*



# Set working directory

WORKDIR /app



# Copy requirements

COPY requirements.txt .



# Install Python dependencies

RUN pip install --no-cache-dir -r requirements.txt



# Copy application code

COPY *.py ./

COPY models/ ./models/



# Expose port 7860 (HF Spaces default)

EXPOSE 7860



# Run the API server

CMD ["uvicorn", "api_server:app", "--host", "0.0.0.0", "--port", "7860"]

```

## Step 3: Train Your Models

Before deployment, train your models locally:

```bash

# Train classifier

python train_classifier.py



# Train NER model

python train_ner.py

```

This will create:
- `models/classifier/best_classifier.pt`
- `models/ner/best_ner.pt`

## Step 4: (Optional) Optimize Models for Faster Inference

Convert to ONNX and quantize:

```bash

# Convert classifier to ONNX

python model_optimizer.py convert_classifier \

    models/classifier/best_classifier.pt \

    models/classifier/classifier.onnx



# Quantize classifier

python model_optimizer.py quantize \

    models/classifier/classifier.onnx \

    models/classifier/classifier_quantized.onnx



# Convert NER to ONNX

python model_optimizer.py convert_ner \

    models/ner/best_ner.pt \

    models/ner/ner.onnx



# Quantize NER

python model_optimizer.py quantize \

    models/ner/ner.onnx \

    models/ner/ner_quantized.onnx

```

If using ONNX models, update `api_server.py` to use ONNX inference sessions.

## Step 5: Push to Hugging Face Space

Clone your Space repository:

```bash

git clone https://huggingface.co/spaces/YOUR_USERNAME/idp-api

cd idp-api

```

Copy your files:

```bash

# Copy Python files

cp /path/to/your/project/*.py .



# Copy models

cp -r /path/to/your/project/models .



# Copy config files

cp /path/to/your/project/requirements.txt .

cp /path/to/your/project/Dockerfile .

```

Create a `README.md` for your Space:

```markdown

---

title: IDP API

emoji: 📄

colorFrom: blue

colorTo: green

sdk: docker

pinned: false

---



# Intelligent Document Processing API



API for extracting structured data from invoices, receipts, and forms.



## Features



- Lightweight OCR with PaddleOCR

- Document classification (Invoice, Receipt, Form)

- Entity extraction (dates, amounts, names, etc.)

- Confidence scoring

- CORS-enabled for web integration



## API Endpoints



### POST /process



Upload a document (PDF or image) and get structured data.



### GET /health



Health check endpoint.



See full documentation at [YOUR_REPO_URL]

```

Commit and push:

```bash

git add .

git commit -m "Initial deployment"

git push

```

## Step 6: Monitor Deployment

1. Go to your Space URL: `https://huggingface.co/spaces/YOUR_USERNAME/idp-api`
2. Watch the build logs in the "Logs" tab
3. Wait for the build to complete (may take 5-10 minutes for first build)
4. Once running, the Space will show "Running" status

## Step 7: Test Your API

Test the health endpoint:

```bash

curl https://YOUR_USERNAME-idp-api.hf.space/health

```

Test document processing:

```bash

curl -X POST \

  https://YOUR_USERNAME-idp-api.hf.space/process \

  -F "file=@sample_invoice.pdf"

```

## Step 8: Configure for Production

### Upgrade Hardware (Optional)

If CPU performance is insufficient:

1. Go to Space settings
2. Under "Hardware", upgrade to:
   - **CPU Upgrade**: 2 vCPUs, 16GB RAM
   - **GPU** T4 Small**: 15GB VRAM (if using GPU)



### Set Secrets (Optional)



If you need API keys or secrets:



1. Go to Space settings

2. Add secrets under "Repository secrets"

3. Access in code via `os.environ.get('SECRET_NAME')`



### Enable Persistent Storage (Optional)



For caching or logging:



1. Go to Space settings

2. Enable "Persistent Storage"

3. Data will persist in `/data` directory



## Troubleshooting



### Build Fails



**Issue**: Docker build fails with package errors



**Solution**:

- Check `requirements.txt` for version conflicts

- Review build logs for specific errors

- Ensure Dockerfile has all system dependencies



### Out of Memory



**Issue**: API crashes with OOM errors



**Solution**:

- Upgrade to higher memory tier

- Reduce batch size

- Use ONNX quantized models

- Disable GPU if not needed



### Slow Inference



**Issue**: Processing takes >5 seconds per document



**Solution**:

- Use ONNX models with INT8 quantization

- Upgrade to GPU hardware

- Reduce OCR DPI (lower quality but faster)

- Cache models in memory (done by default)



## Cost Optimization



**Free Tier**:

- CPU Basic: Free forever

- Suitable for demos and low-traffic apps

- May sleep after inactivity



**Paid Tier** (if needed):

- CPU Upgrade: ~$0.50/hour

- GPU T4: ~$0.60/hour

- Only charged when running



**Tips**:

- Use CPU for development

- Upgrade to GPU only for production with high traffic

- Use ONNX + quantization to maximize CPU performance



## Next Steps



- Set up monitoring with [Hugging Face Analytics](https://huggingface.co/docs/hub/spaces-analytics)

- Add authentication if needed

- Create a custom domain (paid feature)

- Integrate with your Next.js frontend (see `nextjs_integration_guide.md`)



## Support



For issues:

- Check [HF Spaces docs](https://huggingface.co/docs/hub/spaces)

- Ask in [HF Forums](https://discuss.huggingface.co/)

- Review API logs in Space "Logs" tab