NINA / config.py
Malina
Claude Fable 5.1
v0.90: scoped single-use tokens, hardened link fetch, 45-minute cap, whole-video digest, one classification call, no OCR
1d697a7
Raw History Blame
10 kB
import os
import tempfile
WHISPER_FAST = "base"
WHISPER_PRECISE = "large-v3-turbo"
SAMPLE_RATE = 16000
WHISPER_CPU_THREADS = int(os.getenv("WHISPER_CPU_THREADS", "3"))
RETRANSCRIBE_MAX = 12
# Re-transcribe a sector with the precise model when the base model's confidence
# (avg_logprob) drops below this. Loosened from -0.55 β†’ -0.40 so it actually fires
# on weak audio: the base model sits around -0.2…-0.5 on normal speech, so the old
# gate almost never triggered. -0.40 catches genuinely-shaky sectors; the budget
# (RETRANSCRIBE_MAX) and the per-sector cache keep it from running on everything.
PRECISE_LOGPROB_GATE = -0.40
# ── Whisper hallucination guards ───────────────────────────────────────────────
# On noisy / musical / distorted audio, Whisper invents fluent-but-fake speech
# (e.g. a concert recording β†’ plausible nonsense). These decode-time thresholds
# make it mark such audio as silence/low-confidence instead of fabricating, and
# disable cross-segment conditioning so it can't spiral into repetition loops.
WHISPER_NO_SPEECH_THRESHOLD = 0.6
WHISPER_LOGPROB_THRESHOLD = -1.0
WHISPER_COMPRESSION_RATIO_THRESHOLD = 2.4
WHISPER_CONDITION_ON_PREV = False
# ── ASR reliability β†’ honest hedging ──────────────────────────────────────────
# A transcript whose mean confidence is poor (or which is mostly weak segments,
# or mostly "no-speech") is flagged UNRELIABLE. Synthesis then tells the LLM the
# audio is hard to make out and to say so plainly rather than treating garbled
# fragments as real statements.
ASR_RELIABILITY_LOGPROB_FLOOR = -0.85 # mean segment avg_logprob below β†’ suspect
ASR_WEAK_SEGMENT_LOGPROB = -0.85 # a single segment is "weak" below this
ASR_UNRELIABLE_WEAK_FRACTION = 0.5 # β‰₯ this fraction weak β†’ unreliable
ASR_NO_SPEECH_CEILING = 0.55 # mean no_speech_prob above β†’ unreliable
CHUNK_SPLIT_THRESHOLD = 0.45
CHUNK_MIN_DURATION = 8.0
CHUNK_MAX_DURATION = 90.0
CHUNK_WINDOW = 5
BI_ENCODER_TOP_K = 40
CROSS_ENCODER_TOP_K = 10
RESULTS_TOP_N = 6
REGION_MIN_DURATION = 5.0
MERGE_GAP_SEC = 6.0
BI_SCORE_GLOBAL_FLOOR = 0.10
BI_SCORE_HYBRID_FLOOR = 0.28
GLOBAL_MAX_CHUNKS = 12
CLIP_MODEL = "clip-ViT-B-32"
CLIP_MAX_FRAMES = 96
CLIP_MIN_FPS = 0.1
CLIP_MAX_FPS = 0.5
CLIP_FRAME_SIZE = 224
VISUAL_WEIGHT = 0.30
VISUAL_TAGS_TOP_K = 4
VISUAL_TAG_MIN_SIM = 0.20
VISUAL_CONCEPTS = [
"a person talking to the camera", "a group of people", "a presentation with slides",
"a lecture or classroom", "an interview", "a news broadcast",
"a screen recording of software", "source code in a text editor",
"a webpage in a browser", "a chart or graph", "text on screen",
"a product demonstration", "hands demonstrating something",
"a cooking or food scene", "a kitchen", "an office", "a laboratory",
"a factory or machinery", "a construction site", "an outdoor nature landscape",
"a forest", "mountains", "a beach or ocean", "a city street",
"buildings and architecture", "a car or vehicle driving", "an airplane", "a train",
"a sports event", "people exercising", "a concert or live music",
"a video game gameplay", "an animation or cartoon", "a medical or hospital scene",
"a doctor or patient", "an animal", "a dog", "a cat", "a crowd of people",
"a map", "a whiteboard with diagrams", "a scientific diagram",
"a phone or mobile device", "a computer or laptop", "money or finance",
"a meeting or conference room", "a stage with a speaker",
"a dark scene at night", "a daytime scene",
]
LLM_MODEL = "Qwen/Qwen2.5-72B-Instruct" # text-only fallback synthesis
LLM_MAX_TOKENS = 1800
LLM_TEMPERATURE = 0.05
LLM_TIMEOUT = 120
# ── Vision-Language synthesis (primary) ───────────────────────────────────────
# NINA's answer engine is a VLM that actually SEES sampled keyframes + reads the
# transcript, so it can read on-screen text, describe who/what is shown, and fuse
# vision with speech β€” far more accurate than CLIP-tags + text-LLM. One call per
# request. Falls back to the text LLM above if the VLM is unavailable.
USE_VLM = True
VLM_MODEL = "Qwen/Qwen3-VL-30B-A3B-Instruct" # live via novita/featherless; sharper
# grammar/facts than the old 8B. Further
# upgrade: "Qwen/Qwen3-VL-235B-A22B-Instruct"
VLM_MAX_FRAMES = 10 # keyframes sent to the VLM (more = more image tokens)
VLM_FRAME_MAX_PX = 768 # downscale each frame's long side (caps image tokens)
# ── Chat follow-ups ───────────────────────────────────────────────────────────
# Follow-up turns answer from the conversation + stored transcript, and before that
# run two one-word checks (_route, _on_topic). All three used LLM_MODEL, the 72B
# text model, and a single follow-up took 3 min 48 s on 2026-09-19: the route check
# alone ran into the website's 30 s cap, and the answer streamed a 3.5k-char quiz in
# nearly four minutes. In the same Space, minutes earlier, VLM_MODEL wrote the first
# answer in 8 s. Measured directly it generates ~4.5x faster (~450 vs ~100 chars/s),
# and it is the model that already writes every analysis, so the voice stays the same.
CHAT_MODEL = VLM_MODEL
# The two checks have safe defaults (route -> answer, on_topic -> on), so a slow
# provider must not be allowed to hold a turn hostage. Past this, take the default.
CHAT_CHECK_TIMEOUT = 10
# Server-side caps on what one chat turn may carry (the website caps too, but a token
# holder can call the Space directly).
CHAT_MAX_TRANSCRIPT_CHARS = 60000
CHAT_MAX_MESSAGE_CHARS = 4000
CHAT_MAX_PROMPT_CHARS = 2000
# ── Pasted links ──────────────────────────────────────────────────────────────
FETCH_DEADLINE_SEC = 240 # the website gives up at 4 minutes anyway
FETCH_MAX_BYTES = 400 * 1024 * 1024
# ── Upload limits & server-side compression ───────────────────────────────────
# The website rejects > MAX_UPLOAD_MB before upload; we re-check here because the
# client check is bypassable. Files over COMPRESS_TRIGGER_MB are transcoded down
# to an aggressive 720p / low-bitrate MP4 before decoding β€” NINA only needs the
# audio plus a handful of sampled frames, so quality loss is irrelevant and it
# makes frame extraction + decode noticeably faster on big uploads.
MAX_UPLOAD_MB = 500
# Longer than this is refused before any decoding: the waveform of 45 minutes alone is
# ~170 MB of float32, and nothing above it finishes inside the website's turn budget.
MAX_VIDEO_SECONDS = 45 * 60
COMPRESS_TRIGGER_MB = 20
COMPRESS_TARGET_HEIGHT = 720
COMPRESS_VIDEO_BITRATE = "1200k"
COMPRESS_AUDIO_BITRATE = "96k"
# ── Vocal isolation (torchaudio Hybrid Demucs) ────────────────────────────────
# Pull the speaking voice out from under music/noise before re-transcribing, but
# only when the first ASR pass looks unreliable (musical/distorted) β€” it's CPU-heavy.
# Uses torchaudio's bundled Hybrid Demucs (no separate `demucs` package β†’ no torch
# version conflict). Best-effort: silently skipped if torchaudio is unavailable.
USE_VOCAL_ISOLATION = True
DEMUCS_BUNDLE = "HDEMUCS_HIGH_MUSDB_PLUS"
ISOLATION_MAX_SECONDS = 480 # don't isolate very long audio (CPU/memory)
ISOLATION_CHUNK_SEC = 12.0 # process in chunks to bound memory
# ── Subtitle fallback ladder ──────────────────────────────────────────────────
# When the ASR transcript is unreliable (garbled/musical), prefer a clean human
# subtitle track if the file carries one. Only text-based subtitle codecs can be
# read directly; bitmap subs (PGS/VobSub) would need OCR (separate, heavier step).
USE_SUBTITLES = True
SUBTITLE_TEXT_CODECS = frozenset({
"subrip", "srt", "ass", "ssa", "mov_text", "webvtt", "vtt", "text", "eia_608",
})
# OCR of burned-in captions. Disabled now that the VLM reads on-screen text
# natively (and far better) β€” kept here so it can be re-enabled if ever needed.
USE_OCR = False
OCR_FPS = 1.0 # sample ~1 frame/sec
OCR_MAX_FRAMES = 90
OCR_FRAME_W = 1280
OCR_FRAME_H = 720
OCR_BOTTOM_FRACTION = 0.34 # crop the bottom third (where captions sit)
VISUAL_ONSCREEN_TEXT_SIM = 0.22 # CLIP gate: frames must look like they contain text
# ── Speaker diarization (pyannote) + name attribution ─────────────────────────
# Identifies who-spoke-when; synthesis then attributes lines and maps a speaker to
# a name when they introduce themselves. Gated models β€” the Space's HF_TOKEN must
# have accepted pyannote/speaker-diarization-3.1 + pyannote/segmentation-3.0.
# Best-effort: skipped if pyannote is unavailable or the token lacks access.
USE_DIARIZATION = True
DIARIZATION_MODEL = "pyannote/speaker-diarization-3.1"
DIARIZATION_MAX_SECONDS = 1800
CACHE_DIR = os.path.join(tempfile.gettempdir(), "nina_cache")
DISK_CACHE = True
CACHE_IDLE_TTL = 1200
CACHE_DISK_TTL = 1800
DEBUG = False
SIGNING_SECRET = os.getenv("NINA_SIGNING_SECRET", "")
SPACE_TAG = "nina"