llm / app.py
steliosot's picture
Replace Gradio with FastAPI UI for stable HF startup
ed69e30
Raw
History Blame Contribute Delete
2.65 kB
import os
import requests
from fastapi import FastAPI, Form
from fastapi.responses import HTMLResponse, JSONResponse
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://127.0.0.1:11434")
MODEL = os.getenv("OLLAMA_MODEL", "qwen2.5:0.5b")
APP_KEY = os.getenv("APP_KEY", "")
app = FastAPI(title="Ollama Qwen Demo")
def run_prompt(prompt: str, key: str = "") -> dict:
if APP_KEY and key != APP_KEY:
return {"error": "Unauthorized"}
try:
res = requests.post(
f"{OLLAMA_URL}/api/generate",
json={"model": MODEL, "prompt": prompt, "stream": False},
timeout=180,
)
res.raise_for_status()
return {"response": res.json().get("response", "")}
except Exception as exc:
return {"error": str(exc)}
@app.get("/", response_class=HTMLResponse)
def index() -> str:
return f"""<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Ollama Qwen Demo</title>
<style>
body {{ font-family: sans-serif; max-width: 900px; margin: 24px auto; padding: 0 16px; }}
textarea, input, button {{ width: 100%; margin-top: 8px; padding: 10px; box-sizing: border-box; }}
textarea {{ min-height: 140px; }}
pre {{ white-space: pre-wrap; background: #f3f3f3; padding: 12px; border-radius: 8px; }}
</style>
</head>
<body>
<h1>Ollama Qwen Demo</h1>
<p>Model: <code>{MODEL}</code></p>
<form method="post" action="/chat">
<label>Prompt</label>
<textarea name="prompt" placeholder="Ask Qwen anything..." required></textarea>
<label>Access key (optional)</label>
<input name="key" type="password" />
<button type="submit">Generate</button>
</form>
</body>
</html>"""
@app.post("/chat", response_class=HTMLResponse)
def chat_form(prompt: str = Form(...), key: str = Form(default="")) -> str:
result = run_prompt(prompt, key)
output = result.get("response") or result.get("error", "No response")
return f"""<!doctype html>
<html>
<head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /></head>
<body style="font-family:sans-serif;max-width:900px;margin:24px auto;padding:0 16px;">
<a href="/">← Back</a>
<h2>Response</h2>
<pre style="white-space:pre-wrap;background:#f3f3f3;padding:12px;border-radius:8px;">{output}</pre>
</body>
</html>"""
@app.post("/api/chat")
def chat_api(payload: dict) -> JSONResponse:
prompt = str(payload.get("prompt", ""))
key = str(payload.get("key", ""))
return JSONResponse(run_prompt(prompt, key))