yuvaraj commited on
Commit ·
2eb92c2
1
Parent(s): 2a7abc9
Add interactive demo UI, update for Hugging Face Spaces deployment
Browse files- .gitignore +39 -0
- README.md +134 -3
- main.py +462 -29
.gitignore
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Virtual Environment
|
| 2 |
+
venv/
|
| 3 |
+
env/
|
| 4 |
+
ENV/
|
| 5 |
+
.venv
|
| 6 |
+
|
| 7 |
+
# Python
|
| 8 |
+
__pycache__/
|
| 9 |
+
*.py[cod]
|
| 10 |
+
*$py.class
|
| 11 |
+
*.so
|
| 12 |
+
.Python
|
| 13 |
+
*.egg-info/
|
| 14 |
+
dist/
|
| 15 |
+
build/
|
| 16 |
+
|
| 17 |
+
# IDE
|
| 18 |
+
.vscode/
|
| 19 |
+
.idea/
|
| 20 |
+
*.swp
|
| 21 |
+
*.swo
|
| 22 |
+
*~
|
| 23 |
+
|
| 24 |
+
# Environment variables
|
| 25 |
+
.env
|
| 26 |
+
.env.local
|
| 27 |
+
|
| 28 |
+
# OS
|
| 29 |
+
.DS_Store
|
| 30 |
+
Thumbs.db
|
| 31 |
+
|
| 32 |
+
# Logs
|
| 33 |
+
*.log
|
| 34 |
+
|
| 35 |
+
# Temporary files
|
| 36 |
+
*.tmp
|
| 37 |
+
temp/
|
| 38 |
+
tmp/
|
| 39 |
+
|
README.md
CHANGED
|
@@ -1,11 +1,142 @@
|
|
| 1 |
---
|
| 2 |
-
title: PreOCR
|
| 3 |
emoji: 👁
|
| 4 |
colorFrom: indigo
|
|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|
| 11 |
+
# PreOCR Document Detection and Extraction Service
|
| 12 |
+
|
| 13 |
+
A FastAPI service for detecting OCR requirements and extracting structured data from documents using the PreOCR library.
|
| 14 |
+
|
| 15 |
+
## Features
|
| 16 |
+
|
| 17 |
+
- **Need OCR Detection**: Detect whether documents need OCR processing using `needs_ocr`
|
| 18 |
+
- **Native Data Extraction**: Extract structured data from machine-readable documents using `extract_native_data`
|
| 19 |
+
- **Interactive Demo UI**: Test endpoints directly from the browser
|
| 20 |
+
- **Batch Processing**: Support for processing multiple documents
|
| 21 |
+
- **Multiple Output Formats**: Support for pydantic, JSON, and markdown formats
|
| 22 |
+
|
| 23 |
+
## Setup
|
| 24 |
+
|
| 25 |
+
### 1. Create Virtual Environment
|
| 26 |
+
|
| 27 |
+
```bash
|
| 28 |
+
python3 -m venv venv
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
### 2. Activate Virtual Environment
|
| 32 |
+
|
| 33 |
+
**On Linux/Mac:**
|
| 34 |
+
```bash
|
| 35 |
+
source venv/bin/activate
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
**On Windows:**
|
| 39 |
+
```bash
|
| 40 |
+
venv\Scripts\activate
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
### 3. Install Dependencies
|
| 44 |
+
|
| 45 |
+
```bash
|
| 46 |
+
pip install --upgrade pip
|
| 47 |
+
pip install -r requirements.txt
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
## Running the Service
|
| 51 |
+
|
| 52 |
+
### Start the Server
|
| 53 |
+
|
| 54 |
+
```bash
|
| 55 |
+
# Activate virtual environment first
|
| 56 |
+
source venv/bin/activate
|
| 57 |
+
|
| 58 |
+
# Run the service
|
| 59 |
+
python main.py
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
Or using uvicorn directly:
|
| 63 |
+
|
| 64 |
+
```bash
|
| 65 |
+
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
### Access the Service
|
| 69 |
+
|
| 70 |
+
- **Web UI**: http://localhost:8000/
|
| 71 |
+
- **API Documentation**: http://localhost:8000/docs
|
| 72 |
+
- **ReDoc**: http://localhost:8000/redoc
|
| 73 |
+
- **OpenAPI Spec**: http://localhost:8000/openapi.json
|
| 74 |
+
|
| 75 |
+
## API Endpoints
|
| 76 |
+
|
| 77 |
+
### POST `/need_ocr`
|
| 78 |
+
|
| 79 |
+
Detect whether documents need OCR processing.
|
| 80 |
+
|
| 81 |
+
**Request:**
|
| 82 |
+
```json
|
| 83 |
+
{
|
| 84 |
+
"urls": "https://example.com/document.pdf"
|
| 85 |
+
}
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
**Response:**
|
| 89 |
+
```json
|
| 90 |
+
{
|
| 91 |
+
"success": true,
|
| 92 |
+
"results": [
|
| 93 |
+
{
|
| 94 |
+
"url": "https://example.com/document.pdf",
|
| 95 |
+
"needs_ocr": false,
|
| 96 |
+
"reason": "machine_readable",
|
| 97 |
+
"pages": [...]
|
| 98 |
+
}
|
| 99 |
+
]
|
| 100 |
+
}
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
### POST `/extract-native-data`
|
| 104 |
+
|
| 105 |
+
Extract structured data from machine-readable documents.
|
| 106 |
+
|
| 107 |
+
**Request:**
|
| 108 |
+
```json
|
| 109 |
+
{
|
| 110 |
+
"urls": "https://example.com/document.pdf",
|
| 111 |
+
"output_format": "pydantic"
|
| 112 |
+
}
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
**Response:**
|
| 116 |
+
```json
|
| 117 |
+
{
|
| 118 |
+
"success": true,
|
| 119 |
+
"results": [
|
| 120 |
+
{
|
| 121 |
+
"url": "https://example.com/document.pdf",
|
| 122 |
+
"needs_ocr": false,
|
| 123 |
+
"reason": "machine_readable",
|
| 124 |
+
"pages": [...],
|
| 125 |
+
"extracted_data": {
|
| 126 |
+
"elements": [...],
|
| 127 |
+
"tables": [...],
|
| 128 |
+
"forms": [...],
|
| 129 |
+
"images": [...]
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
]
|
| 133 |
+
}
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
## Configuration
|
| 137 |
+
|
| 138 |
+
The service can be configured using environment variables or a `.env` file. See `config.py` for available settings.
|
| 139 |
+
|
| 140 |
+
## License
|
| 141 |
+
|
| 142 |
+
Apache 2.0
|
main.py
CHANGED
|
@@ -7,7 +7,7 @@ from contextlib import asynccontextmanager
|
|
| 7 |
from typing import List
|
| 8 |
|
| 9 |
from fastapi import FastAPI, HTTPException, Request
|
| 10 |
-
from fastapi.responses import JSONResponse
|
| 11 |
from starlette.middleware.base import BaseHTTPMiddleware
|
| 12 |
|
| 13 |
from config import get_settings
|
|
@@ -112,24 +112,467 @@ async def health_check():
|
|
| 112 |
return {"status": "healthy", "service": "preocr"}
|
| 113 |
|
| 114 |
|
| 115 |
-
@app.get("/")
|
| 116 |
async def root():
|
| 117 |
-
"""Root endpoint."""
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
|
| 132 |
-
@app.post("/
|
| 133 |
async def detect_ocr(request: DetectRequest):
|
| 134 |
"""
|
| 135 |
Detect whether documents need OCR processing.
|
|
@@ -219,20 +662,10 @@ async def detect_ocr(request: DetectRequest):
|
|
| 219 |
)
|
| 220 |
|
| 221 |
|
| 222 |
-
@app.post("/
|
| 223 |
-
async def
|
| 224 |
"""
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
This endpoint performs OCR detection AND extracts structured data (tables, forms,
|
| 228 |
-
images, text elements) if the document is machine-readable. Documents that need
|
| 229 |
-
OCR will only return detection results without extraction.
|
| 230 |
-
|
| 231 |
-
Args:
|
| 232 |
-
request: AnalyzeRequest containing single URL or list of URLs
|
| 233 |
-
|
| 234 |
-
Returns:
|
| 235 |
-
AnalyzeResponse with detection results and extracted data (if readable)
|
| 236 |
"""
|
| 237 |
import time
|
| 238 |
start_time = time.time()
|
|
|
|
| 7 |
from typing import List
|
| 8 |
|
| 9 |
from fastapi import FastAPI, HTTPException, Request
|
| 10 |
+
from fastapi.responses import JSONResponse, HTMLResponse
|
| 11 |
from starlette.middleware.base import BaseHTTPMiddleware
|
| 12 |
|
| 13 |
from config import get_settings
|
|
|
|
| 112 |
return {"status": "healthy", "service": "preocr"}
|
| 113 |
|
| 114 |
|
| 115 |
+
@app.get("/", response_class=HTMLResponse)
|
| 116 |
async def root():
|
| 117 |
+
"""Root endpoint with UI."""
|
| 118 |
+
settings = get_settings()
|
| 119 |
+
port = int(os.getenv("PORT", settings.PORT))
|
| 120 |
+
base_url = f"http://{settings.HOST}:{port}"
|
| 121 |
+
|
| 122 |
+
html_content = f"""
|
| 123 |
+
<!DOCTYPE html>
|
| 124 |
+
<html lang="en">
|
| 125 |
+
<head>
|
| 126 |
+
<meta charset="UTF-8">
|
| 127 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 128 |
+
<title>PreOCR Document Detection and Extraction Service</title>
|
| 129 |
+
<style>
|
| 130 |
+
* {{
|
| 131 |
+
margin: 0;
|
| 132 |
+
padding: 0;
|
| 133 |
+
box-sizing: border-box;
|
| 134 |
+
}}
|
| 135 |
+
body {{
|
| 136 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
| 137 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 138 |
+
min-height: 100vh;
|
| 139 |
+
display: flex;
|
| 140 |
+
justify-content: center;
|
| 141 |
+
align-items: center;
|
| 142 |
+
padding: 20px;
|
| 143 |
+
}}
|
| 144 |
+
.container {{
|
| 145 |
+
background: white;
|
| 146 |
+
border-radius: 20px;
|
| 147 |
+
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
|
| 148 |
+
max-width: 900px;
|
| 149 |
+
width: 100%;
|
| 150 |
+
padding: 40px;
|
| 151 |
+
}}
|
| 152 |
+
.header {{
|
| 153 |
+
text-align: center;
|
| 154 |
+
margin-bottom: 40px;
|
| 155 |
+
}}
|
| 156 |
+
.header h1 {{
|
| 157 |
+
color: #333;
|
| 158 |
+
font-size: 2.5em;
|
| 159 |
+
margin-bottom: 10px;
|
| 160 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 161 |
+
-webkit-background-clip: text;
|
| 162 |
+
-webkit-text-fill-color: transparent;
|
| 163 |
+
background-clip: text;
|
| 164 |
+
}}
|
| 165 |
+
.header p {{
|
| 166 |
+
color: #666;
|
| 167 |
+
font-size: 1.1em;
|
| 168 |
+
}}
|
| 169 |
+
.version {{
|
| 170 |
+
display: inline-block;
|
| 171 |
+
background: #f0f0f0;
|
| 172 |
+
padding: 5px 15px;
|
| 173 |
+
border-radius: 20px;
|
| 174 |
+
font-size: 0.9em;
|
| 175 |
+
color: #666;
|
| 176 |
+
margin-top: 10px;
|
| 177 |
+
}}
|
| 178 |
+
.endpoints {{
|
| 179 |
+
display: grid;
|
| 180 |
+
gap: 20px;
|
| 181 |
+
margin-top: 30px;
|
| 182 |
+
}}
|
| 183 |
+
.endpoint-card {{
|
| 184 |
+
background: #f8f9fa;
|
| 185 |
+
border-radius: 12px;
|
| 186 |
+
padding: 25px;
|
| 187 |
+
border-left: 4px solid #667eea;
|
| 188 |
+
transition: transform 0.2s, box-shadow 0.2s;
|
| 189 |
+
}}
|
| 190 |
+
.endpoint-card:hover {{
|
| 191 |
+
transform: translateY(-2px);
|
| 192 |
+
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
|
| 193 |
+
}}
|
| 194 |
+
.endpoint-header {{
|
| 195 |
+
display: flex;
|
| 196 |
+
align-items: center;
|
| 197 |
+
gap: 15px;
|
| 198 |
+
margin-bottom: 15px;
|
| 199 |
+
}}
|
| 200 |
+
.method {{
|
| 201 |
+
background: #667eea;
|
| 202 |
+
color: white;
|
| 203 |
+
padding: 5px 12px;
|
| 204 |
+
border-radius: 6px;
|
| 205 |
+
font-weight: bold;
|
| 206 |
+
font-size: 0.85em;
|
| 207 |
+
text-transform: uppercase;
|
| 208 |
+
}}
|
| 209 |
+
.method.get {{
|
| 210 |
+
background: #28a745;
|
| 211 |
+
}}
|
| 212 |
+
.method.post {{
|
| 213 |
+
background: #007bff;
|
| 214 |
+
}}
|
| 215 |
+
.path {{
|
| 216 |
+
font-family: 'Courier New', monospace;
|
| 217 |
+
font-size: 1.1em;
|
| 218 |
+
color: #333;
|
| 219 |
+
font-weight: 600;
|
| 220 |
+
}}
|
| 221 |
+
.description {{
|
| 222 |
+
color: #666;
|
| 223 |
+
line-height: 1.6;
|
| 224 |
+
margin-top: 10px;
|
| 225 |
+
}}
|
| 226 |
+
.links {{
|
| 227 |
+
margin-top: 30px;
|
| 228 |
+
text-align: center;
|
| 229 |
+
padding-top: 30px;
|
| 230 |
+
border-top: 2px solid #e9ecef;
|
| 231 |
+
}}
|
| 232 |
+
.links a {{
|
| 233 |
+
display: inline-block;
|
| 234 |
+
margin: 0 10px;
|
| 235 |
+
padding: 12px 24px;
|
| 236 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 237 |
+
color: white;
|
| 238 |
+
text-decoration: none;
|
| 239 |
+
border-radius: 8px;
|
| 240 |
+
font-weight: 600;
|
| 241 |
+
transition: transform 0.2s, box-shadow 0.2s;
|
| 242 |
+
}}
|
| 243 |
+
.links a:hover {{
|
| 244 |
+
transform: translateY(-2px);
|
| 245 |
+
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
|
| 246 |
+
}}
|
| 247 |
+
.status {{
|
| 248 |
+
display: inline-block;
|
| 249 |
+
background: #d4edda;
|
| 250 |
+
color: #155724;
|
| 251 |
+
padding: 8px 16px;
|
| 252 |
+
border-radius: 20px;
|
| 253 |
+
font-size: 0.9em;
|
| 254 |
+
margin-top: 20px;
|
| 255 |
+
}}
|
| 256 |
+
.demo-section {{
|
| 257 |
+
margin-top: 40px;
|
| 258 |
+
padding-top: 30px;
|
| 259 |
+
border-top: 2px solid #e9ecef;
|
| 260 |
+
}}
|
| 261 |
+
.demo-title {{
|
| 262 |
+
font-size: 1.5em;
|
| 263 |
+
color: #333;
|
| 264 |
+
margin-bottom: 20px;
|
| 265 |
+
text-align: center;
|
| 266 |
+
}}
|
| 267 |
+
.demo-tabs {{
|
| 268 |
+
display: flex;
|
| 269 |
+
gap: 10px;
|
| 270 |
+
margin-bottom: 20px;
|
| 271 |
+
border-bottom: 2px solid #e9ecef;
|
| 272 |
+
}}
|
| 273 |
+
.demo-tab {{
|
| 274 |
+
padding: 12px 24px;
|
| 275 |
+
background: transparent;
|
| 276 |
+
border: none;
|
| 277 |
+
cursor: pointer;
|
| 278 |
+
font-size: 1em;
|
| 279 |
+
color: #666;
|
| 280 |
+
border-bottom: 3px solid transparent;
|
| 281 |
+
transition: all 0.3s;
|
| 282 |
+
}}
|
| 283 |
+
.demo-tab.active {{
|
| 284 |
+
color: #667eea;
|
| 285 |
+
border-bottom-color: #667eea;
|
| 286 |
+
font-weight: 600;
|
| 287 |
+
}}
|
| 288 |
+
.demo-content {{
|
| 289 |
+
display: none;
|
| 290 |
+
}}
|
| 291 |
+
.demo-content.active {{
|
| 292 |
+
display: block;
|
| 293 |
+
}}
|
| 294 |
+
.demo-form {{
|
| 295 |
+
background: #f8f9fa;
|
| 296 |
+
padding: 25px;
|
| 297 |
+
border-radius: 12px;
|
| 298 |
+
}}
|
| 299 |
+
.form-group {{
|
| 300 |
+
margin-bottom: 20px;
|
| 301 |
+
}}
|
| 302 |
+
.form-group label {{
|
| 303 |
+
display: block;
|
| 304 |
+
margin-bottom: 8px;
|
| 305 |
+
color: #333;
|
| 306 |
+
font-weight: 600;
|
| 307 |
+
}}
|
| 308 |
+
.form-group input, .form-group select, .form-group textarea {{
|
| 309 |
+
width: 100%;
|
| 310 |
+
padding: 12px;
|
| 311 |
+
border: 2px solid #e9ecef;
|
| 312 |
+
border-radius: 8px;
|
| 313 |
+
font-size: 1em;
|
| 314 |
+
font-family: inherit;
|
| 315 |
+
transition: border-color 0.3s;
|
| 316 |
+
}}
|
| 317 |
+
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {{
|
| 318 |
+
outline: none;
|
| 319 |
+
border-color: #667eea;
|
| 320 |
+
}}
|
| 321 |
+
.form-group textarea {{
|
| 322 |
+
min-height: 100px;
|
| 323 |
+
resize: vertical;
|
| 324 |
+
font-family: 'Courier New', monospace;
|
| 325 |
+
}}
|
| 326 |
+
.btn {{
|
| 327 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 328 |
+
color: white;
|
| 329 |
+
border: none;
|
| 330 |
+
padding: 12px 30px;
|
| 331 |
+
border-radius: 8px;
|
| 332 |
+
font-size: 1em;
|
| 333 |
+
font-weight: 600;
|
| 334 |
+
cursor: pointer;
|
| 335 |
+
transition: transform 0.2s, box-shadow 0.2s;
|
| 336 |
+
width: 100%;
|
| 337 |
+
}}
|
| 338 |
+
.btn:hover {{
|
| 339 |
+
transform: translateY(-2px);
|
| 340 |
+
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
|
| 341 |
+
}}
|
| 342 |
+
.btn:disabled {{
|
| 343 |
+
opacity: 0.6;
|
| 344 |
+
cursor: not-allowed;
|
| 345 |
+
transform: none;
|
| 346 |
+
}}
|
| 347 |
+
.result-box {{
|
| 348 |
+
margin-top: 20px;
|
| 349 |
+
padding: 20px;
|
| 350 |
+
background: #f8f9fa;
|
| 351 |
+
border-radius: 8px;
|
| 352 |
+
border-left: 4px solid #667eea;
|
| 353 |
+
display: none;
|
| 354 |
+
}}
|
| 355 |
+
.result-box.show {{
|
| 356 |
+
display: block;
|
| 357 |
+
}}
|
| 358 |
+
.result-box pre {{
|
| 359 |
+
background: #2d2d2d;
|
| 360 |
+
color: #f8f8f2;
|
| 361 |
+
padding: 15px;
|
| 362 |
+
border-radius: 6px;
|
| 363 |
+
overflow-x: auto;
|
| 364 |
+
font-size: 0.9em;
|
| 365 |
+
line-height: 1.5;
|
| 366 |
+
}}
|
| 367 |
+
.loading {{
|
| 368 |
+
display: none;
|
| 369 |
+
text-align: center;
|
| 370 |
+
padding: 20px;
|
| 371 |
+
color: #667eea;
|
| 372 |
+
}}
|
| 373 |
+
.loading.show {{
|
| 374 |
+
display: block;
|
| 375 |
+
}}
|
| 376 |
+
.error {{
|
| 377 |
+
background: #f8d7da;
|
| 378 |
+
color: #721c24;
|
| 379 |
+
padding: 12px;
|
| 380 |
+
border-radius: 6px;
|
| 381 |
+
margin-top: 10px;
|
| 382 |
+
}}
|
| 383 |
+
.example-url {{
|
| 384 |
+
font-size: 0.85em;
|
| 385 |
+
color: #666;
|
| 386 |
+
margin-top: 5px;
|
| 387 |
+
font-style: italic;
|
| 388 |
+
}}
|
| 389 |
+
</style>
|
| 390 |
+
</head>
|
| 391 |
+
<body>
|
| 392 |
+
<div class="container">
|
| 393 |
+
<div class="header">
|
| 394 |
+
<h1>PreOCR Service</h1>
|
| 395 |
+
<p>Document Detection and Extraction Service</p>
|
| 396 |
+
<span class="version">Version 1.0.0</span>
|
| 397 |
+
</div>
|
| 398 |
+
|
| 399 |
+
<div class="endpoints">
|
| 400 |
+
<div class="endpoint-card">
|
| 401 |
+
<div class="endpoint-header">
|
| 402 |
+
<span class="method get">GET</span>
|
| 403 |
+
<span class="path">/health</span>
|
| 404 |
+
</div>
|
| 405 |
+
<div class="description">
|
| 406 |
+
Health check endpoint to verify service status
|
| 407 |
+
</div>
|
| 408 |
+
</div>
|
| 409 |
+
|
| 410 |
+
<div class="endpoint-card">
|
| 411 |
+
<div class="endpoint-header">
|
| 412 |
+
<span class="method post">POST</span>
|
| 413 |
+
<span class="path">/need_ocr</span>
|
| 414 |
+
</div>
|
| 415 |
+
<div class="description">
|
| 416 |
+
Detect whether documents need OCR processing using the needs_ocr function.
|
| 417 |
+
Returns detection results with page-wise information.
|
| 418 |
+
</div>
|
| 419 |
+
</div>
|
| 420 |
+
|
| 421 |
+
<div class="endpoint-card">
|
| 422 |
+
<div class="endpoint-header">
|
| 423 |
+
<span class="method post">POST</span>
|
| 424 |
+
<span class="path">/extract-native-data</span>
|
| 425 |
+
</div>
|
| 426 |
+
<div class="description">
|
| 427 |
+
Extract structured data from machine-readable documents using extract_native_data.
|
| 428 |
+
Performs OCR detection first, then extracts data if the document is machine-readable.
|
| 429 |
+
</div>
|
| 430 |
+
</div>
|
| 431 |
+
</div>
|
| 432 |
+
|
| 433 |
+
<div class="demo-section">
|
| 434 |
+
<h2 class="demo-title">🚀 Interactive Demo</h2>
|
| 435 |
+
<div class="demo-tabs">
|
| 436 |
+
<button class="demo-tab active" onclick="switchTab('need_ocr')">Need OCR</button>
|
| 437 |
+
<button class="demo-tab" onclick="switchTab('extract')">Extract Data</button>
|
| 438 |
+
</div>
|
| 439 |
+
|
| 440 |
+
<div id="demo-need_ocr" class="demo-content active">
|
| 441 |
+
<div class="demo-form">
|
| 442 |
+
<div class="form-group">
|
| 443 |
+
<label for="need_ocr_url">Document URL</label>
|
| 444 |
+
<input type="text" id="need_ocr_url" placeholder="https://example.com/document.pdf" value="">
|
| 445 |
+
<div class="example-url">Example: https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf</div>
|
| 446 |
+
</div>
|
| 447 |
+
<button class="btn" onclick="testNeedOCR()">Test Need OCR</button>
|
| 448 |
+
<div class="loading" id="need_ocr_loading">Processing... Please wait</div>
|
| 449 |
+
<div class="result-box" id="need_ocr_result"></div>
|
| 450 |
+
</div>
|
| 451 |
+
</div>
|
| 452 |
+
|
| 453 |
+
<div id="demo-extract" class="demo-content">
|
| 454 |
+
<div class="demo-form">
|
| 455 |
+
<div class="form-group">
|
| 456 |
+
<label for="extract_url">Document URL</label>
|
| 457 |
+
<input type="text" id="extract_url" placeholder="https://example.com/document.pdf" value="">
|
| 458 |
+
<div class="example-url">Example: https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf</div>
|
| 459 |
+
</div>
|
| 460 |
+
<div class="form-group">
|
| 461 |
+
<label for="output_format">Output Format</label>
|
| 462 |
+
<select id="output_format">
|
| 463 |
+
<option value="pydantic">Pydantic (default)</option>
|
| 464 |
+
<option value="json">JSON</option>
|
| 465 |
+
<option value="markdown">Markdown</option>
|
| 466 |
+
</select>
|
| 467 |
+
</div>
|
| 468 |
+
<button class="btn" onclick="testExtract()">Extract Native Data</button>
|
| 469 |
+
<div class="loading" id="extract_loading">Processing... Please wait</div>
|
| 470 |
+
<div class="result-box" id="extract_result"></div>
|
| 471 |
+
</div>
|
| 472 |
+
</div>
|
| 473 |
+
</div>
|
| 474 |
+
|
| 475 |
+
<div class="links">
|
| 476 |
+
<a href="{base_url}/docs" target="_blank">API Documentation</a>
|
| 477 |
+
<a href="{base_url}/redoc" target="_blank">ReDoc</a>
|
| 478 |
+
<a href="{base_url}/openapi.json" target="_blank">OpenAPI Spec</a>
|
| 479 |
+
</div>
|
| 480 |
+
|
| 481 |
+
<div style="text-align: center;">
|
| 482 |
+
<div class="status">✓ Service is running</div>
|
| 483 |
+
</div>
|
| 484 |
+
</div>
|
| 485 |
+
|
| 486 |
+
<script>
|
| 487 |
+
function switchTab(tabName) {{
|
| 488 |
+
// Hide all tabs and content
|
| 489 |
+
document.querySelectorAll('.demo-tab').forEach(tab => tab.classList.remove('active'));
|
| 490 |
+
document.querySelectorAll('.demo-content').forEach(content => content.classList.remove('active'));
|
| 491 |
+
|
| 492 |
+
// Show selected tab and content
|
| 493 |
+
event.target.classList.add('active');
|
| 494 |
+
document.getElementById('demo-' + tabName).classList.add('active');
|
| 495 |
+
}}
|
| 496 |
+
|
| 497 |
+
async function testNeedOCR() {{
|
| 498 |
+
const url = document.getElementById('need_ocr_url').value.trim();
|
| 499 |
+
const resultBox = document.getElementById('need_ocr_result');
|
| 500 |
+
const loading = document.getElementById('need_ocr_loading');
|
| 501 |
+
|
| 502 |
+
if (!url) {{
|
| 503 |
+
resultBox.innerHTML = '<div class="error">Please enter a document URL</div>';
|
| 504 |
+
resultBox.classList.add('show');
|
| 505 |
+
return;
|
| 506 |
+
}}
|
| 507 |
+
|
| 508 |
+
loading.classList.add('show');
|
| 509 |
+
resultBox.classList.remove('show');
|
| 510 |
+
|
| 511 |
+
try {{
|
| 512 |
+
const response = await fetch('{base_url}/need_ocr', {{
|
| 513 |
+
method: 'POST',
|
| 514 |
+
headers: {{
|
| 515 |
+
'Content-Type': 'application/json',
|
| 516 |
+
}},
|
| 517 |
+
body: JSON.stringify({{ urls: url }})
|
| 518 |
+
}});
|
| 519 |
+
|
| 520 |
+
const data = await response.json();
|
| 521 |
+
resultBox.innerHTML = '<pre>' + JSON.stringify(data, null, 2) + '</pre>';
|
| 522 |
+
resultBox.classList.add('show');
|
| 523 |
+
}} catch (error) {{
|
| 524 |
+
resultBox.innerHTML = '<div class="error">Error: ' + error.message + '</div>';
|
| 525 |
+
resultBox.classList.add('show');
|
| 526 |
+
}} finally {{
|
| 527 |
+
loading.classList.remove('show');
|
| 528 |
+
}}
|
| 529 |
+
}}
|
| 530 |
+
|
| 531 |
+
async function testExtract() {{
|
| 532 |
+
const url = document.getElementById('extract_url').value.trim();
|
| 533 |
+
const outputFormat = document.getElementById('output_format').value;
|
| 534 |
+
const resultBox = document.getElementById('extract_result');
|
| 535 |
+
const loading = document.getElementById('extract_loading');
|
| 536 |
+
|
| 537 |
+
if (!url) {{
|
| 538 |
+
resultBox.innerHTML = '<div class="error">Please enter a document URL</div>';
|
| 539 |
+
resultBox.classList.add('show');
|
| 540 |
+
return;
|
| 541 |
+
}}
|
| 542 |
+
|
| 543 |
+
loading.classList.add('show');
|
| 544 |
+
resultBox.classList.remove('show');
|
| 545 |
+
|
| 546 |
+
try {{
|
| 547 |
+
const response = await fetch('{base_url}/extract-native-data', {{
|
| 548 |
+
method: 'POST',
|
| 549 |
+
headers: {{
|
| 550 |
+
'Content-Type': 'application/json',
|
| 551 |
+
}},
|
| 552 |
+
body: JSON.stringify({{
|
| 553 |
+
urls: url,
|
| 554 |
+
output_format: outputFormat
|
| 555 |
+
}})
|
| 556 |
+
}});
|
| 557 |
+
|
| 558 |
+
const data = await response.json();
|
| 559 |
+
resultBox.innerHTML = '<pre>' + JSON.stringify(data, null, 2) + '</pre>';
|
| 560 |
+
resultBox.classList.add('show');
|
| 561 |
+
}} catch (error) {{
|
| 562 |
+
resultBox.innerHTML = '<div class="error">Error: ' + error.message + '</div>';
|
| 563 |
+
resultBox.classList.add('show');
|
| 564 |
+
}} finally {{
|
| 565 |
+
loading.classList.remove('show');
|
| 566 |
+
}}
|
| 567 |
+
}}
|
| 568 |
+
</script>
|
| 569 |
+
</body>
|
| 570 |
+
</html>
|
| 571 |
+
"""
|
| 572 |
+
return html_content
|
| 573 |
|
| 574 |
|
| 575 |
+
@app.post("/need_ocr", response_model=DetectResponse)
|
| 576 |
async def detect_ocr(request: DetectRequest):
|
| 577 |
"""
|
| 578 |
Detect whether documents need OCR processing.
|
|
|
|
| 662 |
)
|
| 663 |
|
| 664 |
|
| 665 |
+
@app.post("/extract-native-data", response_model=AnalyzeResponse)
|
| 666 |
+
async def extract_native_data(request: AnalyzeRequest):
|
| 667 |
"""
|
| 668 |
+
Extract structured data from machine-readable documents.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 669 |
"""
|
| 670 |
import time
|
| 671 |
start_time = time.time()
|