WickTech commited on
Commit
2be5486
·
verified ·
1 Parent(s): ae70c56

Deploy from push.sh

Browse files
README.md CHANGED
@@ -18,4 +18,7 @@ nDCG@k) render live from the eval harness. Retrieval uses a real embedding
18
  model (BAAI/bge-small-en-v1.5) running locally on CPU, and the app refuses
19
  questions the documents can't answer. No API key required.
20
 
 
 
 
21
  Source: https://github.com/WickTech/lumen-rag
 
18
  model (BAAI/bge-small-en-v1.5) running locally on CPU, and the app refuses
19
  questions the documents can't answer. No API key required.
20
 
21
+ The page at `/` is the FastAPI demo UI from the main repository; the HTTP API
22
+ and its docs are at `/docs`.
23
+
24
  Source: https://github.com/WickTech/lumen-rag
app.py CHANGED
@@ -1,6 +1,14 @@
1
- """Gradio front-end for the Lumen RAG Space (free-tier CPU, no Docker needed).
2
 
3
- Wraps `lumen_rag.RagEngine` directly in-process — no HTTP layer.
 
 
 
 
 
 
 
 
4
  """
5
  from __future__ import annotations
6
 
@@ -14,6 +22,8 @@ from secrets import compare_digest
14
  os.environ.setdefault("LUMEN_EMBEDDER", "fastembed")
15
  os.environ.setdefault("LUMEN_ABSTAIN_THRESHOLD", "0.66")
16
 
 
 
17
  import gradio as gr
18
  import spaces
19
 
@@ -24,10 +34,9 @@ from lumen_rag.eval.harness import load_cases
24
  from lumen_rag.ingestion.loaders import _LOADERS, load_file
25
  from lumen_rag.retrieval import Retriever
26
 
27
- # This is the app that's actually publicly live (huggingface.co/spaces/WickTech/lumen-rag)
28
- # — the FastAPI app in lumen_rag/api/app.py has its own rate limits/upload cap/admin
29
- # gate, but this Gradio front-end is a completely separate process that shares none
30
- # of that. Same protections, adapted to Gradio's callback model.
31
  _MAX_UPLOAD_BYTES = 10 * 1024 * 1024 # 10MB per file
32
  _ADMIN_KEY = os.getenv("ADMIN_KEY", "")
33
 
@@ -54,7 +63,14 @@ _REPO_ROOT = _find_repo_root()
54
  _SAMPLE_DIR = _REPO_ROOT / "data" / "docs"
55
  _EVAL_PATH = _REPO_ROOT / "data" / "eval.jsonl"
56
 
57
- engine = RagEngine()
 
 
 
 
 
 
 
58
 
59
 
60
  def load_sample(request: gr.Request) -> str:
@@ -62,7 +78,7 @@ def load_sample(request: gr.Request) -> str:
62
  raise gr.Error("Too many requests, try again shortly.")
63
  files = sorted(p for p in _SAMPLE_DIR.iterdir() if p.suffix.lower() in _LOADERS)
64
  docs = [load_file(p) for p in files]
65
- total = engine.add_documents(docs)
66
  return f"Indexed {len(docs)} sample files, {total} chunks."
67
 
68
 
@@ -84,7 +100,7 @@ def upload_files(files: list[str], request: gr.Request) -> str:
84
  docs.append(doc)
85
  if not docs:
86
  return f"No supported files. Supported: {sorted(_LOADERS)}"
87
- total = engine.add_documents(docs)
88
  return f"Indexed {len(docs)} files, {total} chunks."
89
 
90
 
@@ -98,8 +114,7 @@ def reset_index(admin_key: str, request: gr.Request) -> str:
98
  raise gr.Error("Too many requests, try again shortly.")
99
  if _ADMIN_KEY and not compare_digest(admin_key or "", _ADMIN_KEY):
100
  raise gr.Error("Invalid admin key.")
101
- global engine
102
- engine = RagEngine()
103
  return "Index reset."
104
 
105
 
@@ -107,9 +122,9 @@ def reset_index(admin_key: str, request: gr.Request) -> str:
107
  def ask(question: str, k: int, mode: str, request: gr.Request):
108
  if _rate_limited(request, "query", limit=30, window_s=60):
109
  raise gr.Error("Too many requests, try again shortly.")
110
- if len(engine.store) == 0:
111
  return "Index is empty — load the sample corpus or upload files first.", ""
112
- result = engine.query(question, k=int(k), mode=mode)
113
  lines = []
114
  if result.confidence is not None:
115
  verdict = "below threshold, abstained" if result.abstained else "answered"
@@ -126,10 +141,11 @@ def run_eval(k: int, request: gr.Request) -> str:
126
  raise gr.Error("Too many requests, try again shortly.")
127
  if not _EVAL_PATH.exists():
128
  return "Bundled eval set not found."
129
- if len(engine.store) == 0:
130
  return "Index is empty — load the sample corpus first."
131
  cases = load_cases(_EVAL_PATH)
132
- report = evaluate(Retriever(engine.store, engine.embedder), cases, k=int(k))
 
133
  d = report.as_dict()
134
  return "\n".join(f"{key}: {value}" for key, value in d.items())
135
 
@@ -179,4 +195,10 @@ with gr.Blocks(title="Lumen RAG") as demo:
179
 
180
 
181
  if __name__ == "__main__":
182
- demo.launch()
 
 
 
 
 
 
 
1
+ """Entry point for the Lumen RAG Space.
2
 
3
+ The Space serves the same thing as `lumen serve`: the demo UI at / (the one in
4
+ the README GIF) and the HTTP API with its docs at /docs.
5
+
6
+ Free accounts can only host Gradio Spaces on ZeroGPU, and ZeroGPU registers its
7
+ @spaces.GPU functions inside Gradio's own launch(), not under a hand-run server
8
+ (that fails with "No @spaces.GPU function detected during startup"). So the
9
+ Gradio app below is still what launches, but the FastAPI routes are handed to
10
+ it first and take precedence, including /. The Gradio blocks stay as the
11
+ backend ZeroGPU needs, and use the same engine and index as the API.
12
  """
13
  from __future__ import annotations
14
 
 
22
  os.environ.setdefault("LUMEN_EMBEDDER", "fastembed")
23
  os.environ.setdefault("LUMEN_ABSTAIN_THRESHOLD", "0.66")
24
 
25
+ import importlib
26
+
27
  import gradio as gr
28
  import spaces
29
 
 
34
  from lumen_rag.ingestion.loaders import _LOADERS, load_file
35
  from lumen_rag.retrieval import Retriever
36
 
37
+ # The FastAPI routes in lumen_rag/api/app.py enforce their own rate limits, upload cap
38
+ # and admin gate; the Gradio callbacks below bypass those routes, so they repeat the
39
+ # same protections, adapted to Gradio's callback model.
 
40
  _MAX_UPLOAD_BYTES = 10 * 1024 * 1024 # 10MB per file
41
  _ADMIN_KEY = os.getenv("ADMIN_KEY", "")
42
 
 
63
  _SAMPLE_DIR = _REPO_ROOT / "data" / "docs"
64
  _EVAL_PATH = _REPO_ROOT / "data" / "eval.jsonl"
65
 
66
+ # The FastAPI module, not the `app` object lumen_rag.api re-exports: its `engine`
67
+ # global is what the HTML UI and the API use, and the lifespan may replace it,
68
+ # so it is looked up on every call rather than bound once.
69
+ _api = importlib.import_module("lumen_rag.api.app")
70
+
71
+
72
+ def _engine() -> RagEngine:
73
+ return _api.engine
74
 
75
 
76
  def load_sample(request: gr.Request) -> str:
 
78
  raise gr.Error("Too many requests, try again shortly.")
79
  files = sorted(p for p in _SAMPLE_DIR.iterdir() if p.suffix.lower() in _LOADERS)
80
  docs = [load_file(p) for p in files]
81
+ total = _engine().add_documents(docs)
82
  return f"Indexed {len(docs)} sample files, {total} chunks."
83
 
84
 
 
100
  docs.append(doc)
101
  if not docs:
102
  return f"No supported files. Supported: {sorted(_LOADERS)}"
103
+ total = _engine().add_documents(docs)
104
  return f"Indexed {len(docs)} files, {total} chunks."
105
 
106
 
 
114
  raise gr.Error("Too many requests, try again shortly.")
115
  if _ADMIN_KEY and not compare_digest(admin_key or "", _ADMIN_KEY):
116
  raise gr.Error("Invalid admin key.")
117
+ _api.engine = RagEngine()
 
118
  return "Index reset."
119
 
120
 
 
122
  def ask(question: str, k: int, mode: str, request: gr.Request):
123
  if _rate_limited(request, "query", limit=30, window_s=60):
124
  raise gr.Error("Too many requests, try again shortly.")
125
+ if len(_engine().store) == 0:
126
  return "Index is empty — load the sample corpus or upload files first.", ""
127
+ result = _engine().query(question, k=int(k), mode=mode)
128
  lines = []
129
  if result.confidence is not None:
130
  verdict = "below threshold, abstained" if result.abstained else "answered"
 
141
  raise gr.Error("Too many requests, try again shortly.")
142
  if not _EVAL_PATH.exists():
143
  return "Bundled eval set not found."
144
+ if len(_engine().store) == 0:
145
  return "Index is empty — load the sample corpus first."
146
  cases = load_cases(_EVAL_PATH)
147
+ eng = _engine()
148
+ report = evaluate(Retriever(eng.store, eng.embedder), cases, k=int(k))
149
  d = report.as_dict()
150
  return "\n".join(f"{key}: {value}" for key, value in d.items())
151
 
 
195
 
196
 
197
  if __name__ == "__main__":
198
+ demo.launch(
199
+ # Routes passed to the FastAPI constructor are matched before the ones
200
+ # Gradio adds, so / redirects to the demo UI instead of Gradio's page.
201
+ app_kwargs={"routes": list(_api.app.routes), "lifespan": _api.lifespan},
202
+ # SSR puts a Node server in front that owns /; keep Python serving it.
203
+ ssr_mode=False,
204
+ )
lumen_rag/api/app.py CHANGED
@@ -88,7 +88,15 @@ def root():
88
 
89
  @app.get("/health")
90
  def health() -> dict:
91
- return {"status": "ok", "offline_mode": settings.offline}
 
 
 
 
 
 
 
 
92
 
93
 
94
  @app.get("/stats")
 
88
 
89
  @app.get("/health")
90
  def health() -> dict:
91
+ emb = engine.embedder
92
+ return {
93
+ "status": "ok",
94
+ "offline_mode": settings.offline,
95
+ # Which embedder is live, so the demo UI can say what it is running on.
96
+ "embedder": type(emb).__name__,
97
+ "embedding_model": getattr(emb, "model_name", None),
98
+ "abstain_threshold": settings.abstain_threshold,
99
+ }
100
 
101
 
102
  @app.get("/stats")
lumen_rag/api/static/index.html CHANGED
@@ -37,7 +37,8 @@
37
  <body>
38
  <header>
39
  <h1>🔦 Lumen RAG — Live Demo</h1>
40
- <p>Upload docs → hybrid retrieval (vector + BM25) → cited answer. Runs 100% offline.</p>
 
41
  </header>
42
  <main>
43
 
@@ -66,6 +67,7 @@
66
  <button id="askBtn">Ask</button>
67
  </div>
68
  <div class="answer" id="answer">Answer will appear here.</div>
 
69
  <div id="citations"></div>
70
  </div>
71
 
@@ -126,6 +128,7 @@ $('resetBtn').onclick = async () => {
126
  $('ingestStatus').textContent = 'Index cleared.';
127
  $('answer').textContent = 'Answer will appear here.';
128
  $('citations').innerHTML = '';
 
129
  $('metrics').innerHTML = '';
130
  };
131
 
@@ -134,12 +137,17 @@ $('askBtn').onclick = async () => {
134
  if (!question) return;
135
  $('answer').textContent = 'Thinking…';
136
  $('citations').innerHTML = '';
 
137
  try {
138
  const r = await api('/query', {
139
  method: 'POST', headers: { 'Content-Type': 'application/json' },
140
  body: JSON.stringify({ question, mode: $('mode').value, k: 5 })
141
  });
142
  $('answer').textContent = r.answer;
 
 
 
 
143
  const citationsEl = $('citations');
144
  citationsEl.innerHTML = '';
145
  // doc_id is attacker-controlled (set at /ingest time via IngestRequest.documents[].id,
@@ -156,6 +164,15 @@ $('askBtn').onclick = async () => {
156
  }
157
  };
158
 
 
 
 
 
 
 
 
 
 
159
  $('question').addEventListener('keydown', e => { if (e.key === 'Enter') $('askBtn').click(); });
160
 
161
  $('evalBtn').onclick = async () => {
 
37
  <body>
38
  <header>
39
  <h1>🔦 Lumen RAG — Live Demo</h1>
40
+ <p>Upload docs → retrieval (vector, BM25 or hybrid) → cited answer, or a refusal when the documents don't contain it. No API key.</p>
41
+ <p class="status" id="engineInfo"></p>
42
  </header>
43
  <main>
44
 
 
67
  <button id="askBtn">Ask</button>
68
  </div>
69
  <div class="answer" id="answer">Answer will appear here.</div>
70
+ <div class="status" id="confidence"></div>
71
  <div id="citations"></div>
72
  </div>
73
 
 
128
  $('ingestStatus').textContent = 'Index cleared.';
129
  $('answer').textContent = 'Answer will appear here.';
130
  $('citations').innerHTML = '';
131
+ $('confidence').textContent = '';
132
  $('metrics').innerHTML = '';
133
  };
134
 
 
137
  if (!question) return;
138
  $('answer').textContent = 'Thinking…';
139
  $('citations').innerHTML = '';
140
+ $('confidence').textContent = '';
141
  try {
142
  const r = await api('/query', {
143
  method: 'POST', headers: { 'Content-Type': 'application/json' },
144
  body: JSON.stringify({ question, mode: $('mode').value, k: 5 })
145
  });
146
  $('answer').textContent = r.answer;
147
+ if (r.confidence !== null && r.confidence !== undefined) {
148
+ $('confidence').textContent = `Retrieval confidence ${Number(r.confidence).toFixed(3)}` +
149
+ (r.abstained ? ' — below the threshold, so it refused instead of guessing.' : ' — answered.');
150
+ }
151
  const citationsEl = $('citations');
152
  citationsEl.innerHTML = '';
153
  // doc_id is attacker-controlled (set at /ingest time via IngestRequest.documents[].id,
 
164
  }
165
  };
166
 
167
+ // Say which embedder is live; real embedders rank best in vector mode (see README benchmarks).
168
+ api('/health').then(h => {
169
+ const names = { HashingEmbedder: 'offline hashing embedder', FastEmbedEmbedder: 'local embedding model', OpenAIEmbedder: 'OpenAI embeddings' };
170
+ let info = 'Running on the ' + (names[h.embedder] || h.embedder) + (h.embedding_model ? ` (${h.embedding_model})` : '');
171
+ info += h.abstain_threshold !== null ? ` · refuses below retrieval confidence ${h.abstain_threshold}` : ' · abstention off';
172
+ $('engineInfo').textContent = info;
173
+ if (h.embedder !== 'HashingEmbedder') $('mode').value = 'vector';
174
+ }).catch(() => {});
175
+
176
  $('question').addEventListener('keydown', e => { if (e.key === 'Enter') $('askBtn').click(); });
177
 
178
  $('evalBtn').onclick = async () => {
lumen_rag/embeddings.py CHANGED
@@ -78,7 +78,7 @@ class OpenAIEmbedder:
78
  api_key=settings.openai_api_key,
79
  base_url=settings.openai_base_url,
80
  )
81
- self._model = settings.embed_model
82
  self.dim = _OPENAI_DIMS.get(self._model, 1536)
83
 
84
  def embed(self, texts: list[str]) -> np.ndarray:
 
78
  api_key=settings.openai_api_key,
79
  base_url=settings.openai_base_url,
80
  )
81
+ self._model = self.model_name = settings.embed_model
82
  self.dim = _OPENAI_DIMS.get(self._model, 1536)
83
 
84
  def embed(self, texts: list[str]) -> np.ndarray: