Download src/api/main.py from morpheuslord/rewrite: direct link, hf CLI and curl.
- Browser
- Download file 2.39 kB
-
https://huggingface.co/morpheuslord/rewrite/resolve/main/src/api/main.py
- Command line
-
hf download hf://morpheuslord/rewrite/src/api/main.py
-
curl -L -o main.py https://huggingface.co/morpheuslord/rewrite/resolve/main/src/api/main.py
2.39 kB
| """ | |
| 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 | |
| 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") | |
| 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)}") | |
| async def health(): | |
| """Health check endpoint.""" | |
| model_status = "loaded" if corrector is not None else "not loaded" | |
| return {"status": "ok", "model": model_status} | |