Spaces:
Configuration error
Configuration error
Download app.py from world44/humanize-e: direct link, hf CLI and curl.
- Browser
- Download file 6.46 kB
-
https://huggingface.co/spaces/world44/humanize-e/resolve/main/app.py
- Command line
-
hf download hf://spaces/world44/humanize-e/app.py
-
curl -L -o app.py https://huggingface.co/spaces/world44/humanize-e/resolve/main/app.py
6.46 kB
| """ | |
| 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 | |
| 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" | |
| 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> | |
| """ | |
| 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", | |
| ) | |
| 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) |