File size: 2,388 Bytes
9dd64b9 | 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | """
FastAPI server for the Dyslexia Academic Writing Corrector API.
Provides RESTful endpoints for text correction and health checking.
"""
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from .schemas import CorrectionRequest, CorrectionResponse
from ..inference.corrector import AcademicCorrector
import yaml
from loguru import logger
app = FastAPI(
title="Dyslexia Academic Writing Corrector API",
description="Style-preserving grammar correction and academic vocabulary elevation.",
version="1.0.0",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# Global corrector instance
corrector = None
@app.on_event("startup")
async def startup():
"""Load config and initialise corrector on startup."""
global corrector
try:
with open("configs/inference_config.yaml") as f:
config = yaml.safe_load(f)
corrector = AcademicCorrector(config)
logger.info("Corrector loaded successfully on startup")
except Exception as e:
logger.error(f"Failed to load corrector on startup: {e}")
logger.warning("API will return 503 until corrector is loaded")
@app.post("/correct", response_model=CorrectionResponse)
async def correct_text(request: CorrectionRequest):
"""Correct dyslectic text with style preservation and academic elevation."""
if corrector is None:
raise HTTPException(status_code=503, detail="Model not loaded yet")
try:
result = corrector.correct(
raw_text=request.text,
master_copy=request.master_copy,
style_alpha=request.style_alpha,
)
return CorrectionResponse(
original=result.original,
corrected=result.corrected,
style_similarity=result.style_similarity,
awl_coverage=result.awl_coverage,
readability=result.readability,
changes_summary=result.changes_summary,
)
except Exception as e:
logger.error(f"Correction failed: {e}")
raise HTTPException(status_code=500, detail=f"Correction failed: {str(e)}")
@app.get("/health")
async def health():
"""Health check endpoint."""
model_status = "loaded" if corrector is not None else "not loaded"
return {"status": "ok", "model": model_status}
|