-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (47 loc) · 1.97 KB
/
main.py
File metadata and controls
59 lines (47 loc) · 1.97 KB
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
from fastapi import FastAPI, HTTPException, Depends, Security
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from pydantic import BaseModel, HttpUrl
from typing import Any, Dict
import uvicorn
import os
from processor import handler as process_handler
app = FastAPI(title="Doc Processor API", description="Process documents through enhanced Docling pipeline")
# Security
security = HTTPBearer()
def verify_api_key(credentials: HTTPAuthorizationCredentials = Security(security)):
"""Verify API key from Authorization header"""
api_key = os.getenv("API_KEY", "admin") # Default to "admin" if not set
if credentials.credentials != api_key:
raise HTTPException(
status_code=401,
detail="Invalid API key",
headers={"WWW-Authenticate": "Bearer"},
)
return credentials.credentials
class ProcessRequest(BaseModel):
document_url: HttpUrl
class ProcessResult(BaseModel):
result: Dict[str, Any]
@app.get("/")
async def root():
return {
"message": "Doc Processor API",
"docs": "/docs",
"auth": "Required - use Authorization: Bearer <API_KEY>"
}
@app.post("/process", response_model=ProcessResult)
async def process_document(request: ProcessRequest, api_key: str = Depends(verify_api_key)):
"""
Process a document through the enhanced Docling pipeline.
- **document_url**: URL of the document to process (PDF, etc.)
Downloads the document, processes it through Docling, and returns
the processed output along with useful metadata.
Requires Authorization header with Bearer token (API key).
"""
try:
result = process_handler(str(request.document_url))
return ProcessResult(result=result)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Document processing failed: {str(e)}")
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, workers=int(os.getenv("WORKERS", "1")))