chefcodeocr / README.md
Mariem-Daha's picture
Upload 22 files
8c33e8e verified
|
raw
history blame
8 kB
---
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** πŸš€