# ───────────────────────────────────────────── # NINA — conversational chat endpoint # ───────────────────────────────────────────── # Turns NINA into a chat: each turn the model decides whether to RE-WATCH the video # (retrieval + VLM on the uploaded/cached clip) or ANSWER from the conversation + # the stored transcript. The video itself is never stored server-side beyond the # short processing cache; the website stores only the derived transcript text. # # Streams a 3-tuple [answer_text, analyzed_bool, transcript_text]: # • answer_text — grows as the model streams # • analyzed_bool — True if this turn re-watched the video (counts toward usage) # • transcript_text — filled on the final event after an analysis (for storage) import json import random import time from auth import verify_usage_token from config import (CHAT_MAX_MESSAGE_CHARS, CHAT_MAX_PROMPT_CHARS, CHAT_MAX_TRANSCRIPT_CHARS, CHAT_MODEL, LLM_MAX_TOKENS, LLM_TEMPERATURE) from models import check_client, llm_client from persona import OFFTOPIC_EN, OFFTOPIC_PL, PERSONA_CHAT from lang import detect_language as _detect_language # Every step of a chat turn logs how long it took. Until 2026-09-19 this path printed # nothing at all, so a turn that took four minutes looked, in the Space logs, exactly # like a turn that crashed - and exactly like one that worked. def _log(msg: str) -> None: print(f"[NINA chat] {msg}", flush=True) def _last_user(messages: list) -> dict: return next((m for m in reversed(messages) if m.get("role") == "user"), {}) def _clip(s, n: int) -> str: s = s if isinstance(s, str) else ("" if s is None else str(s)) return s[:n] def _render_history(messages: list, max_turns: int = 12) -> str: """Server-side caps apply here too: a token holder can call the Space directly.""" lines = [] for m in messages[-max_turns:]: who = "User" if m.get("role") == "user" else "NINA" text = _clip(m.get("content", ""), CHAT_MAX_MESSAGE_CHARS) if m.get("quote"): text = f'(referring to: "{_clip(m["quote"], 400)}")\n{text}' lines.append(f"{who}: {text}") return "\n\n".join(lines) def _classify(messages: list) -> str: """ONE short call for both pre-flight questions of a follow-up: is the message about the video at all, and does answering it need the footage again. Returns "OFF", "ANALYZE" or "ANSWER". Two separate calls used to cost two round trips per turn for two one-word answers. Any failure defaults to ANSWER (the cheap, safe path).""" if not any(m.get("role") == "assistant" for m in messages): return "ANALYZE" last = _last_user(messages) if not (last.get("content") or "").strip(): return "ANSWER" t0 = time.time() try: r = check_client.chat_completion( model=CHAT_MODEL, messages=[ {"role": "system", "content": "You route turns for a video-chat assistant called NINA. The user uploaded a " "video and is chatting about it. Classify the user's LATEST message:\n" "OFF - a general request with nothing to do with the video or with NINA " "herself (recipes, coding help, world trivia).\n" "ANALYZE - about the video, and answering it needs RE-WATCHING the footage " "for visual or audio detail not covered yet.\n" "ANSWER - about the video, NINA, or the discussion, and answerable from the " "conversation so far. Short follow-ups ('explain more', 'why?', 'the second " "one', 'and then?') are almost always ANSWER.\n" "WHEN IN DOUBT between OFF and the others, it is NOT OFF: anything that could " "plausibly be about the recording is about it, even if the recording may not " "contain the answer. Questions about NINA herself are ANSWER.\n" "Examples: \"jak ta osoba ma na imie?\" -> ANSWER; \"who is the man on the " "left\" -> ANALYZE; \"what time does he say that\" -> ANSWER; \"kim ty " "jestes\" -> ANSWER; \"a co dalej?\" -> ANSWER; \"napisz mi przepis na " "pierogi\" -> OFF; \"who won the 2024 election\" -> OFF.\n" "Reply with exactly one word: OFF, ANALYZE or ANSWER."}, {"role": "user", "content": _render_history(messages) + "\n\nReply OFF, ANALYZE or ANSWER."}, ], max_tokens=4, temperature=0.0, stream=False, ) out = (r.choices[0].message.content or "").strip().upper() verdict = "OFF" if out.startswith("OFF") else ("ANALYZE" if "ANALYZE" in out else "ANSWER") _log(f"classify -> {verdict} ({time.time() - t0:.1f}s)") return verdict except Exception as e: _log(f"classify failed after {time.time() - t0:.1f}s ({type(e).__name__}) - defaulting to ANSWER") return "ANSWER" def _offtopic_line(prompt: str) -> str: """One short in-voice steer back to the video, picked from a rotating set. It used to be a single hardcoded sentence per language, so asking twice returned the identical refusal twice. Generating it with the chat model was tried and dropped: see the note in persona.py. Rotation gives the variety without the three failure modes.""" lines = (OFFTOPIC_PL if _detect_language(prompt).lower().startswith("pol") else OFFTOPIC_EN) return random.choice(lines) def route_decision(payload_json: str, token: str = "") -> str: """Pre-flight for the website: should this turn RE-ANALYZE the video? Returns one of: 'analyze_first' (first turn — analyze, no prompt), 'confirm' (a later turn needs footage detail not covered yet AND a video is available → ask the user before spending the analysis), or 'answer' (answerable from context, or no video to re-watch). The website turns 'confirm' into the "NINA wants to analyze the video again" popup.""" if not verify_usage_token(token, "route"): _log("route: token rejected") return "answer" try: payload = json.loads(payload_json or "{}") except Exception: payload = {} messages = payload.get("messages") or [] has_video = bool(payload.get("has_video", False)) if not any(m.get("role") == "assistant" for m in messages): return "analyze_first" if has_video and _classify(messages) == "ANALYZE": return "confirm" return "answer" def _answer_from_context(messages: list, transcript: str): """Stream a conversational answer grounded in the history + stored transcript.""" last = _last_user(messages) prompt = _clip((last.get("content") or "").strip(), CHAT_MAX_PROMPT_CHARS) transcript = _clip(transcript, CHAT_MAX_TRANSCRIPT_CHARS) # Language follows THIS message first. Reading the whole conversation instead was # tried and was worse: a Polish question inside an English chat came back in English, # because a long history outvoted the message actually being answered. The history is # only consulted when the message is too short to carry any signal of its own # ("ok?", "a to?"), which is the case the history was brought in for. language = _detect_language(prompt) if len(prompt) < 15: said = [m.get("content") or "" for m in messages if m.get("role") == "user"] language = _detect_language("\n".join(reversed(said))[:1200] or prompt) system = ( PERSONA_CHAT + "\n\n" f"THIS TURN - you are continuing a conversation about a video the person uploaded. " f"Write your answer in {language}. " f"RESOLVE REFERENCES to earlier turns: a short follow-up like 'explain more', 'why?', " f"'the second one', 'and that part' refers to your previous answer or a specific earlier " f"point — use the conversation to work out exactly what they mean and answer that. " f"If such a reference is genuinely AMBIGUOUS (you can't tell which message or which part " f"they mean), do NOT guess — reply with one short clarifying question naming the options. " f"Ground everything in the conversation and the stored transcript below; do not invent " f"facts. Stay on the video: if asked something unrelated to it, briefly steer back. " f"If the answer needs visual detail you don't have, say so and suggest re-analyzing. " f"CRITICAL OUTPUT FORMAT — your response must be visually structured; assume the user is " f"skimming, not reading word-for-word, so optimize for scannability. Use (whichever fit) " f"Markdown headings, bullet lists, numbered steps, and tables when comparing items. " f"Forbidden: large blocks of uninterrupted text, or any answer longer than ~5 lines " f"without formatting. A trivial one-line answer may stay plain — don't pad it." ) system += ( f"\n\nANSWER IN {language.upper()}. The persona above is written in English; that " f"says nothing about the language you answer in. Mirror the person." ) user = f"Stored video transcript:\n{transcript or '(none yet)'}\n\nConversation:\n{_render_history(messages)}" if last.get("quote"): user += f'\n\nThe user is asking about this excerpt: "{_clip(last["quote"], 400)}"' user += "\n\nAnswer the user's latest message." acc = "" t0 = time.time() first = None _log(f"answering from context ({len(messages)} messages, transcript {len(transcript or '')} chars)") try: stream = llm_client.chat_completion( model=CHAT_MODEL, messages=[{"role": "system", "content": system}, {"role": "user", "content": user}], max_tokens=LLM_MAX_TOKENS, temperature=LLM_TEMPERATURE, stream=True, ) for ev in stream: if not ev.choices: continue d = ev.choices[0].delta.content or "" if d: if first is None: first = time.time() - t0 acc += d yield acc.strip() took = time.time() - t0 _log(f"answer done: {len(acc)} chars in {took:.1f}s (first token {first or 0:.1f}s)") if not acc.strip(): yield "I couldn't form an answer from the conversation. Try re-analyzing the video." except Exception as e: # Details (provider, model, balance messages) belong in the logs, not in the chat. _log(f"answer failed after {time.time() - t0:.1f}s: {type(e).__name__}: {str(e)[:200]}") if acc.strip(): return yield "⛔ NINA couldn't answer right now (the language model didn't respond). Please try again in a minute." def chat_stream(video_path, payload_json: str, token: str = ""): """Gradio chat endpoint. payload_json: {messages, transcript, allow_analyze, force_analyze}.""" if not verify_usage_token(token, "chat"): yield ["⛔ Not authorized. Please use NINA at https://app.nnlabs.pl (sign in first).", False, ""] return try: payload = json.loads(payload_json or "{}") except Exception: payload = {} messages = [m for m in (payload.get("messages") or []) if isinstance(m, dict)][-40:] transcript = _clip(payload.get("transcript") or "", CHAT_MAX_TRANSCRIPT_CHARS) allow_analyze = bool(payload.get("allow_analyze", True)) force = bool(payload.get("force_analyze", False)) # user chose "Let her" force_answer = bool(payload.get("force_answer", False)) # user chose "Don't" has_video = bool(payload.get("has_video", False)) # client holds the chat's video last = _last_user(messages) prompt = _clip((last.get("content") or "").strip(), CHAT_MAX_PROMPT_CHARS) quote = _clip(last.get("quote") or "", 400) or None _log(f'turn: {len(prompt)} chars force_analyze={force} force_answer={force_answer} ' f'video={"yes" if video_path is not None else "no"} transcript={len(transcript)} chars') if not prompt and video_path is None: yield ["⛔ Please type a message.", False, transcript] return # Decide in ONE call: off-topic, re-watch, or answer from context. The website # settles the re-watch question up front via route_decision + the "analyze again?" # popup, then sends force_*; those skip the call. if force_answer: decision = "answer" # "Don't" — answer from context / cache only elif force: decision = "analyze" # "Let her" / first turn — re-watch else: verdict = _classify(messages) if verdict == "OFF": # Keep NINA on the uploaded video — politely refuse off-topic detours. yield [_offtopic_line(prompt), False, transcript] return decision = "analyze" if verdict == "ANALYZE" else "answer" if decision == "analyze" and allow_analyze and video_path is not None: from pipeline import _run_core, transcript_text ask = (f'Regarding "{quote}": ' if quote else "") + (prompt or "Summarize the video.") last_out = "" for out in _run_core(video_path, ask): last_out = out yield [out, True, ""] yield [last_out, True, transcript_text(video_path)] return # Answer from context (decision was answer, or no video to re-watch this turn). for partial in _answer_from_context(messages, transcript): yield [partial, False, transcript]