"""ARIA brain โ€” Gradio Space (nextneural-aria.hf.space). Exposes ONE machine API, used only by the Cloudflare aria-worker: api_name="respond" inputs : context_json (str), sig (str) output : bubbles_json (str) โ€” JSON array of {bubble, target_event_id, type} `sig` is HMAC-SHA256(ARIA_BRAIN_SECRET, context_json) in base64url. Without the secret on this Space, or with a bad signature, nothing is sent to the model: the brain is paid for by the organisation and must not be callable by anyone who finds this URL. The old in-page tester was removed for the same reason. """ import base64 import hashlib import hmac import json import os import gradio as gr from brain.orchestrator import respond, MODEL BRAIN_SECRET = os.environ.get("ARIA_BRAIN_SECRET", "") MAX_CONTEXT_CHARS = 60_000 MAX_HISTORY = 60 if not BRAIN_SECRET: print("[ARIA] ARIA_BRAIN_SECRET is not set: every call will be refused", flush=True) def _b64url_decode(s: str) -> bytes: s = (s or "").strip() return base64.urlsafe_b64decode(s + "=" * (-len(s) % 4)) def _verify(context_json: str, sig: str) -> bool: if not BRAIN_SECRET or not sig or not isinstance(context_json, str): return False try: got = _b64url_decode(sig) except Exception: return False want = hmac.new(BRAIN_SECRET.encode("utf-8"), context_json.encode("utf-8"), hashlib.sha256).digest() return hmac.compare_digest(got, want) def respond_api(context_json: str, sig: str) -> str: if not isinstance(context_json, str) or len(context_json) > MAX_CONTEXT_CHARS: raise gr.Error("context too large") if not _verify(context_json, sig): raise gr.Error("unauthorized") try: ctx = json.loads(context_json) except Exception: raise gr.Error("bad context") if not isinstance(ctx, dict) or len(ctx.get("history") or []) > MAX_HISTORY: raise gr.Error("bad context") return respond(ctx) with gr.Blocks(title="ARIA ยท brain") as demo: gr.Markdown( f"## ARIA โ€” brain\nVision + reasoning service for app.nnlabs.pl. Model: `{MODEL}`.\n\n" "This Space has no public playground: calls must be signed by the NNL backend." ) with gr.Accordion("Machine API (respond)", open=False): api_in = gr.Textbox(label="context_json", lines=6) api_sig = gr.Textbox(label="sig", lines=1) api_out = gr.Textbox(label="bubbles_json", lines=6) gr.Button("respond").click(respond_api, inputs=[api_in, api_sig], outputs=api_out, api_name="respond") if __name__ == "__main__": demo.queue(default_concurrency_limit=4, max_size=16).launch()