Spaces:
Paused
Paused
File size: 10,026 Bytes
05f095d 0695bec 05f095d 6acd603 05f095d 45db8e1 05f095d 33ec6e1 45db8e1 05f095d 33ec6e1 162bca3 33ec6e1 077fd06 1d697a7 077fd06 0695bec 9c49140 1d697a7 0695bec e2c96b8 f1e4c89 33ec6e1 729fbc4 e2c96b8 05f095d 1d697a7 | 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 | 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"
|