Spaces:
Paused
Paused
Malina Claude Opus 4.7 commited on
Commit Β·
87577ff
1
Parent(s): 3ffb602
NINA: privacy/cleanup for uploaded media (delete_cache + idle eviction)
Browse files- Interface delete_cache=(300,300): Gradio purges uploaded videos older than
5 min, every 5 min. One upload still serves many prompts (audio + transcript
are cached in RAM, so the file isn't needed after first processing).
- In-memory session state evicted after 20 min idle; on-disk transcript/embedding
cache purged after 30 min. Keeps data from lingering; keeps multi-prompt fast.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -68,8 +68,10 @@ import re
|
|
| 68 |
import json
|
| 69 |
import math
|
| 70 |
import hashlib
|
|
|
|
| 71 |
import tempfile
|
| 72 |
import threading
|
|
|
|
| 73 |
from concurrent.futures import ThreadPoolExecutor
|
| 74 |
|
| 75 |
import ffmpeg
|
|
@@ -256,7 +258,31 @@ print(f"[NINA] Ready. ( {WHISPER_PRECISE} + CLIP warming up...)")
|
|
| 256 |
# Stores segments, chunks (with embeddings), visual frame embeddings, the decoded
|
| 257 |
# waveform, and a per-sector precise-transcription cache, keyed by video path.
|
| 258 |
# Second prompt on same video skips transcription, chunking and CLIP entirely.
|
| 259 |
-
_cache: dict = {} # { video_path: {"segments","chunks","visual","audio","precise"} }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
|
| 262 |
# ββ Audio ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -1165,6 +1191,7 @@ def run(video_path: str, prompt: str):
|
|
| 1165 |
prompt = prompt.strip()
|
| 1166 |
max_words = _prompt_to_words(prompt) # output size inferred from the prompt
|
| 1167 |
print(f'\n[NINA] ββ New request ββ "{prompt}" (target β {max_words} words)')
|
|
|
|
| 1168 |
|
| 1169 |
# Stage 1β3: transcription + chunking + CLIP visual (cached per video)
|
| 1170 |
fp = None
|
|
@@ -1193,6 +1220,7 @@ def run(video_path: str, prompt: str):
|
|
| 1193 |
_disk_save(fp, entry)
|
| 1194 |
print(f"[NINA] Cached transcript + visuals for this video.")
|
| 1195 |
|
|
|
|
| 1196 |
segments = entry["segments"]
|
| 1197 |
chunks = entry["chunks"]
|
| 1198 |
visual = entry["visual"]
|
|
@@ -1295,6 +1323,10 @@ demo = gr.Interface(
|
|
| 1295 |
"Answer length adapts to your prompt."
|
| 1296 |
),
|
| 1297 |
flagging_mode="never",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1298 |
)
|
| 1299 |
|
| 1300 |
if __name__ == "__main__":
|
|
|
|
| 68 |
import json
|
| 69 |
import math
|
| 70 |
import hashlib
|
| 71 |
+
import shutil
|
| 72 |
import tempfile
|
| 73 |
import threading
|
| 74 |
+
import time
|
| 75 |
from concurrent.futures import ThreadPoolExecutor
|
| 76 |
|
| 77 |
import ffmpeg
|
|
|
|
| 258 |
# Stores segments, chunks (with embeddings), visual frame embeddings, the decoded
|
| 259 |
# waveform, and a per-sector precise-transcription cache, keyed by video path.
|
| 260 |
# Second prompt on same video skips transcription, chunking and CLIP entirely.
|
| 261 |
+
_cache: dict = {} # { video_path: {"segments","chunks","visual","audio","precise","_atime"} }
|
| 262 |
+
|
| 263 |
+
# Data lifecycle / privacy:
|
| 264 |
+
# β’ The uploaded video itself is purged by Gradio's own temp cleaner
|
| 265 |
+
# (Interface(delete_cache=...)) β see the bottom of this file β so a file is
|
| 266 |
+
# removed shortly after the user's session goes idle. One upload still serves
|
| 267 |
+
# MANY prompts: the decoded audio + transcript live in RAM (below), so the
|
| 268 |
+
# file is no longer needed once processed.
|
| 269 |
+
# β’ This in-memory state and the derived on-disk cache are TTL-evicted here so
|
| 270 |
+
# nothing lingers indefinitely.
|
| 271 |
+
CACHE_IDLE_TTL = 1200 # drop in-memory video state idle for > 20 min
|
| 272 |
+
CACHE_DISK_TTL = 1800 # delete on-disk transcript/embedding cache > 30 min old
|
| 273 |
+
|
| 274 |
+
def _evict_stale() -> None:
|
| 275 |
+
now = time.time()
|
| 276 |
+
for vp in [k for k, e in _cache.items() if now - e.get("_atime", now) > CACHE_IDLE_TTL]:
|
| 277 |
+
_cache.pop(vp, None)
|
| 278 |
+
try:
|
| 279 |
+
if os.path.isdir(CACHE_DIR):
|
| 280 |
+
for name in os.listdir(CACHE_DIR):
|
| 281 |
+
d = os.path.join(CACHE_DIR, name)
|
| 282 |
+
if os.path.isdir(d) and now - os.path.getmtime(d) > CACHE_DISK_TTL:
|
| 283 |
+
shutil.rmtree(d, ignore_errors=True)
|
| 284 |
+
except Exception:
|
| 285 |
+
pass
|
| 286 |
|
| 287 |
|
| 288 |
# ββ Audio ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 1191 |
prompt = prompt.strip()
|
| 1192 |
max_words = _prompt_to_words(prompt) # output size inferred from the prompt
|
| 1193 |
print(f'\n[NINA] ββ New request ββ "{prompt}" (target β {max_words} words)')
|
| 1194 |
+
_evict_stale() # drop idle sessions / stale disk cache
|
| 1195 |
|
| 1196 |
# Stage 1β3: transcription + chunking + CLIP visual (cached per video)
|
| 1197 |
fp = None
|
|
|
|
| 1220 |
_disk_save(fp, entry)
|
| 1221 |
print(f"[NINA] Cached transcript + visuals for this video.")
|
| 1222 |
|
| 1223 |
+
entry["_atime"] = time.time() # mark recently used (keeps many-prompt sessions alive)
|
| 1224 |
segments = entry["segments"]
|
| 1225 |
chunks = entry["chunks"]
|
| 1226 |
visual = entry["visual"]
|
|
|
|
| 1323 |
"Answer length adapts to your prompt."
|
| 1324 |
),
|
| 1325 |
flagging_mode="never",
|
| 1326 |
+
# Privacy: Gradio purges uploaded files older than 5 min, every 5 min. One
|
| 1327 |
+
# upload still serves many prompts (audio/transcript are cached in RAM), and
|
| 1328 |
+
# the file is cleaned shortly after the session goes idle.
|
| 1329 |
+
delete_cache=(300, 300),
|
| 1330 |
)
|
| 1331 |
|
| 1332 |
if __name__ == "__main__":
|