""" FastAPI backend for humanize-e Developed by Mike Elistra (Mike Koci) Kaggle Training Notebook: https://www.kaggle.com/code/mick009/notebook9c4e543da9 Endpoints: - POST /humanize - Humanize text (JSON: {"text": "...", "temperature": 0.8}) - GET / - Web UI - GET /health - Health check """ import os from contextlib import asynccontextmanager from fastapi import FastAPI, HTTPException from fastapi.responses import HTMLResponse from pydantic import BaseModel import uvicorn # Load .env if present from dotenv import load_dotenv load_dotenv() from model import HumanizeModel model: HumanizeModel | None = None @asynccontextmanager async def lifespan(app: FastAPI): global model print("[humanize-e] Starting up...") model = HumanizeModel() print(f"[humanize-e] Model loaded: {model._loaded}") yield print("[humanize-e] Shutting down...") app = FastAPI( title="humanize-e API", description="Humanize AI-generated text with a QLoRA fine-tuned Qwen2.5-3B", version="1.0.0", lifespan=lifespan, ) class HumanizeRequest(BaseModel): text: str temperature: float = 0.8 class HumanizeResponse(BaseModel): original: str humanized: str model: str # "qwen2.5-3b-lora" or "fallback" @app.get("/", response_class=HTMLResponse) async def root(): return """ humanize-e

humanize-e

Make AI texts sound more human

Humanized text

""" @app.post("/humanize", response_model=HumanizeResponse) async def humanize(req: HumanizeRequest): if not req.text.strip(): raise HTTPException(status_code=400, detail="Text cannot be empty") if model is None: raise HTTPException(status_code=503, detail="Model not loaded") out = model.humanize(req.text, temperature=req.temperature) return HumanizeResponse( original=req.text, humanized=out, model="qwen2.5-3b-lora" if model._loaded else "fallback", ) @app.get("/health") async def health(): return { "status": "ok", "model": "qwen2.5-3b-lora" if (model and model._loaded) else "fallback", "adapter_path": model.adapter_path if model else None, } if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000)