35 lines
830 B
Python
35 lines
830 B
Python
import logging
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI, HTTPException
|
|
|
|
from extractor import extract, load_all_models
|
|
from models import ParseRequest, ParseResponse
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
logger.info("Loading spaCy models...")
|
|
load_all_models()
|
|
logger.info("All models ready.")
|
|
yield
|
|
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
|
|
|
|
@app.get("/health")
|
|
def health() -> dict:
|
|
return {"status": "ok"}
|
|
|
|
|
|
@app.post("/parse", response_model=ParseResponse)
|
|
def parse(request: ParseRequest) -> ParseResponse:
|
|
try:
|
|
return extract(request.query, request.lang)
|
|
except Exception as exc:
|
|
logger.exception("Extraction pipeline failed")
|
|
raise HTTPException(status_code=500, detail=str(exc)) from exc
|