Spaces:
Paused
Paused
Commit ·
8f9bde4
1
Parent(s): bc1c646
Mozg odpowiada tylko na podpisane wywolania; bez placu zabaw; awaria to blad, nie pozegnanie
Browse files- respond(context_json, sig): HMAC-SHA256 z ARIA_BRAIN_SECRET, fail-closed, limity rozmiaru
- _try_it usuniete (publiczne wywolania modelu na kredyt organizacji)
- jezyk po granicach slow, honorowany wybor z Workera; role tylko user/assistant
- blad inferencji rzuca gr.Error zamiast udawac wyjscie ARII
- gradio i huggingface_hub przypiete; README bez hf_oauth
- README.md +4 -4
- app.py +53 -37
- brain/orchestrator.py +34 -34
- requirements.txt +4 -2
README.md
CHANGED
|
@@ -7,9 +7,6 @@ sdk: gradio
|
|
| 7 |
sdk_version: 6.5.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
hf_oauth: true
|
| 11 |
-
hf_oauth_scopes:
|
| 12 |
-
- inference-api
|
| 13 |
license: apache-2.0
|
| 14 |
short_description: Adaptive Response & Intelligence Agent
|
| 15 |
---
|
|
@@ -34,7 +31,10 @@ the brain. A public Space is the wrong place to keep the map of a private backen
|
|
| 34 |
and the copies here had drifted behind the live ones anyway.
|
| 35 |
|
| 36 |
## Run / deploy
|
| 37 |
-
- **Brain (this Space):**
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
Built on [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub),
|
| 40 |
and [Cloudflare Workers / Durable Objects / R2](https://developers.cloudflare.com/).
|
|
|
|
| 7 |
sdk_version: 6.5.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
|
|
|
|
|
|
| 10 |
license: apache-2.0
|
| 11 |
short_description: Adaptive Response & Intelligence Agent
|
| 12 |
---
|
|
|
|
| 31 |
and the copies here had drifted behind the live ones anyway.
|
| 32 |
|
| 33 |
## Run / deploy
|
| 34 |
+
- **Brain (this Space):** secrets `HF_TOKEN` (inference) and `ARIA_BRAIN_SECRET` (the
|
| 35 |
+
aria-worker signs every call with it; without it the Space refuses every call);
|
| 36 |
+
optionally `ARIA_MODEL`. There is no public playground: the brain is billed to the
|
| 37 |
+
organisation and answers only signed requests.
|
| 38 |
|
| 39 |
Built on [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub),
|
| 40 |
and [Cloudflare Workers / Durable Objects / R2](https://developers.cloudflare.com/).
|
app.py
CHANGED
|
@@ -1,60 +1,76 @@
|
|
| 1 |
"""ARIA brain — Gradio Space (nextneural-aria.hf.space).
|
| 2 |
|
| 3 |
-
Exposes
|
| 4 |
|
| 5 |
api_name="respond"
|
| 6 |
-
|
| 7 |
-
output: bubbles_json (str)
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
|
|
|
|
|
|
|
|
|
| 11 |
import json
|
|
|
|
| 12 |
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
from brain.orchestrator import respond, MODEL
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
state = state or {"history": [], "n": 0}
|
| 21 |
-
state["n"] += 1
|
| 22 |
-
eid = f"evt_{state['n']}"
|
| 23 |
-
state["history"].append({"id": eid, "role": "user", "type": "message", "text": text})
|
| 24 |
-
|
| 25 |
-
ctx = {
|
| 26 |
-
"lang": "auto",
|
| 27 |
-
"topic": None,
|
| 28 |
-
"turn_events": [eid],
|
| 29 |
-
"media": {},
|
| 30 |
-
"history": state["history"],
|
| 31 |
-
}
|
| 32 |
-
out = respond(json.dumps(ctx, ensure_ascii=False))
|
| 33 |
-
bubbles = json.loads(out)
|
| 34 |
-
for b in bubbles:
|
| 35 |
-
state["history"].append({"id": f"a_{state['n']}", "role": "assistant",
|
| 36 |
-
"type": "message", "text": b["bubble"]})
|
| 37 |
-
pretty = "\n".join(f"• {b['bubble']} [{b['type']}]" for b in bubbles) or "(cisza)"
|
| 38 |
-
return pretty, state
|
| 39 |
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
with gr.Accordion("Machine API (respond)", open=False):
|
| 46 |
api_in = gr.Textbox(label="context_json", lines=6)
|
|
|
|
| 47 |
api_out = gr.Textbox(label="bubbles_json", lines=6)
|
| 48 |
-
gr.Button("respond").click(
|
| 49 |
api_name="respond")
|
| 50 |
|
| 51 |
-
# Human tester.
|
| 52 |
-
gr.Markdown("### Pogadaj z ARIĄ")
|
| 53 |
-
st = gr.State()
|
| 54 |
-
box = gr.Textbox(label="Ty", placeholder="napisz coś...")
|
| 55 |
-
reply = gr.Textbox(label="ARIA", lines=4)
|
| 56 |
-
box.submit(_try_it, inputs=[box, st], outputs=[reply, st])
|
| 57 |
-
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
-
demo.queue().launch()
|
|
|
|
| 1 |
"""ARIA brain — Gradio Space (nextneural-aria.hf.space).
|
| 2 |
|
| 3 |
+
Exposes ONE machine API, used only by the Cloudflare aria-worker:
|
| 4 |
|
| 5 |
api_name="respond"
|
| 6 |
+
inputs : context_json (str), sig (str)
|
| 7 |
+
output : bubbles_json (str) — JSON array of {bubble, target_event_id, type}
|
| 8 |
|
| 9 |
+
`sig` is HMAC-SHA256(ARIA_BRAIN_SECRET, context_json) in base64url. Without the
|
| 10 |
+
secret on this Space, or with a bad signature, nothing is sent to the model: the
|
| 11 |
+
brain is paid for by the organisation and must not be callable by anyone who finds
|
| 12 |
+
this URL. The old in-page tester was removed for the same reason.
|
| 13 |
"""
|
| 14 |
+
import base64
|
| 15 |
+
import hashlib
|
| 16 |
+
import hmac
|
| 17 |
import json
|
| 18 |
+
import os
|
| 19 |
|
| 20 |
import gradio as gr
|
| 21 |
|
| 22 |
from brain.orchestrator import respond, MODEL
|
| 23 |
|
| 24 |
+
BRAIN_SECRET = os.environ.get("ARIA_BRAIN_SECRET", "")
|
| 25 |
+
MAX_CONTEXT_CHARS = 60_000
|
| 26 |
+
MAX_HISTORY = 60
|
| 27 |
|
| 28 |
+
if not BRAIN_SECRET:
|
| 29 |
+
print("[ARIA] ARIA_BRAIN_SECRET is not set: every call will be refused", flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
+
def _b64url_decode(s: str) -> bytes:
|
| 33 |
+
s = (s or "").strip()
|
| 34 |
+
return base64.urlsafe_b64decode(s + "=" * (-len(s) % 4))
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _verify(context_json: str, sig: str) -> bool:
|
| 38 |
+
if not BRAIN_SECRET or not sig or not isinstance(context_json, str):
|
| 39 |
+
return False
|
| 40 |
+
try:
|
| 41 |
+
got = _b64url_decode(sig)
|
| 42 |
+
except Exception:
|
| 43 |
+
return False
|
| 44 |
+
want = hmac.new(BRAIN_SECRET.encode("utf-8"), context_json.encode("utf-8"), hashlib.sha256).digest()
|
| 45 |
+
return hmac.compare_digest(got, want)
|
| 46 |
+
|
| 47 |
|
| 48 |
+
def respond_api(context_json: str, sig: str) -> str:
|
| 49 |
+
if not isinstance(context_json, str) or len(context_json) > MAX_CONTEXT_CHARS:
|
| 50 |
+
raise gr.Error("context too large")
|
| 51 |
+
if not _verify(context_json, sig):
|
| 52 |
+
raise gr.Error("unauthorized")
|
| 53 |
+
try:
|
| 54 |
+
ctx = json.loads(context_json)
|
| 55 |
+
except Exception:
|
| 56 |
+
raise gr.Error("bad context")
|
| 57 |
+
if not isinstance(ctx, dict) or len(ctx.get("history") or []) > MAX_HISTORY:
|
| 58 |
+
raise gr.Error("bad context")
|
| 59 |
+
return respond(ctx)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
with gr.Blocks(title="ARIA · brain") as demo:
|
| 63 |
+
gr.Markdown(
|
| 64 |
+
f"## ARIA — brain\nVision + reasoning service for app.nnlabs.pl. Model: `{MODEL}`.\n\n"
|
| 65 |
+
"This Space has no public playground: calls must be signed by the NNL backend."
|
| 66 |
+
)
|
| 67 |
with gr.Accordion("Machine API (respond)", open=False):
|
| 68 |
api_in = gr.Textbox(label="context_json", lines=6)
|
| 69 |
+
api_sig = gr.Textbox(label="sig", lines=1)
|
| 70 |
api_out = gr.Textbox(label="bubbles_json", lines=6)
|
| 71 |
+
gr.Button("respond").click(respond_api, inputs=[api_in, api_sig], outputs=api_out,
|
| 72 |
api_name="respond")
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
+
demo.queue(default_concurrency_limit=4, max_size=16).launch()
|
brain/orchestrator.py
CHANGED
|
@@ -13,7 +13,9 @@ We return a JSON string: an array of bubble dicts (see schema.sanitize_bubbles).
|
|
| 13 |
|
| 14 |
import json
|
| 15 |
import os
|
|
|
|
| 16 |
|
|
|
|
| 17 |
from huggingface_hub import InferenceClient
|
| 18 |
|
| 19 |
from .persona import SYSTEM_PROMPT, build_user_frame
|
|
@@ -27,36 +29,28 @@ BILL_TO = os.environ.get("ARIA_BILL_TO", "NextNeural") or None
|
|
| 27 |
_client = InferenceClient(model=MODEL, token=HF_TOKEN, bill_to=BILL_TO)
|
| 28 |
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
_LEAVE_EN = ["gotta go", "gotta run", "ok i gotta bounce", "gotta head out"]
|
| 34 |
-
_BYE_EN = ["see you", "talk soon", "take care", "bye for now", "cya"]
|
| 35 |
|
| 36 |
|
| 37 |
def _lang(ctx: dict) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
users = [h.get("text", "") for h in ctx.get("history", []) if h.get("role") == "user"]
|
| 39 |
t = (users[-1] if users else "").lower()
|
| 40 |
-
|
| 41 |
if any(c in t for c in "ąćęłńóśźż"):
|
| 42 |
return "pl"
|
| 43 |
-
|
| 44 |
-
has_pl = any(w in
|
| 45 |
-
has_en = any(w in
|
| 46 |
-
|
| 47 |
return "en" if (has_en and not has_pl) else "pl"
|
| 48 |
|
| 49 |
|
| 50 |
-
def _signoff_bubbles(ctx: dict):
|
| 51 |
-
import random
|
| 52 |
-
leave, bye = (_LEAVE_EN, _BYE_EN) if _lang(ctx) == "en" else (_LEAVE_PL, _BYE_PL)
|
| 53 |
-
|
| 54 |
-
return [
|
| 55 |
-
{"bubble": random.choice(leave), "target_event_id": None, "type": "proactive"},
|
| 56 |
-
{"bubble": random.choice(bye), "target_event_id": None, "type": "proactive"},
|
| 57 |
-
]
|
| 58 |
-
|
| 59 |
-
|
| 60 |
def _history_to_messages(ctx: dict, sys_extra=""):
|
| 61 |
"""
|
| 62 |
Role metadata is the ONLY source of truth for who said what.
|
|
@@ -71,7 +65,11 @@ def _history_to_messages(ctx: dict, sys_extra=""):
|
|
| 71 |
|
| 72 |
for h in ctx.get("history", []):
|
| 73 |
role = h.get("role", "user")
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# keep raw text ONLY
|
| 77 |
parts = [{"type": "text", "text": text}]
|
|
@@ -85,13 +83,16 @@ def _history_to_messages(ctx: dict, sys_extra=""):
|
|
| 85 |
return msgs
|
| 86 |
|
| 87 |
|
| 88 |
-
def respond(
|
| 89 |
-
"""Entry called
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
|
| 96 |
valid_ids = {h.get("id") for h in ctx.get("history", [])}
|
| 97 |
valid_ids |= set(ctx.get("turn_events", []))
|
|
@@ -117,14 +118,13 @@ def respond(context_json: str) -> str:
|
|
| 117 |
temperature=0.85,
|
| 118 |
top_p=0.95,
|
| 119 |
)
|
| 120 |
-
|
| 121 |
raw = completion.choices[0].message.content or "[]"
|
| 122 |
-
|
| 123 |
except Exception as e:
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
| 128 |
|
| 129 |
bubbles = sanitize_bubbles(raw, valid_ids)
|
| 130 |
-
return json.dumps(bubbles, ensure_ascii=False)
|
|
|
|
| 13 |
|
| 14 |
import json
|
| 15 |
import os
|
| 16 |
+
import re
|
| 17 |
|
| 18 |
+
import gradio as gr
|
| 19 |
from huggingface_hub import InferenceClient
|
| 20 |
|
| 21 |
from .persona import SYSTEM_PROMPT, build_user_frame
|
|
|
|
| 29 |
_client = InferenceClient(model=MODEL, token=HF_TOKEN, bill_to=BILL_TO)
|
| 30 |
|
| 31 |
|
| 32 |
+
_PL_WORDS = ("nie", "cześć", "czesc", "jesteś", "jestes", "co", "tak", "jak", "siema", "dzięki", "dzieki",
|
| 33 |
+
"jestem", "mam", "robisz", "elo", "dobra", "spoko")
|
| 34 |
+
_EN_WORDS = ("the", "you", "what", "hello", "how", "are", "hey", "hi", "yes", "yeah", "thanks", "im", "me")
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
def _lang(ctx: dict) -> str:
|
| 38 |
+
"""Polish-first. The worker may pass a decision ("pl"/"en"); otherwise look at
|
| 39 |
+
the user's latest message with WORD boundaries (``"tak" in "take"`` used to make
|
| 40 |
+
"take care" Polish)."""
|
| 41 |
+
hinted = ctx.get("lang")
|
| 42 |
+
if hinted in ("pl", "en"):
|
| 43 |
+
return hinted
|
| 44 |
users = [h.get("text", "") for h in ctx.get("history", []) if h.get("role") == "user"]
|
| 45 |
t = (users[-1] if users else "").lower()
|
|
|
|
| 46 |
if any(c in t for c in "ąćęłńóśźż"):
|
| 47 |
return "pl"
|
| 48 |
+
words = set(re.findall(r"[a-ząćęłńóśźż']+", t))
|
| 49 |
+
has_pl = any(w in words for w in _PL_WORDS)
|
| 50 |
+
has_en = any(w in words for w in _EN_WORDS)
|
|
|
|
| 51 |
return "en" if (has_en and not has_pl) else "pl"
|
| 52 |
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
def _history_to_messages(ctx: dict, sys_extra=""):
|
| 55 |
"""
|
| 56 |
Role metadata is the ONLY source of truth for who said what.
|
|
|
|
| 65 |
|
| 66 |
for h in ctx.get("history", []):
|
| 67 |
role = h.get("role", "user")
|
| 68 |
+
if role == "system" and h.get("type") == "system_idle":
|
| 69 |
+
role = "user" # the idle marker reads as an observation, never as an instruction
|
| 70 |
+
if role not in ("user", "assistant"):
|
| 71 |
+
role = "user"
|
| 72 |
+
text = str(h.get("text", ""))[:4000]
|
| 73 |
|
| 74 |
# keep raw text ONLY
|
| 75 |
parts = [{"type": "text", "text": text}]
|
|
|
|
| 83 |
return msgs
|
| 84 |
|
| 85 |
|
| 86 |
+
def respond(ctx) -> str:
|
| 87 |
+
"""Entry called from app.py after signature verification. `ctx` is the parsed
|
| 88 |
+
context dict (a JSON string is still accepted for local testing)."""
|
| 89 |
|
| 90 |
+
if isinstance(ctx, str):
|
| 91 |
+
try:
|
| 92 |
+
ctx = json.loads(ctx)
|
| 93 |
+
except Exception:
|
| 94 |
+
return "[]"
|
| 95 |
+
ctx = ctx or {}
|
| 96 |
|
| 97 |
valid_ids = {h.get("id") for h in ctx.get("history", [])}
|
| 98 |
valid_ids |= set(ctx.get("turn_events", []))
|
|
|
|
| 118 |
temperature=0.85,
|
| 119 |
top_p=0.95,
|
| 120 |
)
|
|
|
|
| 121 |
raw = completion.choices[0].message.content or "[]"
|
|
|
|
| 122 |
except Exception as e:
|
| 123 |
+
# No in-character goodbye here: the worker turns a failed call into a visible
|
| 124 |
+
# error line, so an outage never looks like ARIA leaving.
|
| 125 |
+
import sys
|
| 126 |
+
print("ARIA inference error:", type(e).__name__, file=sys.stderr)
|
| 127 |
+
raise gr.Error("model unavailable")
|
| 128 |
|
| 129 |
bubbles = sanitize_bubbles(raw, valid_ids)
|
| 130 |
+
return json.dumps(bubbles, ensure_ascii=False)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# Pinned so a Space restart cannot pull a different Gradio and change the API the
|
| 2 |
+
# worker depends on. gradio matches sdk_version in README.md.
|
| 3 |
+
gradio==6.5.1
|
| 4 |
+
huggingface_hub==1.15.0
|