feat(nlp-service): FastAPI app with /parse and /health endpoints
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
34
nlp-service/main.py
Normal file
34
nlp-service/main.py
Normal file
@@ -0,0 +1,34 @@
|
||||
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
|
||||
Reference in New Issue
Block a user