File size: 4,116 Bytes
281f5b1
a416f24
281f5b1
 
 
 
62d6859
281f5b1
62d6859
24ef38b
a483b94
30ec081
281f5b1
24ef38b
 
 
 
a416f24
da9de03
24ef38b
 
281f5b1
 
3712e45
 
 
a483b94
 
 
 
 
3712e45
 
 
281f5b1
 
a416f24
281f5b1
 
 
 
 
 
 
 
 
 
 
 
 
 
a416f24
24ef38b
a416f24
281f5b1
a416f24
281f5b1
da9de03
 
281f5b1
 
 
 
 
 
 
 
 
 
 
 
 
24ef38b
 
 
 
 
 
 
 
281f5b1
 
 
62d6859
 
 
da9de03
 
 
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
# ─────────────────────────────────────────────
#  LUNA - Latent Understanding & Narrative Assembly
#  app.py  |  v0.1 | Made by Malina
# ─────────────────────────────────────────────
import logging
import os

import gradio as gr

from auth import verify_usage_token
from config import MAP_MODELS, REDUCE_MODELS, VLM_MODELS
from models import check_models_async, prewarm_whisper
from pipeline import run
from synthesis import clarify as _clarify


def clarify(prompt, token):
    """Wizard 'Am I right?' step - a short restatement of the understood intent."""
    if not verify_usage_token(token, "clarify"):
        return ""
    return _clarify(prompt or "")


# Say out loud, once, whether the configured models are actually served. A name that
# no provider carries any more fails every call, and every call site degrades quietly,
# so the whole extraction stage can be dead for weeks without a single visible error.
# Every candidate, not just the three in front: a fallback nobody has ever
# called is exactly the one that turns out to be unusable on the day it is
# needed. The probe also catches a reasoning model that answers with nothing
# but thinking, which at REDUCE's budget is indistinguishable from an outage.
check_models_async(MAP_MODELS + REDUCE_MODELS + VLM_MODELS)
prewarm_whisper()


# Quiet benign upload aborts (a browser cancelling a multipart upload mid-stream
# floods the Space logs with harmless ClientDisconnect tracebacks). Same filter NINA
# uses - drop only those records.
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 the `build` endpoint directly (the
# Gradio UI below is just for direct/testing access). Privacy: purge uploaded files
# after an hour - long enough that files uploaded during the wizard survive until the
# user finishes the steps and clicks Build; short enough to honor "files are not stored".
with gr.Blocks(title="LUNA - File Synthesis", delete_cache=(3600, 3600)) as demo:
    gr.Markdown(
        "## LUNA - Latent Understanding & Narrative Assembly\n"
        "API for the app at **https://app.nnlabs.pl/luna**. Drop up to 100 files, say "
        "what to build, and LUNA returns one structured Note. Endpoints: `/build`, "
        "`/clarify`. Every call needs a signed, single-use token from the website."
    )

    with gr.Tab("Build"):
        files  = gr.File(label="Files (up to 100, any type)", file_count="multiple")
        prompt = gr.Textbox(label="What should LUNA build?",
                            placeholder="e.g. Everything about climate change in Italy")
        token  = gr.Textbox(label="access token (set by the website)", value="")
        status = gr.Textbox(label="status", lines=1)
        note   = gr.Markdown(label="Note")
        gr.Button("Build").click(
            run, inputs=[files, prompt, token], outputs=[status, note], api_name="build"
        )

    with gr.Tab("Clarify"):
        c_prompt = gr.Textbox(label="prompt")
        c_token  = gr.Textbox(label="access token", value="")
        c_out    = gr.Textbox(label="understanding")
        gr.Button("Clarify").click(
            clarify, inputs=[c_prompt, c_token], outputs=c_out, api_name="clarify"
        )

demo.queue(
    default_concurrency_limit=int(os.getenv("LUNA_CONCURRENCY", "4")),
    max_size=int(os.getenv("LUNA_QUEUE_MAX", "24")),
)

if __name__ == "__main__":
    # The upload route accepted any size before a token was ever checked; the task
    # limit (500 MB) now holds at the door too.
    demo.launch(max_file_size="510mb")