This commit is contained in:
Yaojia Wang
2026-01-13 00:10:27 +01:00
parent 1b7c61cdd8
commit b26fd61852
43 changed files with 7751 additions and 578 deletions

616
src/web/app.py Normal file
View File

@@ -0,0 +1,616 @@
"""
FastAPI Application Factory
Creates and configures the FastAPI application.
"""
from __future__ import annotations
import logging
from contextlib import asynccontextmanager
from pathlib import Path
from typing import TYPE_CHECKING
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
from .config import AppConfig, default_config
from .routes import create_api_router
from .services import InferenceService
if TYPE_CHECKING:
from collections.abc import AsyncGenerator
logger = logging.getLogger(__name__)
def create_app(config: AppConfig | None = None) -> FastAPI:
"""
Create and configure FastAPI application.
Args:
config: Application configuration. Uses default if not provided.
Returns:
Configured FastAPI application
"""
config = config or default_config
# Create inference service
inference_service = InferenceService(
model_config=config.model,
storage_config=config.storage,
)
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
"""Application lifespan manager."""
logger.info("Starting Invoice Inference API...")
# Initialize inference service on startup
try:
inference_service.initialize()
logger.info("Inference service ready")
except Exception as e:
logger.error(f"Failed to initialize inference service: {e}")
# Continue anyway - service will retry on first request
yield
logger.info("Shutting down Invoice Inference API...")
# Create FastAPI app
app = FastAPI(
title="Invoice Field Extraction API",
description="""
REST API for extracting fields from Swedish invoices.
## Features
- YOLO-based field detection
- OCR text extraction
- Field normalization and validation
- Visualization of detections
## Supported Fields
- InvoiceNumber
- InvoiceDate
- InvoiceDueDate
- OCR (reference number)
- Bankgiro
- Plusgiro
- Amount
""",
version="1.0.0",
lifespan=lifespan,
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount static files for results
config.storage.result_dir.mkdir(parents=True, exist_ok=True)
app.mount(
"/static/results",
StaticFiles(directory=str(config.storage.result_dir)),
name="results",
)
# Include API routes
api_router = create_api_router(inference_service, config.storage)
app.include_router(api_router)
# Root endpoint - serve HTML UI
@app.get("/", response_class=HTMLResponse)
async def root() -> str:
"""Serve the web UI."""
return get_html_ui()
return app
def get_html_ui() -> str:
"""Generate HTML UI for the web application."""
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Invoice Field Extraction</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
header {
text-align: center;
color: white;
margin-bottom: 30px;
}
header h1 {
font-size: 2.5rem;
margin-bottom: 10px;
}
header p {
opacity: 0.9;
font-size: 1.1rem;
}
.main-content {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
@media (max-width: 900px) {
.main-content {
grid-template-columns: 1fr;
}
}
.card {
background: white;
border-radius: 16px;
padding: 24px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}
.card h2 {
color: #333;
margin-bottom: 20px;
font-size: 1.3rem;
display: flex;
align-items: center;
gap: 10px;
}
.upload-area {
border: 3px dashed #ddd;
border-radius: 12px;
padding: 40px;
text-align: center;
cursor: pointer;
transition: all 0.3s;
background: #fafafa;
}
.upload-area:hover, .upload-area.dragover {
border-color: #667eea;
background: #f0f4ff;
}
.upload-area.has-file {
border-color: #10b981;
background: #ecfdf5;
}
.upload-icon {
font-size: 48px;
margin-bottom: 15px;
}
.upload-area p {
color: #666;
margin-bottom: 10px;
}
.upload-area small {
color: #999;
}
#file-input {
display: none;
}
.file-name {
margin-top: 15px;
padding: 10px 15px;
background: #e0f2fe;
border-radius: 8px;
color: #0369a1;
font-weight: 500;
}
.btn {
display: inline-block;
padding: 14px 28px;
border: none;
border-radius: 10px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.btn-primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
width: 100%;
margin-top: 20px;
}
.btn-primary:hover:not(:disabled) {
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
}
.btn-primary:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.loading {
display: none;
text-align: center;
padding: 20px;
}
.loading.active {
display: block;
}
.spinner {
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top: 4px solid #667eea;
border-radius: 50%;
animation: spin 1s linear infinite;
margin: 0 auto 15px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.results {
display: none;
}
.results.active {
display: block;
}
.result-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
padding-bottom: 15px;
border-bottom: 2px solid #eee;
}
.result-status {
padding: 6px 12px;
border-radius: 20px;
font-size: 0.85rem;
font-weight: 600;
}
.result-status.success {
background: #dcfce7;
color: #166534;
}
.result-status.partial {
background: #fef3c7;
color: #92400e;
}
.result-status.error {
background: #fee2e2;
color: #991b1b;
}
.fields-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 12px;
}
.field-item {
padding: 12px;
background: #f8fafc;
border-radius: 10px;
border-left: 4px solid #667eea;
}
.field-item label {
display: block;
font-size: 0.75rem;
color: #64748b;
text-transform: uppercase;
letter-spacing: 0.5px;
margin-bottom: 4px;
}
.field-item .value {
font-size: 1.1rem;
font-weight: 600;
color: #1e293b;
}
.field-item .confidence {
font-size: 0.75rem;
color: #10b981;
margin-top: 2px;
}
.visualization {
margin-top: 20px;
}
.visualization img {
width: 100%;
border-radius: 12px;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}
.processing-time {
text-align: center;
color: #64748b;
font-size: 0.9rem;
margin-top: 15px;
}
.error-message {
background: #fee2e2;
color: #991b1b;
padding: 15px;
border-radius: 10px;
margin-top: 15px;
}
footer {
text-align: center;
color: white;
opacity: 0.8;
margin-top: 30px;
font-size: 0.9rem;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>📄 Invoice Field Extraction</h1>
<p>Upload a Swedish invoice (PDF or image) to extract fields automatically</p>
</header>
<div class="main-content">
<div class="card">
<h2>📤 Upload Document</h2>
<div class="upload-area" id="upload-area">
<div class="upload-icon">📁</div>
<p>Drag & drop your file here</p>
<p>or <strong>click to browse</strong></p>
<small>Supports PDF, PNG, JPG (max 50MB)</small>
<input type="file" id="file-input" accept=".pdf,.png,.jpg,.jpeg">
<div class="file-name" id="file-name" style="display: none;"></div>
</div>
<button class="btn btn-primary" id="submit-btn" disabled>
🚀 Extract Fields
</button>
<div class="loading" id="loading">
<div class="spinner"></div>
<p>Processing document...</p>
</div>
</div>
<div class="card">
<h2>📊 Extraction Results</h2>
<div id="placeholder" style="text-align: center; padding: 40px; color: #999;">
<div style="font-size: 64px; margin-bottom: 15px;">🔍</div>
<p>Upload a document to see extraction results</p>
</div>
<div class="results" id="results">
<div class="result-header">
<span>Document: <strong id="doc-id"></strong></span>
<span class="result-status" id="result-status"></span>
</div>
<div class="fields-grid" id="fields-grid"></div>
<div class="processing-time" id="processing-time"></div>
<div class="error-message" id="error-message" style="display: none;"></div>
<div class="visualization" id="visualization" style="display: none;">
<h3 style="margin-bottom: 10px; color: #333;">🎯 Detection Visualization</h3>
<img id="viz-image" src="" alt="Detection visualization">
</div>
</div>
</div>
</div>
<footer>
<p>Powered by ColaCoder</p>
</footer>
</div>
<script>
const uploadArea = document.getElementById('upload-area');
const fileInput = document.getElementById('file-input');
const fileName = document.getElementById('file-name');
const submitBtn = document.getElementById('submit-btn');
const loading = document.getElementById('loading');
const placeholder = document.getElementById('placeholder');
const results = document.getElementById('results');
let selectedFile = null;
// Drag and drop handlers
uploadArea.addEventListener('click', () => fileInput.click());
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('dragover');
const files = e.dataTransfer.files;
if (files.length > 0) {
handleFile(files[0]);
}
});
fileInput.addEventListener('change', (e) => {
if (e.target.files.length > 0) {
handleFile(e.target.files[0]);
}
});
function handleFile(file) {
const validTypes = ['.pdf', '.png', '.jpg', '.jpeg'];
const ext = '.' + file.name.split('.').pop().toLowerCase();
if (!validTypes.includes(ext)) {
alert('Please upload a PDF, PNG, or JPG file.');
return;
}
selectedFile = file;
fileName.textContent = `📎 ${file.name}`;
fileName.style.display = 'block';
uploadArea.classList.add('has-file');
submitBtn.disabled = false;
}
submitBtn.addEventListener('click', async () => {
if (!selectedFile) return;
// Show loading
submitBtn.disabled = true;
loading.classList.add('active');
placeholder.style.display = 'none';
results.classList.remove('active');
try {
const formData = new FormData();
formData.append('file', selectedFile);
const response = await fetch('/api/v1/infer', {
method: 'POST',
body: formData,
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.detail || 'Processing failed');
}
displayResults(data);
} catch (error) {
console.error('Error:', error);
document.getElementById('error-message').textContent = error.message;
document.getElementById('error-message').style.display = 'block';
results.classList.add('active');
} finally {
loading.classList.remove('active');
submitBtn.disabled = false;
}
});
function displayResults(data) {
const result = data.result;
// Document ID
document.getElementById('doc-id').textContent = result.document_id;
// Status
const statusEl = document.getElementById('result-status');
statusEl.textContent = result.success ? 'Success' : 'Partial';
statusEl.className = 'result-status ' + (result.success ? 'success' : 'partial');
// Fields
const fieldsGrid = document.getElementById('fields-grid');
fieldsGrid.innerHTML = '';
const fieldOrder = ['InvoiceNumber', 'InvoiceDate', 'InvoiceDueDate', 'OCR', 'Amount', 'Bankgiro', 'Plusgiro'];
fieldOrder.forEach(field => {
const value = result.fields[field];
const confidence = result.confidence[field];
if (value !== null && value !== undefined) {
const fieldDiv = document.createElement('div');
fieldDiv.className = 'field-item';
fieldDiv.innerHTML = `
<label>${formatFieldName(field)}</label>
<div class="value">${value}</div>
${confidence ? `<div class="confidence">✓ ${(confidence * 100).toFixed(1)}% confident</div>` : ''}
`;
fieldsGrid.appendChild(fieldDiv);
}
});
// Processing time
document.getElementById('processing-time').textContent =
`⏱️ Processed in ${result.processing_time_ms.toFixed(0)}ms`;
// Visualization
if (result.visualization_url) {
const vizDiv = document.getElementById('visualization');
const vizImg = document.getElementById('viz-image');
vizImg.src = result.visualization_url;
vizDiv.style.display = 'block';
}
// Errors
if (result.errors && result.errors.length > 0) {
document.getElementById('error-message').textContent = result.errors.join(', ');
document.getElementById('error-message').style.display = 'block';
} else {
document.getElementById('error-message').style.display = 'none';
}
results.classList.add('active');
}
function formatFieldName(name) {
return name.replace(/([A-Z])/g, ' $1').trim();
}
</script>
</body>
</html>
"""