File size: 2,677 Bytes
4131875
9971119
8f9bde4
9971119
4131875
8f9bde4
 
9971119
8f9bde4
 
 
 
9971119
8f9bde4
 
 
4131875
8f9bde4
4131875
 
9971119
4131875
 
8f9bde4
 
 
4131875
8f9bde4
 
4131875
 
8f9bde4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4131875
8f9bde4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4131875
 
8f9bde4
4131875
8f9bde4
4131875
 
9971119
 
8f9bde4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""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()