Add SMC Gradio demo
Browse files- README.md +4 -12
- app.py +419 -0
- data/sample_docs/doc1.txt +1 -0
- data/sample_docs/doc2.txt +1 -0
- docs/03_unit_economics.md +24 -0
- requirements.txt +24 -0
README.md
CHANGED
|
@@ -1,12 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
colorTo: red
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 6.0.1
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
# SMC Demo (Structural Manifold Sidecar)
|
| 2 |
+
Upload PDF/txt/md → see compression + reconstruction + hazard gate.
|
| 3 |
+
Paste a chunk → hazard-gated verification (window=128B, stride=96B).
|
| 4 |
+
Hazard gate shows green/red; lower the gate slider to be more permissive.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Gradio app for structural manifold sidecar: compression, reconstruction, and verification."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import io
|
| 7 |
+
import textwrap
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from typing import Dict, Optional, Tuple
|
| 10 |
+
|
| 11 |
+
import os
|
| 12 |
+
import gradio as gr
|
| 13 |
+
import matplotlib.pyplot as plt
|
| 14 |
+
import numpy as np
|
| 15 |
+
|
| 16 |
+
import sys
|
| 17 |
+
|
| 18 |
+
REPO_ROOT = Path(__file__).resolve().parent
|
| 19 |
+
SRC_PATH = REPO_ROOT / "src"
|
| 20 |
+
if str(SRC_PATH) not in sys.path:
|
| 21 |
+
sys.path.insert(0, str(SRC_PATH))
|
| 22 |
+
|
| 23 |
+
WINDOW_BYTES = 128
|
| 24 |
+
STRIDE_BYTES = 96
|
| 25 |
+
EMBEDDING_MODEL_NAME = "all-MiniLM-L6-v2"
|
| 26 |
+
ENABLE_RETRIEVE = os.getenv("ENABLE_RETRIEVE", "0").lower() in {"1", "true", "yes"}
|
| 27 |
+
|
| 28 |
+
from manifold.sidecar import (
|
| 29 |
+
EncodeResult,
|
| 30 |
+
ManifoldIndex,
|
| 31 |
+
build_index,
|
| 32 |
+
encode_text,
|
| 33 |
+
reconstruct_from_windows,
|
| 34 |
+
verify_snippet,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
import pdfplumber # type: ignore
|
| 39 |
+
except Exception: # pragma: no cover - optional dependency handled by requirements.txt
|
| 40 |
+
pdfplumber = None
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
from sentence_transformers import SentenceTransformer # type: ignore
|
| 44 |
+
except Exception: # pragma: no cover - lazy load handled later
|
| 45 |
+
SentenceTransformer = None
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
docs_store: Dict[str, str] = {}
|
| 49 |
+
encodings_store: Dict[str, EncodeResult] = {}
|
| 50 |
+
doc_counter = 0
|
| 51 |
+
_embedding_model = None
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _next_doc_id() -> str:
|
| 55 |
+
global doc_counter
|
| 56 |
+
doc_counter += 1
|
| 57 |
+
return f"doc-{doc_counter}"
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def _extract_text_from_file(file_obj) -> Tuple[Optional[str], Optional[str]]:
|
| 61 |
+
if file_obj is None:
|
| 62 |
+
return None, None
|
| 63 |
+
path = Path(file_obj.name)
|
| 64 |
+
suffix = path.suffix.lower()
|
| 65 |
+
raw_bytes = file_obj.read()
|
| 66 |
+
file_obj.seek(0)
|
| 67 |
+
if suffix in {".txt", ".md"}:
|
| 68 |
+
text = raw_bytes.decode("utf-8", errors="ignore")
|
| 69 |
+
return path.name, text
|
| 70 |
+
if suffix == ".pdf":
|
| 71 |
+
if pdfplumber is None:
|
| 72 |
+
raise RuntimeError("pdfplumber is required for PDF ingestion. Install with `pip install pdfplumber`.")
|
| 73 |
+
with pdfplumber.open(io.BytesIO(raw_bytes)) as pdf:
|
| 74 |
+
pages = [page.extract_text() or "" for page in pdf.pages]
|
| 75 |
+
text = "\n\n".join(pages).strip()
|
| 76 |
+
return path.name, text
|
| 77 |
+
raise ValueError(f"Unsupported file type: {suffix}")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def _make_hazard_plot(hazards):
|
| 81 |
+
fig, ax = plt.subplots(figsize=(5, 3))
|
| 82 |
+
if hazards:
|
| 83 |
+
ax.hist(hazards, bins=20, color="#2f6fff", alpha=0.8)
|
| 84 |
+
ax.set_title("Window hazards")
|
| 85 |
+
ax.set_xlabel("Hazard λ")
|
| 86 |
+
ax.set_ylabel("Window count")
|
| 87 |
+
fig.tight_layout()
|
| 88 |
+
return fig
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def _preview(text: str, limit: int = 2000) -> str:
|
| 92 |
+
if len(text) <= limit:
|
| 93 |
+
return text
|
| 94 |
+
return text[:limit] + f"\n\n… [truncated {len(text) - limit} chars]"
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def _chunk_text(text: str, chunk_size: int = 512, overlap: int = 128) -> list[tuple[str, str]]:
|
| 98 |
+
chunks = []
|
| 99 |
+
start = 0
|
| 100 |
+
text_len = len(text)
|
| 101 |
+
idx = 0
|
| 102 |
+
while start < text_len:
|
| 103 |
+
end = min(text_len, start + chunk_size)
|
| 104 |
+
chunk = text[start:end]
|
| 105 |
+
chunks.append((f"chunk-{idx}", chunk))
|
| 106 |
+
if end == text_len:
|
| 107 |
+
break
|
| 108 |
+
start = end - overlap
|
| 109 |
+
idx += 1
|
| 110 |
+
return chunks
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def _get_embedding_model():
|
| 114 |
+
global _embedding_model
|
| 115 |
+
if _embedding_model is None:
|
| 116 |
+
if SentenceTransformer is None:
|
| 117 |
+
raise RuntimeError(
|
| 118 |
+
"sentence-transformers is required for retrieval demo. Install with `pip install sentence-transformers`."
|
| 119 |
+
)
|
| 120 |
+
_embedding_model = SentenceTransformer(EMBEDDING_MODEL_NAME)
|
| 121 |
+
return _embedding_model
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def _embed_texts(texts: list[str]) -> np.ndarray:
|
| 125 |
+
model = _get_embedding_model()
|
| 126 |
+
embeddings = model.encode(texts, convert_to_numpy=True, normalize_embeddings=True)
|
| 127 |
+
return embeddings.astype(np.float32)
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def handle_compress(file, raw_text):
|
| 131 |
+
try:
|
| 132 |
+
text_source = None
|
| 133 |
+
text_content = ""
|
| 134 |
+
if file is not None:
|
| 135 |
+
name, content = _extract_text_from_file(file)
|
| 136 |
+
text_source = name or "upload"
|
| 137 |
+
text_content = content or ""
|
| 138 |
+
elif raw_text and raw_text.strip():
|
| 139 |
+
text_source = "pasted"
|
| 140 |
+
text_content = raw_text
|
| 141 |
+
if not text_content or not text_content.strip():
|
| 142 |
+
return "No text provided.", "", "", "", None, gr.update(choices=list(docs_store.keys()), value=None)
|
| 143 |
+
|
| 144 |
+
doc_id = _next_doc_id()
|
| 145 |
+
docs_store[doc_id] = text_content
|
| 146 |
+
encoded = encode_text(
|
| 147 |
+
text_content,
|
| 148 |
+
window_bytes=WINDOW_BYTES,
|
| 149 |
+
stride_bytes=STRIDE_BYTES,
|
| 150 |
+
)
|
| 151 |
+
encodings_store[doc_id] = encoded
|
| 152 |
+
|
| 153 |
+
reconstruction = reconstruct_from_windows(encoded.windows, encoded.prototypes)
|
| 154 |
+
|
| 155 |
+
unique_sigs = len(encoded.prototypes)
|
| 156 |
+
bytes_before = encoded.original_bytes
|
| 157 |
+
# approximate storage using 9 bytes/signature (matches default precision)
|
| 158 |
+
bytes_after = unique_sigs * 9
|
| 159 |
+
compression_ratio = (bytes_before / bytes_after) if bytes_after else 0.0
|
| 160 |
+
stats = textwrap.dedent(
|
| 161 |
+
f"""
|
| 162 |
+
**doc_id**: {doc_id} ({text_source})
|
| 163 |
+
- windows: {len(encoded.windows)}
|
| 164 |
+
- unique signatures: {unique_sigs}
|
| 165 |
+
- hazard gate: ≤ {encoded.hazard_threshold:.4f}
|
| 166 |
+
- original bytes: {bytes_before}
|
| 167 |
+
- manifold payload bytes (~signatures): {bytes_after}
|
| 168 |
+
- compression ratio (approx): {compression_ratio:.2f}×
|
| 169 |
+
"""
|
| 170 |
+
).strip()
|
| 171 |
+
|
| 172 |
+
hazards = encoded.hazards
|
| 173 |
+
fig = _make_hazard_plot(hazards)
|
| 174 |
+
if encoded.hazard_threshold and hazards:
|
| 175 |
+
ax = fig.axes[0]
|
| 176 |
+
ax.axvline(encoded.hazard_threshold, color="red", linestyle="--", label="hazard gate")
|
| 177 |
+
ax.legend()
|
| 178 |
+
|
| 179 |
+
dropdown_update = gr.update(choices=list(docs_store.keys()), value=doc_id)
|
| 180 |
+
return (
|
| 181 |
+
f"Stored {doc_id}",
|
| 182 |
+
_preview(text_content),
|
| 183 |
+
_preview(reconstruction),
|
| 184 |
+
stats,
|
| 185 |
+
fig,
|
| 186 |
+
dropdown_update,
|
| 187 |
+
)
|
| 188 |
+
except Exception as exc: # pragma: no cover - UI surface
|
| 189 |
+
return f"Error: {exc}", "", "", "", None, gr.update(choices=list(docs_store.keys()), value=None)
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
def _ensure_index() -> Optional[ManifoldIndex]:
|
| 193 |
+
if not docs_store:
|
| 194 |
+
return None
|
| 195 |
+
return build_index(
|
| 196 |
+
docs_store,
|
| 197 |
+
window_bytes=WINDOW_BYTES,
|
| 198 |
+
stride_bytes=STRIDE_BYTES,
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def handle_verify(selected_doc, snippet, coverage_threshold):
|
| 203 |
+
if not snippet or not snippet.strip():
|
| 204 |
+
return "Provide a snippet to verify.", ""
|
| 205 |
+
index = _ensure_index()
|
| 206 |
+
if index is None:
|
| 207 |
+
return "No documents ingested yet.", ""
|
| 208 |
+
|
| 209 |
+
meta = getattr(index, "meta", {}) if hasattr(index, "meta") else {}
|
| 210 |
+
window_bytes = int(meta.get("window_bytes", WINDOW_BYTES))
|
| 211 |
+
default_hazard_threshold = float(meta.get("hazard_threshold", 0.8))
|
| 212 |
+
# hazard threshold slider is passed via bound partial; fallback to meta value
|
| 213 |
+
hazard_threshold = handle_verify.hazard_threshold # type: ignore[attr-defined]
|
| 214 |
+
if hazard_threshold is None:
|
| 215 |
+
hazard_threshold = default_hazard_threshold
|
| 216 |
+
|
| 217 |
+
snippet_bytes = len(snippet.encode("utf-8"))
|
| 218 |
+
too_short = snippet_bytes < window_bytes
|
| 219 |
+
|
| 220 |
+
result = verify_snippet(
|
| 221 |
+
snippet,
|
| 222 |
+
index,
|
| 223 |
+
coverage_threshold=coverage_threshold,
|
| 224 |
+
hazard_threshold=hazard_threshold,
|
| 225 |
+
window_bytes=WINDOW_BYTES,
|
| 226 |
+
stride_bytes=STRIDE_BYTES,
|
| 227 |
+
include_reconstruction=False,
|
| 228 |
+
)
|
| 229 |
+
total = max(result.total_windows, 1)
|
| 230 |
+
raw_hits = sum(1 for m in result.matches if m.get("matched"))
|
| 231 |
+
hazard_hits = sum(1 for m in result.matches if m.get("hazard_ok"))
|
| 232 |
+
raw_coverage = raw_hits / total
|
| 233 |
+
safe_coverage = hazard_hits / total
|
| 234 |
+
verified = safe_coverage >= coverage_threshold
|
| 235 |
+
status = "✅ Verified" if verified else "❌ Not verified"
|
| 236 |
+
status_color = "green" if verified else "red"
|
| 237 |
+
status_line = (
|
| 238 |
+
f"<span style='color:{status_color}; font-weight:700;'>{status}</span> "
|
| 239 |
+
f"(raw={raw_coverage*100:.2f}%, safe={safe_coverage*100:.2f}%, hazard_gate ≤ {hazard_threshold:.3f})"
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
lines = []
|
| 243 |
+
matched = [m for m in result.matches if m.get("occurrences")]
|
| 244 |
+
for match in matched[:20]:
|
| 245 |
+
sig = str(match.get("signature", ""))[:12]
|
| 246 |
+
hz = float(match.get("hazard", 0.0))
|
| 247 |
+
occ = match.get("occurrences", []) or []
|
| 248 |
+
first_doc = occ[0].get("doc_id") if occ else ""
|
| 249 |
+
lines.append(f"- `{sig}` hazard={hz:.3f} occurrences={len(occ)} doc={first_doc}")
|
| 250 |
+
matches_md = "\n".join(lines) if lines else "_No matches_"
|
| 251 |
+
if too_short and not lines:
|
| 252 |
+
matches_md = (
|
| 253 |
+
matches_md
|
| 254 |
+
+ f"\n\n_Note: snippet is {snippet_bytes} bytes; index windows are {window_bytes} bytes. "
|
| 255 |
+
"Use a longer snippet or build the index with a smaller window to improve coverage._"
|
| 256 |
+
)
|
| 257 |
+
if raw_hits and not hazard_hits:
|
| 258 |
+
matches_md = (
|
| 259 |
+
matches_md
|
| 260 |
+
+ "\n\n_Note: matching signatures exist but were filtered out by the hazard gate. "
|
| 261 |
+
"Raise the hazard threshold slider to test without gating._"
|
| 262 |
+
)
|
| 263 |
+
return status_line, matches_md
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
def handle_retrieve(question, top_k, coverage_threshold, hazard_threshold):
|
| 267 |
+
if not question or not question.strip():
|
| 268 |
+
return "Provide a question.", "", ""
|
| 269 |
+
if not docs_store:
|
| 270 |
+
return "No documents ingested yet.", "", ""
|
| 271 |
+
|
| 272 |
+
index = _ensure_index()
|
| 273 |
+
if index is None:
|
| 274 |
+
return "No documents ingested yet.", "", ""
|
| 275 |
+
|
| 276 |
+
# Build chunks
|
| 277 |
+
chunks = []
|
| 278 |
+
for doc_id, text in docs_store.items():
|
| 279 |
+
for chunk_id, chunk_text in _chunk_text(text):
|
| 280 |
+
chunks.append((doc_id, chunk_id, chunk_text))
|
| 281 |
+
if not chunks:
|
| 282 |
+
return "No chunks available to retrieve.", "", ""
|
| 283 |
+
|
| 284 |
+
chunk_texts = [c[2] for c in chunks]
|
| 285 |
+
chunk_embeddings = _embed_texts(chunk_texts)
|
| 286 |
+
question_embedding = _embed_texts([question])[0]
|
| 287 |
+
scores = np.dot(chunk_embeddings, question_embedding)
|
| 288 |
+
order = np.argsort(scores)[::-1]
|
| 289 |
+
top_indices = order[: int(top_k)]
|
| 290 |
+
|
| 291 |
+
naive_lines = []
|
| 292 |
+
verified_lines = []
|
| 293 |
+
for rank, idx in enumerate(top_indices, start=1):
|
| 294 |
+
doc_id, chunk_id, chunk_text = chunks[int(idx)]
|
| 295 |
+
score = float(scores[int(idx)])
|
| 296 |
+
naive_lines.append(f"- [{rank}] {doc_id}::{chunk_id} score={score:.3f}\n {chunk_text[:200]}...")
|
| 297 |
+
|
| 298 |
+
result = verify_snippet(
|
| 299 |
+
chunk_text,
|
| 300 |
+
index,
|
| 301 |
+
coverage_threshold=coverage_threshold,
|
| 302 |
+
hazard_threshold=hazard_threshold,
|
| 303 |
+
window_bytes=WINDOW_BYTES,
|
| 304 |
+
stride_bytes=STRIDE_BYTES,
|
| 305 |
+
include_reconstruction=False,
|
| 306 |
+
)
|
| 307 |
+
total = max(result.total_windows, 1)
|
| 308 |
+
raw_hits = sum(1 for m in result.matches if m.get("matched"))
|
| 309 |
+
hazard_hits = sum(1 for m in result.matches if m.get("hazard_ok"))
|
| 310 |
+
raw_coverage = raw_hits / total
|
| 311 |
+
safe_coverage = hazard_hits / total
|
| 312 |
+
status = "✅" if safe_coverage >= coverage_threshold else "❌"
|
| 313 |
+
verified_lines.append(
|
| 314 |
+
f"- [{rank}] {doc_id}::{chunk_id} {status} score={score:.3f} "
|
| 315 |
+
f"raw={raw_coverage*100:.2f}%, safe={safe_coverage*100:.2f}% "
|
| 316 |
+
f"(hazard_gate ≤ {hazard_threshold:.3f})"
|
| 317 |
+
)
|
| 318 |
+
naive_md = "\n".join(naive_lines) if naive_lines else "_No chunks_"
|
| 319 |
+
verified_md = "\n".join(verified_lines) if verified_lines else "_No verified chunks_"
|
| 320 |
+
return "Retrieved top-k chunks:", naive_md, verified_md
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
with gr.Blocks(title="Structural Manifold Sidecar") as demo:
|
| 324 |
+
gr.Markdown("# Structural Manifold Sidecar\nCompression + verification for RAG provenance.")
|
| 325 |
+
|
| 326 |
+
with gr.Tab("Compress & Reconstruct (Structural Manifolds)"):
|
| 327 |
+
gr.Markdown(
|
| 328 |
+
"Upload a document or paste text. We encode it into structural manifolds, reconstruct an approximate "
|
| 329 |
+
"version, and show compression + hazard stats."
|
| 330 |
+
)
|
| 331 |
+
file_input = gr.File(label="Upload (.pdf, .txt, .md)", file_types=[".pdf", ".txt", ".md"])
|
| 332 |
+
text_input = gr.Textbox(label="Or paste text", lines=6)
|
| 333 |
+
run_btn = gr.Button("Run structural manifold")
|
| 334 |
+
doc_msg = gr.Markdown()
|
| 335 |
+
original_box = gr.Textbox(label="Original (preview)", lines=10)
|
| 336 |
+
recon_box = gr.Textbox(label="Reconstruction (preview)", lines=10)
|
| 337 |
+
stats_box = gr.Markdown(label="Stats")
|
| 338 |
+
hazard_plot = gr.Plot(label="Hazard histogram")
|
| 339 |
+
|
| 340 |
+
with gr.Tab("Verify snippet"):
|
| 341 |
+
gr.Markdown(
|
| 342 |
+
"Paste any snippet. We re-encode it, look for matching manifold signatures in your ingested docs, "
|
| 343 |
+
"and compute hazard-gated coverage."
|
| 344 |
+
)
|
| 345 |
+
doc_dropdown = gr.Dropdown(
|
| 346 |
+
label="Docs ingested this session",
|
| 347 |
+
choices=list(docs_store.keys()),
|
| 348 |
+
interactive=True,
|
| 349 |
+
)
|
| 350 |
+
snippet_box = gr.Textbox(label="Snippet to verify", lines=6)
|
| 351 |
+
coverage_slider = gr.Slider(
|
| 352 |
+
minimum=0.0,
|
| 353 |
+
maximum=1.0,
|
| 354 |
+
value=0.5,
|
| 355 |
+
step=0.05,
|
| 356 |
+
label="Coverage threshold",
|
| 357 |
+
)
|
| 358 |
+
hazard_slider = gr.Slider(
|
| 359 |
+
minimum=0.0,
|
| 360 |
+
maximum=1.0,
|
| 361 |
+
value=0.8,
|
| 362 |
+
step=0.01,
|
| 363 |
+
label="Hazard gate (raise to be more permissive)",
|
| 364 |
+
)
|
| 365 |
+
verify_btn = gr.Button("Verify")
|
| 366 |
+
verify_status = gr.Markdown()
|
| 367 |
+
verify_matches = gr.Markdown()
|
| 368 |
+
|
| 369 |
+
if ENABLE_RETRIEVE:
|
| 370 |
+
with gr.Tab("Retrieve & Verify"):
|
| 371 |
+
gr.Markdown(
|
| 372 |
+
"Chunk-level RAG demo: retrieve top-k chunks via embeddings, then hazard-gate them with manifold verification."
|
| 373 |
+
)
|
| 374 |
+
question_box = gr.Textbox(label="Question / query", lines=3)
|
| 375 |
+
topk_slider = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Top-k chunks")
|
| 376 |
+
rag_coverage = gr.Slider(
|
| 377 |
+
minimum=0.0,
|
| 378 |
+
maximum=1.0,
|
| 379 |
+
value=0.5,
|
| 380 |
+
step=0.05,
|
| 381 |
+
label="Coverage threshold (verification)",
|
| 382 |
+
)
|
| 383 |
+
rag_hazard = gr.Slider(
|
| 384 |
+
minimum=0.0,
|
| 385 |
+
maximum=1.0,
|
| 386 |
+
value=0.8,
|
| 387 |
+
step=0.01,
|
| 388 |
+
label="Hazard gate (verification)",
|
| 389 |
+
)
|
| 390 |
+
retrieve_btn = gr.Button("Retrieve & verify")
|
| 391 |
+
retrieve_status = gr.Markdown()
|
| 392 |
+
naive_rag = gr.Markdown(label="Naive retrieval")
|
| 393 |
+
verified_rag = gr.Markdown(label="Hazard-gated retrieval (secondary demo)")
|
| 394 |
+
|
| 395 |
+
run_btn.click(
|
| 396 |
+
handle_compress,
|
| 397 |
+
inputs=[file_input, text_input],
|
| 398 |
+
outputs=[doc_msg, original_box, recon_box, stats_box, hazard_plot, doc_dropdown],
|
| 399 |
+
)
|
| 400 |
+
def bound_verify(snippet, coverage, hazard):
|
| 401 |
+
# stash hazard threshold on the function object so handle_verify can read it without changing signature
|
| 402 |
+
handle_verify.hazard_threshold = hazard # type: ignore[attr-defined]
|
| 403 |
+
return handle_verify(None, snippet, coverage)
|
| 404 |
+
|
| 405 |
+
verify_btn.click(
|
| 406 |
+
bound_verify,
|
| 407 |
+
inputs=[snippet_box, coverage_slider, hazard_slider],
|
| 408 |
+
outputs=[verify_status, verify_matches],
|
| 409 |
+
)
|
| 410 |
+
if ENABLE_RETRIEVE:
|
| 411 |
+
retrieve_btn.click(
|
| 412 |
+
handle_retrieve,
|
| 413 |
+
inputs=[question_box, topk_slider, rag_coverage, rag_hazard],
|
| 414 |
+
outputs=[retrieve_status, naive_rag, verified_rag],
|
| 415 |
+
)
|
| 416 |
+
|
| 417 |
+
|
| 418 |
+
if __name__ == "__main__":
|
| 419 |
+
demo.launch()
|
data/sample_docs/doc1.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
This is a tiny test document about liquidity and Q3 risk. It mentions cash buffers, credit lines, and how volatility affects capital allocation.
|
data/sample_docs/doc2.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
This second sample note talks about product launches and customer feedback loops. It does not mention liquidity or risk; it focuses on roadmap alignment instead.
|
docs/03_unit_economics.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Unit Economics: Structural Manifold Compression
|
| 2 |
+
|
| 3 |
+
## Scenario
|
| 4 |
+
- Enterprise corpus: **10M pages** (contracts, emails, logs).
|
| 5 |
+
- Baseline vector pipeline: embeddings + vector DB.
|
| 6 |
+
|
| 7 |
+
## Baseline Costs (Vector DB + embeddings)
|
| 8 |
+
- Storage footprint: ~10 TB (dense vectors).
|
| 9 |
+
- Embed pass: ~$2,000 (OpenAI-scale pricing).
|
| 10 |
+
- Monthly storage/query: ~$5,000/month (managed vector DB).
|
| 11 |
+
|
| 12 |
+
## With Structural Manifold Compression
|
| 13 |
+
- Footprint: **~250 GB** (≈40× smaller).
|
| 14 |
+
- Encode pass: **~$50** (CPU/GPU-friendly).
|
| 15 |
+
- Monthly storage: **~$50/month** (S3/Glacier class).
|
| 16 |
+
- Provenance: hazard-gated verification at window level; reconstruct-on-demand; on-device feasible.
|
| 17 |
+
|
| 18 |
+
## Business Impact
|
| 19 |
+
- **99%+ infra savings** on storage/query for context memory.
|
| 20 |
+
- **Auditable AI**: every retrieved chunk carries a structural “fingerprint” + hazard gate for trust.
|
| 21 |
+
- **Privacy**: indexes small enough for local/edge verification (no raw text upload required).
|
| 22 |
+
|
| 23 |
+
## Pitch Line
|
| 24 |
+
“We sell pure margin to AI companies: 40× smaller context memory with built-in provenance, reducing retrieval infra from ~$5k/month to ~$50/month for a 10M-page corpus.”
|
requirements.txt
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests>=2.31.0
|
| 2 |
+
pyyaml>=6.0.1
|
| 3 |
+
redis>=5.0.0
|
| 4 |
+
prometheus-client>=0.17.0
|
| 5 |
+
nvidia-ml-py3>=7.352.0
|
| 6 |
+
tqdm>=4.66.4
|
| 7 |
+
torch>=2.2.0
|
| 8 |
+
numpy>=1.26.4
|
| 9 |
+
transformers>=4.44.0
|
| 10 |
+
accelerate>=0.32.0
|
| 11 |
+
pillow>=10.3.0
|
| 12 |
+
addict>=2.4.0
|
| 13 |
+
matplotlib>=3.9.0
|
| 14 |
+
torchvision>=0.19.0
|
| 15 |
+
pandas>=2.2.0
|
| 16 |
+
pytest>=8.2.0
|
| 17 |
+
einops>=0.8.0
|
| 18 |
+
tensorboard>=2.16.0
|
| 19 |
+
peft>=0.11.1
|
| 20 |
+
huggingface-hub>=0.24.0
|
| 21 |
+
datasets>=2.20.0
|
| 22 |
+
pdfplumber>=0.11.0
|
| 23 |
+
sentence-transformers>=3.0.0
|
| 24 |
+
gradio>=4.44.0
|