Create api/routers/document.py
Browse files- api/routers/document.py +123 -0
api/routers/document.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException, UploadFile, File, Form
|
| 2 |
+
from typing import Dict, Any, Optional, List
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from ...core.document.processor import DocumentProcessor, ProcessedDocument, DocumentConfig
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
router = APIRouter()
|
| 9 |
+
|
| 10 |
+
class ProcessingResponse(BaseModel):
|
| 11 |
+
"""Document processing response"""
|
| 12 |
+
status: str
|
| 13 |
+
document: ProcessedDocument
|
| 14 |
+
processing_time: float
|
| 15 |
+
|
| 16 |
+
@router.post("/process", response_model=ProcessingResponse)
|
| 17 |
+
async def process_document(
|
| 18 |
+
file: UploadFile = File(...),
|
| 19 |
+
config: Optional[str] = Form(None)
|
| 20 |
+
):
|
| 21 |
+
"""Process document"""
|
| 22 |
+
try:
|
| 23 |
+
start_time = datetime.now()
|
| 24 |
+
|
| 25 |
+
# Parse configuration
|
| 26 |
+
proc_config = {}
|
| 27 |
+
if config:
|
| 28 |
+
proc_config = json.loads(config)
|
| 29 |
+
|
| 30 |
+
# Initialize processor
|
| 31 |
+
processor = DocumentProcessor(proc_config)
|
| 32 |
+
await processor.initialize()
|
| 33 |
+
|
| 34 |
+
# Process document
|
| 35 |
+
result = await processor.process_document(file.file)
|
| 36 |
+
|
| 37 |
+
# Calculate processing time
|
| 38 |
+
processing_time = (datetime.now() - start_time).total_seconds()
|
| 39 |
+
|
| 40 |
+
return ProcessingResponse(
|
| 41 |
+
status="success",
|
| 42 |
+
document=result,
|
| 43 |
+
processing_time=processing_time
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
raise HTTPException(
|
| 48 |
+
status_code=500,
|
| 49 |
+
detail=f"Document processing failed: {str(e)}"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
@router.post("/batch", response_model=Dict[str, ProcessingResponse])
|
| 53 |
+
async def batch_process(files: List[UploadFile] = File(...)):
|
| 54 |
+
"""Batch process documents"""
|
| 55 |
+
try:
|
| 56 |
+
# Initialize processor
|
| 57 |
+
processor = DocumentProcessor()
|
| 58 |
+
await processor.initialize()
|
| 59 |
+
|
| 60 |
+
# Process documents
|
| 61 |
+
results = {}
|
| 62 |
+
for file in files:
|
| 63 |
+
start_time = datetime.now()
|
| 64 |
+
result = await processor.process_document(file.file)
|
| 65 |
+
processing_time = (datetime.now() - start_time).total_seconds()
|
| 66 |
+
|
| 67 |
+
results[file.filename] = ProcessingResponse(
|
| 68 |
+
status="success",
|
| 69 |
+
document=result,
|
| 70 |
+
processing_time=processing_time
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
return results
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
raise HTTPException(
|
| 77 |
+
status_code=500,
|
| 78 |
+
detail=f"Batch processing failed: {str(e)}"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
@router.get("/supported-types")
|
| 82 |
+
async def get_supported_types():
|
| 83 |
+
"""Get supported document types"""
|
| 84 |
+
processor = DocumentProcessor()
|
| 85 |
+
return {
|
| 86 |
+
"supported_types": processor.SUPPORTED_TYPES
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
@router.get("/config/validate")
|
| 90 |
+
async def validate_config(config: Dict[str, Any]):
|
| 91 |
+
"""Validate document processing configuration"""
|
| 92 |
+
try:
|
| 93 |
+
processor = DocumentProcessor(config)
|
| 94 |
+
is_valid = await processor.validate_config()
|
| 95 |
+
|
| 96 |
+
return {
|
| 97 |
+
"valid": is_valid,
|
| 98 |
+
"config": config
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
except Exception as e:
|
| 102 |
+
raise HTTPException(
|
| 103 |
+
status_code=400,
|
| 104 |
+
detail=f"Invalid configuration: {str(e)}"
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
@router.get("/health")
|
| 108 |
+
async def health_check():
|
| 109 |
+
"""Check document processor health"""
|
| 110 |
+
try:
|
| 111 |
+
processor = DocumentProcessor()
|
| 112 |
+
await processor.initialize()
|
| 113 |
+
|
| 114 |
+
return {
|
| 115 |
+
"status": "healthy",
|
| 116 |
+
"timestamp": datetime.now()
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
raise HTTPException(
|
| 121 |
+
status_code=500,
|
| 122 |
+
detail=f"Health check failed: {str(e)}"
|
| 123 |
+
)
|