Spaces:
Paused
Paused
| 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("/") | |
| def index(): | |
| target = LOCAL_ENGINE_URL or "SET_LOCAL_ENGINE_URL_SECRET" | |
| return f"""<!doctype html> | |
| <html><head><meta name="viewport" content="width=device-width,initial-scale=1"><title>Continuity Code Camera Relay</title></head> | |
| <body style="font-family:system-ui;background:#070910;color:#fff;padding:20px"> | |
| <h1>Continuity Code Camera Relay</h1> | |
| <p>This Space relays to your local Ollama-backed live engine.</p> | |
| <p><b>Target:</b> {target}</p> | |
| <p>Open the local/tunnel target directly for camera permissions and lowest latency:</p> | |
| <p><a style="color:#82aaff" href="{target}">{target}</a></p> | |
| </body></html>""" | |
| 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()} | |
| 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) | |