Spaces:
Paused
NINA v0.90 — anti-hallucination pass + DEBUG-gated output
Browse files- synthesis: replace static system message with language-aware _make_system_msg();
detect prompt language (Polish/German/French/Spanish/English) via Unicode heuristic
and inject the explicit language name — prevents LLM from drifting to wrong language
- synthesis: remove weaker in-message "write in the same language" rule (now in system)
- synthesis: DEBUG-gate the "N region(s) found" header and "Sources:" footer;
production output now shows only the 🧠 label + LLM answer
- config: lower LLM_TEMPERATURE 0.1 → 0.05 for more deterministic output
- pipeline: apply cached precise (large-v3-turbo) text to candidates before
cross-encoder reranking — improves accuracy on repeat queries for same video
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- config.py +5 -4
- pipeline.py +8 -1
- synthesis.py +69 -24
|
@@ -14,7 +14,7 @@ CHUNK_MIN_DURATION = 8.0
|
|
| 14 |
CHUNK_MAX_DURATION = 90.0
|
| 15 |
CHUNK_WINDOW = 5
|
| 16 |
|
| 17 |
-
BI_ENCODER_TOP_K =
|
| 18 |
CROSS_ENCODER_TOP_K = 10
|
| 19 |
RESULTS_TOP_N = 6
|
| 20 |
REGION_MIN_DURATION = 5.0
|
|
@@ -53,9 +53,10 @@ VISUAL_CONCEPTS = [
|
|
| 53 |
"a dark scene at night", "a daytime scene",
|
| 54 |
]
|
| 55 |
|
| 56 |
-
LLM_MODEL = "Qwen/Qwen2.5-
|
| 57 |
-
LLM_MAX_TOKENS =
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
CACHE_DIR = os.path.join(tempfile.gettempdir(), "nina_cache")
|
| 61 |
DISK_CACHE = True
|
|
|
|
| 14 |
CHUNK_MAX_DURATION = 90.0
|
| 15 |
CHUNK_WINDOW = 5
|
| 16 |
|
| 17 |
+
BI_ENCODER_TOP_K = 40
|
| 18 |
CROSS_ENCODER_TOP_K = 10
|
| 19 |
RESULTS_TOP_N = 6
|
| 20 |
REGION_MIN_DURATION = 5.0
|
|
|
|
| 53 |
"a dark scene at night", "a daytime scene",
|
| 54 |
]
|
| 55 |
|
| 56 |
+
LLM_MODEL = "Qwen/Qwen2.5-72B-Instruct"
|
| 57 |
+
LLM_MAX_TOKENS = 1800
|
| 58 |
+
LLM_TEMPERATURE = 0.05
|
| 59 |
+
LLM_TIMEOUT = 120
|
| 60 |
|
| 61 |
CACHE_DIR = os.path.join(tempfile.gettempdir(), "nina_cache")
|
| 62 |
DISK_CACHE = True
|
|
@@ -159,7 +159,14 @@ def run(video_path: str, prompt: str, token: str = ""):
|
|
| 159 |
|
| 160 |
# SPECIFIC / HYBRID retrieval
|
| 161 |
candidates, best_bi = bi_encode_retrieve(chunks, prompt)
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# Pass 2 routing
|
| 165 |
if best_bi < BI_SCORE_GLOBAL_FLOOR:
|
|
|
|
| 159 |
|
| 160 |
# SPECIFIC / HYBRID retrieval
|
| 161 |
candidates, best_bi = bi_encode_retrieve(chunks, prompt)
|
| 162 |
+
|
| 163 |
+
# Upgrade any candidate whose precise text is already cached (improves cross-encoder accuracy)
|
| 164 |
+
for c in candidates:
|
| 165 |
+
key = round(c["start"], 2)
|
| 166 |
+
if key in precise_cache:
|
| 167 |
+
c["text"] = precise_cache[key]
|
| 168 |
+
|
| 169 |
+
_, v_norm = visual_scores(visual, prompt)
|
| 170 |
|
| 171 |
# Pass 2 routing
|
| 172 |
if best_bi < BI_SCORE_GLOBAL_FLOOR:
|
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import re
|
| 2 |
|
| 3 |
-
from config import DEBUG, GLOBAL_MAX_CHUNKS, LLM_MAX_TOKENS, LLM_MODEL
|
| 4 |
from models import llm_client
|
| 5 |
from utils import conf_bar, fmt_time
|
| 6 |
|
|
@@ -68,6 +68,39 @@ def _fallback_summary(regions: list[dict]) -> str:
|
|
| 68 |
return "(LLM unavailable — raw excerpts:)\n" + "\n".join(snippets)
|
| 69 |
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def _build_synth_prompt(regions: list[dict], prompt: str, max_words: int, mode: str) -> str:
|
| 72 |
blocks = []
|
| 73 |
for r in regions:
|
|
@@ -75,33 +108,33 @@ def _build_synth_prompt(regions: list[dict], prompt: str, max_words: int, mode:
|
|
| 75 |
tags = r.get("visual_tags", [])
|
| 76 |
block = (
|
| 77 |
f"[{fmt_time(r['start'])} → {fmt_time(r['end'])}]\n"
|
| 78 |
-
f" Spoken
|
| 79 |
)
|
| 80 |
if tags:
|
| 81 |
-
block += f"\n On screen
|
| 82 |
blocks.append(block)
|
| 83 |
fragments = "\n\n".join(blocks)
|
| 84 |
|
| 85 |
if mode == "global":
|
| 86 |
-
task_line =
|
|
|
|
|
|
|
|
|
|
| 87 |
else:
|
| 88 |
-
task_line =
|
|
|
|
|
|
|
| 89 |
|
| 90 |
return (
|
| 91 |
-
f
|
| 92 |
-
f"
|
| 93 |
-
f'The user is looking for: "{prompt}"\n\n'
|
| 94 |
-
f"Fragments:\n\n{fragments}\n\n"
|
| 95 |
f"{task_line}\n\n"
|
| 96 |
-
f"
|
| 97 |
-
f"-
|
| 98 |
-
f"-
|
| 99 |
-
f"
|
| 100 |
-
f"- Maximum {max_words} words
|
| 101 |
-
f"-
|
| 102 |
-
f"- Only use information from the fragments (audio + visual) — do not invent details\n"
|
| 103 |
-
f"- If fragments contain no real information about the topic, say so clearly, "
|
| 104 |
-
f"without further explanation\n"
|
| 105 |
)
|
| 106 |
|
| 107 |
|
|
@@ -123,11 +156,17 @@ def llm_synthesize_stream(
|
|
| 123 |
|
| 124 |
try:
|
| 125 |
acc = ""
|
|
|
|
|
|
|
|
|
|
| 126 |
stream = llm_client.chat_completion(
|
| 127 |
model=LLM_MODEL,
|
| 128 |
-
messages=[
|
|
|
|
|
|
|
|
|
|
| 129 |
max_tokens=LLM_MAX_TOKENS,
|
| 130 |
-
temperature=
|
| 131 |
stream=True,
|
| 132 |
)
|
| 133 |
for ev in stream:
|
|
@@ -167,9 +206,10 @@ def format_output(regions: list[dict], prompt: str, summary: str,
|
|
| 167 |
mode_tag = {
|
| 168 |
"global": "🌐 global", "hybrid": "⚡ hybrid", "specific": "🎯 specific"
|
| 169 |
}.get(mode, "")
|
| 170 |
-
lines = [
|
| 171 |
|
| 172 |
if DEBUG:
|
|
|
|
| 173 |
for i, r in enumerate(regions, 1):
|
| 174 |
dur = round(r["end"] - r["start"], 1)
|
| 175 |
text = r.get("precise_text", r["text"])
|
|
@@ -188,8 +228,13 @@ def format_output(regions: list[dict], prompt: str, summary: str,
|
|
| 188 |
lines += [
|
| 189 |
f' 🧠 Based on uploaded video (audio + video): — "{prompt}"',
|
| 190 |
summary,
|
| 191 |
-
f'\n Sources: ' + ', '.join(
|
| 192 |
-
f'[{fmt_time(r["start"])} → {fmt_time(r["end"])}]' for r in regions
|
| 193 |
-
),
|
| 194 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
return "\n".join(lines)
|
|
|
|
| 1 |
import re
|
| 2 |
|
| 3 |
+
from config import DEBUG, GLOBAL_MAX_CHUNKS, LLM_MAX_TOKENS, LLM_MODEL, LLM_TEMPERATURE
|
| 4 |
from models import llm_client
|
| 5 |
from utils import conf_bar, fmt_time
|
| 6 |
|
|
|
|
| 68 |
return "(LLM unavailable — raw excerpts:)\n" + "\n".join(snippets)
|
| 69 |
|
| 70 |
|
| 71 |
+
_POLISH_CHARS = frozenset("ąćęłńóśźżĄĆĘŁŃÓŚŹŻ")
|
| 72 |
+
_GERMAN_CHARS = frozenset("äöüÄÖÜß")
|
| 73 |
+
_FRENCH_CHARS = frozenset("àâæçéèêëîïôœùûüÿÀÂÆÇÉÈÊËÎÏÔŒÙÛÜŸ")
|
| 74 |
+
_SPANISH_CHARS = frozenset("áéíóúüñ¡¿ÁÉÍÓÚÜÑ")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def _detect_language(text: str) -> str:
|
| 78 |
+
if any(c in text for c in _POLISH_CHARS):
|
| 79 |
+
return "Polish"
|
| 80 |
+
if any(c in text for c in _GERMAN_CHARS):
|
| 81 |
+
return "German"
|
| 82 |
+
if any(c in text for c in _FRENCH_CHARS):
|
| 83 |
+
return "French"
|
| 84 |
+
if any(c in text for c in _SPANISH_CHARS):
|
| 85 |
+
return "Spanish"
|
| 86 |
+
return "English"
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def _make_system_msg(language: str) -> str:
|
| 90 |
+
return (
|
| 91 |
+
f"You are a precise video content extractor. "
|
| 92 |
+
f"MANDATORY: Write your entire response in {language}. "
|
| 93 |
+
f"Never switch to another language, even if the transcript is in a different language. "
|
| 94 |
+
"Your ONLY job is to report what is explicitly present in the provided transcript fragments. "
|
| 95 |
+
"NEVER invent, infer, or introduce details that are not in the fragments. "
|
| 96 |
+
"NEVER complete, correct, or guess partial words, names, numbers, or phrases — "
|
| 97 |
+
"if something is unclear or absent, omit it entirely. "
|
| 98 |
+
"For names, numbers, titles, brands, and proper nouns: copy the EXACT characters "
|
| 99 |
+
"from the transcript — do not paraphrase, re-spell, or reinterpret. "
|
| 100 |
+
"Do not fix grammar or pronunciation in quoted speech — reproduce it verbatim."
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
def _build_synth_prompt(regions: list[dict], prompt: str, max_words: int, mode: str) -> str:
|
| 105 |
blocks = []
|
| 106 |
for r in regions:
|
|
|
|
| 108 |
tags = r.get("visual_tags", [])
|
| 109 |
block = (
|
| 110 |
f"[{fmt_time(r['start'])} → {fmt_time(r['end'])}]\n"
|
| 111 |
+
f" Spoken: {spoken or '(no speech)'}"
|
| 112 |
)
|
| 113 |
if tags:
|
| 114 |
+
block += f"\n On screen: {', '.join(tags)}"
|
| 115 |
blocks.append(block)
|
| 116 |
fragments = "\n\n".join(blocks)
|
| 117 |
|
| 118 |
if mode == "global":
|
| 119 |
+
task_line = (
|
| 120 |
+
f"Task: Give a factual overview of what this video covers, "
|
| 121 |
+
f"drawing only from the fragments below."
|
| 122 |
+
)
|
| 123 |
else:
|
| 124 |
+
task_line = (
|
| 125 |
+
f'Task: Answer the user question "{prompt}" using ONLY what the fragments state.'
|
| 126 |
+
)
|
| 127 |
|
| 128 |
return (
|
| 129 |
+
f'User question: "{prompt}"\n\n'
|
| 130 |
+
f"Video transcript fragments:\n\n{fragments}\n\n"
|
|
|
|
|
|
|
| 131 |
f"{task_line}\n\n"
|
| 132 |
+
f"Rules:\n"
|
| 133 |
+
f"- Extract and attribute — do NOT rewrite or synthesize specific claims\n"
|
| 134 |
+
f"- For every fact, name, number, or quote: take it verbatim from the 'Spoken' text\n"
|
| 135 |
+
f"- Append the fragment timestamp after each fact (use the exact value shown in brackets)\n"
|
| 136 |
+
f"- Maximum {max_words} words\n"
|
| 137 |
+
f"- If the fragments contain nothing relevant to the question, say so in one sentence\n"
|
|
|
|
|
|
|
|
|
|
| 138 |
)
|
| 139 |
|
| 140 |
|
|
|
|
| 156 |
|
| 157 |
try:
|
| 158 |
acc = ""
|
| 159 |
+
language = _detect_language(prompt)
|
| 160 |
+
if DEBUG:
|
| 161 |
+
print(f"[DEBUG] Detected prompt language: {language}")
|
| 162 |
stream = llm_client.chat_completion(
|
| 163 |
model=LLM_MODEL,
|
| 164 |
+
messages=[
|
| 165 |
+
{"role": "system", "content": _make_system_msg(language)},
|
| 166 |
+
{"role": "user", "content": synth_prompt},
|
| 167 |
+
],
|
| 168 |
max_tokens=LLM_MAX_TOKENS,
|
| 169 |
+
temperature=LLM_TEMPERATURE,
|
| 170 |
stream=True,
|
| 171 |
)
|
| 172 |
for ev in stream:
|
|
|
|
| 206 |
mode_tag = {
|
| 207 |
"global": "🌐 global", "hybrid": "⚡ hybrid", "specific": "🎯 specific"
|
| 208 |
}.get(mode, "")
|
| 209 |
+
lines = []
|
| 210 |
|
| 211 |
if DEBUG:
|
| 212 |
+
lines.append(f' {len(regions)} region(s) found for: "{prompt}" [{mode_tag} · audio+video]\n')
|
| 213 |
for i, r in enumerate(regions, 1):
|
| 214 |
dur = round(r["end"] - r["start"], 1)
|
| 215 |
text = r.get("precise_text", r["text"])
|
|
|
|
| 228 |
lines += [
|
| 229 |
f' 🧠 Based on uploaded video (audio + video): — "{prompt}"',
|
| 230 |
summary,
|
|
|
|
|
|
|
|
|
|
| 231 |
]
|
| 232 |
+
|
| 233 |
+
if DEBUG:
|
| 234 |
+
lines.append(
|
| 235 |
+
'\n Sources: ' + ', '.join(
|
| 236 |
+
f'[{fmt_time(r["start"])} → {fmt_time(r["end"])}]' for r in regions
|
| 237 |
+
)
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
return "\n".join(lines)
|