""" Turn the Worker's context payload into a model call, return JSON bubbles. The Worker (DO) sends a single JSON string: { "lang": "auto", "topic": "...|null", "turn_events": ["evt_a", "evt_b"], "media": { "evt_a": {"url": "...", "mime": "...", "name": "...", "kind": "image"} }, "history": [ {"id","role","type","text"}, ... ] } We return a JSON string: an array of bubble dicts (see schema.sanitize_bubbles). """ import json import os import re import gradio as gr from huggingface_hub import InferenceClient from .persona import SYSTEM_PROMPT, build_user_frame from .schema import sanitize_bubbles MODEL = os.environ.get("ARIA_MODEL", "google/gemma-4-31B-it:novita") HF_TOKEN = os.environ.get("HF_TOKEN") BILL_TO = os.environ.get("ARIA_BILL_TO", "NextNeural") or None _client = InferenceClient(model=MODEL, token=HF_TOKEN, bill_to=BILL_TO) _PL_WORDS = ("nie", "cześć", "czesc", "jesteś", "jestes", "co", "tak", "jak", "siema", "dzięki", "dzieki", "jestem", "mam", "robisz", "elo", "dobra", "spoko") _EN_WORDS = ("the", "you", "what", "hello", "how", "are", "hey", "hi", "yes", "yeah", "thanks", "im", "me") def _lang(ctx: dict) -> str: """Polish-first. The worker may pass a decision ("pl"/"en"); otherwise look at the user's latest message with WORD boundaries (``"tak" in "take"`` used to make "take care" Polish).""" hinted = ctx.get("lang") if hinted in ("pl", "en"): return hinted users = [h.get("text", "") for h in ctx.get("history", []) if h.get("role") == "user"] t = (users[-1] if users else "").lower() if any(c in t for c in "ąćęłńóśźż"): return "pl" words = set(re.findall(r"[a-ząćęłńóśźż']+", t)) has_pl = any(w in words for w in _PL_WORDS) has_en = any(w in words for w in _EN_WORDS) return "en" if (has_en and not has_pl) else "pl" def _history_to_messages(ctx: dict, sys_extra=""): """ Role metadata is the ONLY source of truth for who said what. Per-turn steering (the frame + language) goes into the SYSTEM message, NOT as a trailing user turn — otherwise the model treats the instruction as the newest user message and answers IT ("no wiadomo że po polsku"). The conversation must END with the user's real latest message. """ msgs = [{"role": "system", "content": SYSTEM_PROMPT + sys_extra}] media = ctx.get("media", {}) for h in ctx.get("history", []): role = h.get("role", "user") if role == "system" and h.get("type") == "system_idle": role = "user" # the idle marker reads as an observation, never as an instruction if role not in ("user", "assistant"): role = "user" text = str(h.get("text", ""))[:4000] # keep raw text ONLY parts = [{"type": "text", "text": text}] m = media.get(h.get("id")) if m and m.get("kind") == "image" and m.get("url"): parts.append({"type": "image_url", "image_url": {"url": m["url"]}}) msgs.append({"role": role, "content": parts}) return msgs def respond(ctx) -> str: """Entry called from app.py after signature verification. `ctx` is the parsed context dict (a JSON string is still accepted for local testing).""" if isinstance(ctx, str): try: ctx = json.loads(ctx) except Exception: return "[]" ctx = ctx or {} valid_ids = {h.get("id") for h in ctx.get("history", [])} valid_ids |= set(ctx.get("turn_events", [])) # Per-turn steering lives in the SYSTEM message so the conversation ends with # the user's real latest message (fixes ARIA answering the instruction itself). lang = _lang(ctx) lang_line = "Reply in ENGLISH." if lang == "en" else "Odpowiedz PO POLSKU." sys_extra = "\n\n--- TA TURA ---\n" + build_user_frame(ctx) + "\n" + lang_line messages = _history_to_messages(ctx, sys_extra) # A reaction-only turn (user just reacted, no new text) has no trailing user # turn — add a tiny one so the model has the reaction to respond to. if not ctx.get("turn_events") and (ctx.get("user_reaction") or {}).get("emoji"): ur = ctx["user_reaction"] messages.append({"role": "user", "content": [{"type": "text", "text": f"(zareagowałem {ur.get('emoji','')} na Twoją ostatnią wiadomość)"}]}) try: completion = _client.chat_completion( messages, max_tokens=700, temperature=0.85, top_p=0.95, ) raw = completion.choices[0].message.content or "[]" except Exception as e: # No in-character goodbye here: the worker turns a failed call into a visible # error line, so an outage never looks like ARIA leaving. import sys print("ARIA inference error:", type(e).__name__, file=sys.stderr) raise gr.Error("model unavailable") bubbles = sanitize_bubbles(raw, valid_ids) return json.dumps(bubbles, ensure_ascii=False)