# ───────────────────────────────────────────── # NINA — Neural Information & Narrative Abstraction # app.py | v0.90 | Made by Malina # ───────────────────────────────────────────── import logging import os import gradio as gr from chat import chat_stream, route_decision from fetch import fetch_video from pipeline import run # ── Quiet benign upload aborts ───────────────────────────────────────────────── # When a browser cancels a multipart upload mid-stream (slow/mobile connection, # tab switch, or our own client rejecting an oversized file), Starlette raises # `ClientDisconnect` and uvicorn logs a full "Exception in ASGI application" # traceback. It's harmless — there's nothing to handle, the request is just gone — # but it floods the Space logs. Drop only those records; everything else logs as # normal. (The new size cap + pre-upload compression also make it far rarer.) class _DropClientDisconnect(logging.Filter): def filter(self, record: logging.LogRecord) -> bool: exc = record.exc_info[1] if record.exc_info else None if exc is not None and type(exc).__name__ == "ClientDisconnect": return False return "ClientDisconnect" not in record.getMessage() for _name in ("uvicorn.error", "uvicorn"): logging.getLogger(_name).addFilter(_DropClientDisconnect()) # The custom front-end at app.nnlabs.pl drives these two API endpoints directly # (the Gradio UI itself is unused). Privacy: purge uploaded files every 15 min — # long enough that follow-up turns in a chat re-use the already-uploaded video # (no re-upload), short enough to honor "media is never stored". with gr.Blocks(title="NINA — Multimodal Video Chat", delete_cache=(900, 900)) as demo: gr.Markdown( "## NINA — Neural Information & Narrative Abstraction\n" "API for the app at **https://app.nnlabs.pl/nina**. Endpoints: `/run` " "(single-shot analysis), `/chat` (conversation), `/route` (pre-flight), `/fetch` " "(pasted link). Every call needs a signed, single-use token from the website." ) # ── /run : single-shot analysis (legacy tool page) ─────────────────────────── with gr.Tab("Analyze"): run_video = gr.Video(label="Upload video") run_prompt = gr.Textbox(label="What are you looking for?") run_token = gr.Textbox(label="access token (set by the website)", value="") run_out = gr.Textbox(label="Results", lines=24) gr.Button("Analyze").click( run, inputs=[run_video, run_prompt, run_token], outputs=run_out, api_name="run" ) # ── /chat : conversational endpoint (new chat UI) ──────────────────────────── # payload (JSON): {messages:[{role,content,quote?}], transcript, allow_analyze, # force_analyze}. Streams [answer, analyzed, transcript]. with gr.Tab("Chat"): chat_video = gr.Video(label="Video (optional — only on upload turns)") chat_payload = gr.Textbox(label="payload (JSON)") chat_token = gr.Textbox(label="access token", value="") chat_answer = gr.Textbox(label="answer", lines=18) chat_analyzed = gr.Checkbox(label="analyzed") chat_transcript = gr.Textbox(label="transcript", lines=6) gr.Button("Send").click( chat_stream, inputs=[chat_video, chat_payload, chat_token], outputs=[chat_answer, chat_analyzed, chat_transcript], api_name="chat", ) # Pre-flight: does this turn want a video re-analysis? Drives the website's # "NINA wants to analyze the video again" popup. Returns one short string. chat_route = gr.Textbox(label="route decision") gr.Button("Route").click( route_decision, inputs=[chat_payload, chat_token], outputs=chat_route, api_name="route", ) # ── /fetch : download a pasted video link (yt-dlp) for analysis ────────────── with gr.Tab("Fetch link"): fetch_url_in = gr.Textbox(label="video URL") fetch_token = gr.Textbox(label="access token", value="") fetch_video_o = gr.Video(label="fetched video") fetch_status = gr.Textbox(label="title / error") gr.Button("Fetch").click( fetch_video, inputs=[fetch_url_in, fetch_token], outputs=[fetch_video_o, fetch_status], api_name="fetch", ) demo.queue( default_concurrency_limit=int(os.getenv("NINA_CONCURRENCY", "3")), max_size=int(os.getenv("NINA_QUEUE_MAX", "40")), ) if __name__ == "__main__": # The upload route accepted any size before the token was ever checked; the # pipeline's own 500 MB limit now holds at the door too. demo.launch(max_file_size="520mb")