Spaces:
Sleeping
Sleeping
| """ | |
| FastAPI application entry point β Detector de Texto Generado por IA | |
| """ | |
| import os | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.staticfiles import StaticFiles | |
| from schemas import PredictRequest, PredictResponse | |
| from model_loader import load_model, predict as run_inference | |
| # ββ Lifespan: eager model loading on startup βββββββββββββββββββββββββββββββββββ | |
| async def lifespan(app: FastAPI): | |
| load_model() | |
| yield | |
| # ββ App ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| app = FastAPI( | |
| title = "Synthetic Sentinel API", | |
| description = "DetecciΓ³n de texto generado por IA β Universidad de Guayaquil", | |
| version = "2.0.0", | |
| lifespan = lifespan, | |
| ) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins = ["*"], | |
| allow_methods = ["GET", "POST"], | |
| allow_headers = ["*"], | |
| ) | |
| # ββ Routes βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def health(): | |
| return {"status": "ok"} | |
| def predict(body: PredictRequest): | |
| """Receive Spanish text and return AI-detection probability.""" | |
| if not body.text.strip(): | |
| raise HTTPException(status_code=422, detail="El texto no puede estar vacΓo.") | |
| try: | |
| result = run_inference(body.text) | |
| except Exception as exc: | |
| raise HTTPException(status_code=500, detail=str(exc)) | |
| return PredictResponse(**result) | |
| # ββ Static frontend (production) βββββββββββββββββββββββββββββββββββββββββββββββ | |
| _static_dir = os.path.join(os.path.dirname(__file__), "..", "frontend", "dist") | |
| if os.path.isdir(_static_dir): | |
| app.mount("/", StaticFiles(directory=_static_dir, html=True), name="frontend") | |