WickTech commited on
Commit
56a36ca
·
verified ·
1 Parent(s): f8a5189

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`, and a Gradio version of the UI is at `/gradio`.
23
+
24
  Source: https://github.com/WickTech/lumen-rag
app.py CHANGED
@@ -1,6 +1,11 @@
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 +19,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 +31,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 +60,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 +75,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 +97,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 +111,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 +119,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 +138,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 +192,7 @@ 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 FastAPI app with its demo
4
+ UI at / (the one in the README GIF) and the HTTP API (/docs). Free accounts can
5
+ only host Gradio Spaces on ZeroGPU, and ZeroGPU needs a @spaces.GPU function, so
6
+ a Gradio front-end is mounted at /gradio. Both share one engine and one index.
7
+
8
+ Run `python app.py` (as the Space does) for the combined server.
9
  """
10
  from __future__ import annotations
11
 
 
19
  os.environ.setdefault("LUMEN_EMBEDDER", "fastembed")
20
  os.environ.setdefault("LUMEN_ABSTAIN_THRESHOLD", "0.66")
21
 
22
+ import importlib
23
+
24
  import gradio as gr
25
  import spaces
26
 
 
31
  from lumen_rag.ingestion.loaders import _LOADERS, load_file
32
  from lumen_rag.retrieval import Retriever
33
 
34
+ # The FastAPI routes in lumen_rag/api/app.py enforce their own rate limits, upload cap
35
+ # and admin gate; the Gradio callbacks below bypass those routes, so they repeat the
36
+ # same protections, adapted to Gradio's callback model.
 
37
  _MAX_UPLOAD_BYTES = 10 * 1024 * 1024 # 10MB per file
38
  _ADMIN_KEY = os.getenv("ADMIN_KEY", "")
39
 
 
60
  _SAMPLE_DIR = _REPO_ROOT / "data" / "docs"
61
  _EVAL_PATH = _REPO_ROOT / "data" / "eval.jsonl"
62
 
63
+ # The FastAPI module, not the `app` object lumen_rag.api re-exports: its `engine`
64
+ # global is what the HTML UI and the API use, and the lifespan may replace it,
65
+ # so it is looked up on every call rather than bound once.
66
+ _api = importlib.import_module("lumen_rag.api.app")
67
+
68
+
69
+ def _engine() -> RagEngine:
70
+ return _api.engine
71
 
72
 
73
  def load_sample(request: gr.Request) -> str:
 
75
  raise gr.Error("Too many requests, try again shortly.")
76
  files = sorted(p for p in _SAMPLE_DIR.iterdir() if p.suffix.lower() in _LOADERS)
77
  docs = [load_file(p) for p in files]
78
+ total = _engine().add_documents(docs)
79
  return f"Indexed {len(docs)} sample files, {total} chunks."
80
 
81
 
 
97
  docs.append(doc)
98
  if not docs:
99
  return f"No supported files. Supported: {sorted(_LOADERS)}"
100
+ total = _engine().add_documents(docs)
101
  return f"Indexed {len(docs)} files, {total} chunks."
102
 
103
 
 
111
  raise gr.Error("Too many requests, try again shortly.")
112
  if _ADMIN_KEY and not compare_digest(admin_key or "", _ADMIN_KEY):
113
  raise gr.Error("Invalid admin key.")
114
+ _api.engine = RagEngine()
 
115
  return "Index reset."
116
 
117
 
 
119
  def ask(question: str, k: int, mode: str, request: gr.Request):
120
  if _rate_limited(request, "query", limit=30, window_s=60):
121
  raise gr.Error("Too many requests, try again shortly.")
122
+ if len(_engine().store) == 0:
123
  return "Index is empty — load the sample corpus or upload files first.", ""
124
+ result = _engine().query(question, k=int(k), mode=mode)
125
  lines = []
126
  if result.confidence is not None:
127
  verdict = "below threshold, abstained" if result.abstained else "answered"
 
138
  raise gr.Error("Too many requests, try again shortly.")
139
  if not _EVAL_PATH.exists():
140
  return "Bundled eval set not found."
141
+ if len(_engine().store) == 0:
142
  return "Index is empty — load the sample corpus first."
143
  cases = load_cases(_EVAL_PATH)
144
+ eng = _engine()
145
+ report = evaluate(Retriever(eng.store, eng.embedder), cases, k=int(k))
146
  d = report.as_dict()
147
  return "\n".join(f"{key}: {value}" for key, value in d.items())
148
 
 
192
 
193
 
194
  if __name__ == "__main__":
195
+ import uvicorn
196
+
197
+ server = gr.mount_gradio_app(_api.app, demo, path="/gradio")
198
+ uvicorn.run(server, host="0.0.0.0", port=int(os.getenv("GRADIO_SERVER_PORT", "7860")))
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: