| """ |
| 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=["*"], |
| ) |
|
|
| |
| 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} |
|
|