import os import httpx from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse, JSONResponse app = FastAPI(title="Continuity Code Camera Relay") LOCAL_ENGINE_URL = os.getenv("LOCAL_ENGINE_URL", "").rstrip("/") @app.get("/", response_class=HTMLResponse) def index(): target = LOCAL_ENGINE_URL or "SET_LOCAL_ENGINE_URL_SECRET" return f"""
This Space relays to your local Ollama-backed live engine.
Target: {target}
Open the local/tunnel target directly for camera permissions and lowest latency:
""" @app.get("/health") async def health(): if not LOCAL_ENGINE_URL: return {"ok": False, "reason": "LOCAL_ENGINE_URL not set"} async with httpx.AsyncClient(timeout=10) as client: r = await client.get(f"{LOCAL_ENGINE_URL}/health") return {"ok": True, "relay_target": LOCAL_ENGINE_URL, "engine": r.json()} @app.api_route("/proxy/{path:path}", methods=["GET","POST","PUT","DELETE","PATCH"]) async def proxy(path: str, request: Request): if not LOCAL_ENGINE_URL: return JSONResponse({"ok": False, "reason": "LOCAL_ENGINE_URL not set"}, status_code=500) body = await request.body() headers = {k:v for k,v in request.headers.items() if k.lower() not in {"host","content-length"}} async with httpx.AsyncClient(timeout=60) as client: r = await client.request(request.method, f"{LOCAL_ENGINE_URL}/{path}", content=body, headers=headers, params=request.query_params) ct = r.headers.get("content-type","") if "application/json" in ct: return JSONResponse(r.json(), status_code=r.status_code) return JSONResponse({"text": r.text}, status_code=r.status_code)