File size: 7,999 Bytes
8c33e8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
---

title: Invoice OCR System
emoji: πŸ“„
colorFrom: blue
colorTo: indigo
sdk: docker
pinned: false
license: mit
app_port: 7860
---


# πŸ“„ Invoice OCR System

**High-accuracy invoice processing** powered by Google Document AI + Gemini 2.0 Flash

πŸ”’ **Access Control**: Restricted to authorized users only via Hugging Face OAuth

## πŸš€ Features

βœ… **Complete Invoice Extraction**:
- Supplier information (name, address, tax ID)
- Customer/Bill-to information  
- Invoice metadata (number, dates, PO, terms)
- Line items (code, description, type, qty, unit, price, total)
- Financial summary (subtotal, tax, total)
- Payment information

βœ… **Type Classification**: Automatic categorization (produce, protein, beverage, dairy, grain, condiment, cleaning, packaging, miscellaneous)  
βœ… **Validation & Correction**: Mandatory math validation with Β±2% tolerance  
βœ… **Cost Tracking**: Real-time processing cost calculation (~$0.002 per invoice)  
βœ… **Image + Text Mode**: Maximum accuracy with dual validation  
βœ… **SQLite Database**: Complete invoice history  
βœ… **REST API**: FastAPI backend with 7 endpoints  
βœ… **Web Interface**: Drag-and-drop upload with instant results  
βœ… **Authentication**: User whitelist for secure access

---

## πŸ’° Cost Per Invoice

| Component | Cost |
|-----------|------|
| Document AI OCR | $0.001500 |
| Gemini 2.0 Flash Input (~2,000 tokens) | $0.000200 |
| Gemini 2.0 Flash Output (~800 tokens) | $0.000320 |
| **TOTAL** | **~$0.002** |

**Free Tier**: First 1,000 invoices/month FREE!

---

## πŸ“‹ Setup

### 1. Install Dependencies
```bash

pip install -r requirements.txt

```

### 2. Configure Environment
Create `.env` file:
```bash

PROJECT_ID=your_project_id

LOCATION=eu

PROCESSOR_ID=your_processor_id

GEMINI_API_KEY=your_gemini_key

GOOGLE_APPLICATION_CREDENTIALS=path_to_credentials.json

```

### 3. Run Server
```bash

python app.py

```

### 4. Open Browser
```

http://localhost:7860

```

---

## πŸ”§ API Endpoints

```

POST /upload          # Upload and process invoice

GET /invoices         # List all invoices

GET /invoices/{id}    # Get specific invoice

DELETE /invoices/{id} # Delete invoice

GET /stats            # Get statistics

```

---

## πŸ“Š Invoice Data Structure

```json

{

  "supplier": {

    "name": "Acme Corp",

    "address": "123 Business St",

    "tax_id": "12-3456789"

  },

  "customer": {

    "name": "Customer Inc",

    "address": "456 Client Ave"

  },

  "invoice_details": {

    "invoice_number": "INV-2025-001",

    "invoice_date": "2025-10-15",

    "due_date": "2025-11-15",

    "payment_terms": "Net 30"

  },

  "line_items": [

    {

      "item_code": "PROD-001",

      "description": "Widget Pro",

      "quantity": 10,

      "unit": "pcs",

      "unit_price": 100.00,

      "total_price": 1000.00

    }

  ],

  "financial_summary": {

    "subtotal": 1000.00,

    "tax_amount": 100.00,

    "total_amount": 1100.00,

    "currency": "USD"

  }

}

```

---

## πŸš€ Deploy to Hugging Face Spaces (with Authentication)

### 1. Create Space
- Go to https://huggingface.co/new-space
- **Name**: invoice-ocr (or your choice)
- **SDK**: Docker
- **Visibility**: Private or Public (authentication works for both)
- Click "Create Space"

### 2. Clone and Add Files
```bash

git clone https://huggingface.co/spaces/YOUR_USERNAME/invoice-ocr

cd invoice-ocr

```

Copy all files to the cloned directory:
```bash

# Copy from your local development directory

cp Dockerfile requirements.txt app.py ocr_invoice.py cost_tracker.py auth.py allowed_users.txt ./

cp -r static/ ./

```

### 3. Configure User Whitelist

**Option A: Using allowed_users.txt file (Recommended)**



Edit `allowed_users.txt` and add authorized Hugging Face usernames (one per line):

```

johndoe

janedoe

companyuser1

```



**Option B: Using Environment Variable**

In Space settings β†’ Variables & Secrets, add:
```

ALLOWED_USERS=johndoe,janedoe,companyuser1

```

### 4. Add Google Cloud Secrets

In Space settings β†’ Variables & Secrets, add these as **Secrets**:

| Name | Value | Example |
|------|-------|---------|
| `PROJECT_ID` | Your GCP project ID | `836634225535` |
| `LOCATION` | Document AI location | `eu` |
| `PROCESSOR_ID` | Document AI processor ID | `696ca0c7b4383217` |
| `GEMINI_API_KEY` | Your Gemini API key | `AIza...` |
| `GOOGLE_APPLICATION_CREDENTIALS` | **Full JSON content** from service account key | `{"type":"service_account","project_id":"..."}` |

⚠️ **Important**: For `GOOGLE_APPLICATION_CREDENTIALS`, paste the **entire JSON file content** as the secret value, not the file path.

### 5. Deploy
```bash

git add .

git commit -m "Deploy invoice OCR with authentication"

git push

```

### 6. Enable Authentication (Critical Step)

After deployment, in your Space settings:

1. Go to **Settings** β†’ **General**
2. Scroll to **Sign-in with Hugging Face**
3. **Enable** the toggle
4. Select **Restricted** (only whitelisted users can access)

This enables OAuth login before accessing your Space.

### 7. Test Access

1. Open your Space URL: `https://huggingface.co/spaces/YOUR_USERNAME/invoice-ocr`
2. You'll be prompted to sign in with Hugging Face
3. Only users in your whitelist can access the app
4. Unauthorized users see "Access Denied" page

---

## πŸ”’ Authentication Details

### How It Works

1. **Hugging Face OAuth**: Users must sign in with their HF account
2. **Whitelist Check**: `auth.py` checks username against allowed list
3. **Protected Endpoints**: Upload, invoice retrieval, stats require authentication
4. **Public Access**: Only the login page and static files are public

### Whitelist Format

**allowed_users.txt** (supports BOTH usernames AND emails):

```

# Hugging Face usernames (if you know them)

johndoe

janedoe



# OR email addresses (if you don't know usernames)

john@company.com

jane@company.com



# You can mix both!

```



**Environment Variables**:

```

# Option 1: Usernames

ALLOWED_USERS=johndoe,janedoe,companyuser1



# Option 2: Emails

ALLOWED_EMAILS=john@company.com,jane@company.com



# Or use both!

```



πŸ’‘ **Don't know the username?** Just use their email address! The system checks both.



### Protected Endpoints



βœ… `POST /upload` - Upload invoice  

βœ… `GET /invoices` - List invoices  

βœ… `GET /invoices/{id}` - Get invoice details  

βœ… `GET /invoices/{id}/debug` - Debug data  

βœ… `DELETE /invoices/{id}` - Delete invoice  

βœ… `GET /stats` - Statistics  



❌ `GET /` - Public (serves login page)  

❌ `GET /static/*` - Public (CSS, JS, logo)



### Local Development



Authentication is **disabled** when running locally (not in HF Space):

```bash

python app.py  # No auth required on localhost:7860

```



To test authentication locally, set:

```bash

export SPACE_ID=test

export ALLOWED_USERS=your_hf_username

```



---



## πŸ“ˆ Performance



- **Processing Time**: 2-4 seconds per invoice

- **Accuracy**: 99%+ text extraction

- **Cost**: ~$0.002 per invoice

- **Free Tier**: 1,000 invoices/month FREE



---



## πŸ†š vs Receipt OCR



| Feature | Receipt OCR | Invoice OCR |

|---------|-------------|-------------|

| **Use Case** | Retail receipts | B2B invoices |

| **Line Items** | Simple items | Item codes + details |

| **Complexity** | Low | Medium-High |

| **Tokens** | ~2,000 | ~2,800 |

| **Cost** | $0.001881 | $0.002020 |

| **Fields** | 15-20 | 30-40 |



---



## πŸ› οΈ Technology Stack



- **OCR**: Google Document AI OCR

- **AI**: Gemini 2.0 Flash (image + text)

- **Backend**: FastAPI + SQLAlchemy

- **Database**: SQLite

- **Frontend**: HTML5 + Vanilla JS

- **Authentication**: Hugging Face OAuth + Whitelist

- **Deployment**: Docker + Hugging Face Spaces



---



## πŸ“ License



MIT License



---



**Built for accurate B2B invoice processing** πŸš€