LUNA / app.py
Malina
Claude Fable 5.1
Scoped tokens, bounded archives and frames, build deadline, circuit breaker, honest note caps
da9de03
Raw History Blame Contribute Delete
4.12 kB
# ─────────────────────────────────────────────
# 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")