Spaces:
Paused
Paused
File size: 13,758 Bytes
9510df1 5af6715 077fd06 9510df1 1d697a7 077fd06 5af6715 1d697a7 9510df1 077fd06 9510df1 1d697a7 9510df1 1d697a7 9510df1 1d697a7 9510df1 1d697a7 9510df1 1d697a7 9510df1 1d697a7 9a43844 1d697a7 077fd06 9a43844 077fd06 9a43844 1d697a7 9a43844 1d697a7 9a43844 1d697a7 077fd06 1d697a7 9a43844 1d697a7 5af6715 7c88737 1d697a7 077fd06 7c88737 1d697a7 7c88737 9510df1 1d697a7 3164aab 9510df1 5af6715 9a43844 7c88737 d0ff022 9510df1 5af6715 9510df1 1d697a7 9510df1 077fd06 9510df1 077fd06 9510df1 077fd06 9510df1 077fd06 9510df1 1d697a7 9510df1 1d697a7 9510df1 1d697a7 9510df1 7c88737 d53485f 9510df1 1d697a7 9510df1 1d697a7 9510df1 1d697a7 7c88737 1d697a7 d53485f 1d697a7 d53485f 1d697a7 9510df1 | 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | # βββββββββββββββββββββββββββββββββββββββββββββ
# 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]
|