#!/usr/bin/env python3
"""FR-Start web UI — stdlib server, streams from local Ollama. No cloud, no deps beyond `ollama`.
Run: python web.py → http://localhost:8420
"""
import json
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from fr_start import MODEL, build_system, stream_reply
PORT = 8420
SYSTEM = build_system()
PAGE = r"""
FR-Start
"""
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
body = PAGE.encode()
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_POST(self):
if self.path != "/chat":
self.send_error(404)
return
length = int(self.headers.get("Content-Length", 0))
history = json.loads(self.rfile.read(length))["messages"]
messages = [{"role": "system", "content": SYSTEM}] + history
self.send_response(200)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.end_headers()
try:
for piece in stream_reply(messages):
self.wfile.write(piece.encode())
self.wfile.flush()
except BrokenPipeError:
pass # client closed the tab mid-stream
def log_message(self, *args):
pass # quiet
if __name__ == "__main__":
print(f"FR-Start web UI → http://localhost:{PORT} (model: {MODEL}, Ctrl-C to stop)")
ThreadingHTTPServer(("127.0.0.1", PORT), Handler).serve_forever()