Spaces:
Configuration error
Configuration error
File size: 6,457 Bytes
6595ce6 | 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | """
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 """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>humanize-e</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 800px; margin: 0 auto; padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh; color: #333;
}
.container { background: white; border-radius: 12px; padding: 40px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2); }
h1 { text-align: center; margin-bottom: 10px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text; -webkit-text-fill-color: transparent; }
.subtitle { text-align: center; color: #666; margin-bottom: 30px; }
label { display: block; margin-bottom: 8px; font-weight: 500; }
textarea { width: 100%; min-height: 120px; padding: 12px;
border: 2px solid #e1e5e9; border-radius: 8px; font-size: 14px;
resize: vertical; font-family: inherit; }
textarea:focus { outline: none; border-color: #667eea; }
button { width: 100%; padding: 14px; margin-top: 15px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white; border: none; border-radius: 8px; font-size: 16px;
font-weight: 600; cursor: pointer; }
button:hover { opacity: 0.9; }
.result { margin-top: 30px; background: #f8f9fa; border-radius: 8px;
padding: 20px; border-left: 4px solid #667eea; display: none; }
.result h3 { color: #667eea; margin-bottom: 12px; }
.badge { display: inline-block; font-size: 12px; padding: 2px 8px;
border-radius: 10px; background: #e1e5e9; color: #555; margin-left: 8px; }
.footer { text-align: center; margin-top: 30px; color: rgba(0,0,0,0.4); font-size: 13px; }
</style>
</head>
<body>
<div class="container">
<h1>humanize-e</h1>
<p class="subtitle">Make AI texts sound more human <span id="badge" class="badge"></span></p>
<form id="f" onsubmit="go(event)">
<label for="text">Paste AI-generated text:</label>
<textarea id="text" required placeholder="Paste your AI-generated text here..."></textarea>
<button type="submit">Humanize it</button>
</form>
<div class="result" id="result">
<h3>Humanized text</h3>
<div id="out"></div>
</div>
<p class="footer">humanize-e · Qwen2.5-3B-Instruct + QLoRA</p>
</div>
<script>
fetch('/health').then(r => r.json()).then(h => {
document.getElementById('badge').textContent = h.model;
});
async function go(e) {
e.preventDefault();
const text = document.getElementById('text').value;
if (!text.trim()) return;
const btn = e.target.querySelector('button');
btn.disabled = true; btn.textContent = 'Humanizing...';
try {
const r = await fetch('/humanize', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({text})
});
const d = await r.json();
document.getElementById('out').textContent = d.humanized;
document.getElementById('result').style.display = 'block';
} catch (err) {
document.getElementById('out').textContent = 'Error: ' + err.message;
document.getElementById('result').style.display = 'block';
} finally {
btn.disabled = false; btn.textContent = 'Humanize it';
}
}
</script>
</body>
</html>
"""
@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) |