File size: 5,215 Bytes
b1fe369
e54908c
9510df1
b1fe369
 
0695bec
b1fe369
0695bec
e54908c
 
7c88737
51e7ae9
05f095d
b1fe369
0695bec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9510df1
9a43844
 
 
 
9510df1
 
1d697a7
 
 
9510df1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1fe369
7c88737
 
 
 
 
 
 
 
 
 
51e7ae9
 
 
 
 
 
 
 
 
 
 
 
 
05f095d
 
 
 
 
b1fe369
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
# ─────────────────────────────────────────────
#  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")