"""Interactive Gradio workbench for DocMorph and SeekMorph.""" from __future__ import annotations import json from typing import Any from docmorph import DocumentMorpher, HybridIndex from seekmorph import ( KnowledgeItem, PersonalMemoryStore, ProactiveTicketRuntime, SeekMorphEngine, TriggerEvent, UserProfile, ) from tests.test_docmorph_5layers import ( run_layer_1_engineering_boundary, run_layer_2_structure_and_table_preservation, run_layer_3_information_retrieval_benchmark, run_layer_4_security_and_tenant_isolation, run_layer_5_agent_rag_citation_and_grounding, ) SAMPLE_MD = """# 2026 LLM Inference & Speculative Decoding Benchmark ## Hardware Throughput Matrix | Hardware Cluster | Model | Speculative Engine | Mean Acceptance (tokens) | Throughput (tok/s) | GSM8K Accuracy | | :--- | :--- | :--- | :--- | :--- | :--- | | 8x AMD MI308X | DeepSeek-V4.1-Flash | DSpark (Patched) | 2.95 | 312.5 | 99.0% | | 8x NVIDIA H100 | DeepSeek-V4.1-Flash | DSpark (Patched) | 3.05 | 328.4 | 99.1% | | 8x AMD MI308X | DeepSeek-V4.1-Flash | DSpark (Unpatched) | 0.00 | 104.2 | N/A | | 4x NVIDIA L40S | Qwen-2.5-72B-FP8 | Eagle-2 | 2.42 | 168.0 | 98.8% | | 8x NVIDIA H800 | Qwen-2.5-72B-FP8 | Eagle-2 | 2.61 | 275.6 | 99.2% | | 8x Intel Gaudi2 | DeepSeek-V4.1-Flash | DSpark (Patched) | 2.81 | 240.8 | 98.9% | ## Storage & Indexing Notes All benchmark telemetry logs are indexed in a thread-safe inverted index with pre-retrieval RBAC tenant isolation. """ SAMPLE_CSV = """region,enterprise_tier,q1_calls,q2_calls,daily_reach_per_rep,latency_ms Beijing,Enterprise_A,320000,410000,30,820 Shanghai,Enterprise_A,280000,360000,29,790 Shenzhen,Enterprise_B,195000,245000,28,840 Hangzhou,Enterprise_B,160000,210000,31,760 Guangzhou,Enterprise_C,145000,185000,27,880 Chengdu,Enterprise_C,110000,150000,26,910 """ SAMPLE_HTML = """

Storage Engine Specification

Write-Ahead Log (WAL)

Every transaction commit flushes records with fsync before returning ack.

ParameterDefaultDescription
wal_sync_methodfdatasyncForces WAL flush to disk
checkpoint_timeout5minMaximum time between automatic WAL checkpoints
max_wal_size16GBMaximum WAL size before triggering checkpoint
""" def run_docmorph_pipeline( raw_text: str, fmt: str, max_chars: int, namespace: str, query: str, top_k: int, expand_window: int, ) -> tuple[str, str, str]: morpher = DocumentMorpher(max_chars=int(max_chars)) chunks = morpher.morph_text( f"document.{fmt}", raw_text or "", fmt=fmt, namespace=(namespace or "default").strip(), ) chunk_cards: list[str] = [f"### Total Chunks Generated: `{len(chunks)}`\n"] jsonl_lines: list[str] = [] for i, c in enumerate(chunks, 1): breadcrumb = c.breadcrumb_str or "(Root)" chunk_cards.append( f"#### Chunk {i} | `kind={c.kind}` | `chars={len(c.content)}` | Path: `{breadcrumb}`\n\n" f"{c.content}\n\n---" ) jsonl_lines.append(json.dumps(c.to_dict(), ensure_ascii=False)) index = HybridIndex() index.add_many(chunks) search_md_parts: list[str] = [] if query and query.strip(): hits = index.search( query.strip(), top_k=int(top_k), namespace=(namespace or "default").strip(), expand_window=int(expand_window), ) if not hits: search_md_parts.append("No matching chunks found for query in this namespace.") else: for rank, h in enumerate(hits, 1): search_md_parts.append( f"#### Rank {rank} | `RRF={h.score:.4f}` | `BM25={h.bm25_score:.4f}` | " f"Path: `{h.chunk.breadcrumb_str or '(Root)'}`\n\n" f"{h.expanded_content}\n\n---" ) else: search_md_parts.append("Enter a search query to test BM25 + RRF retrieval.") return "\n".join(chunk_cards), "\n".join(search_md_parts), "\n".join(jsonl_lines) def run_seekmorph_pipeline( query: str, tenant_id: str, roles_csv: str, preferred_tags_csv: str, eval_mode: str, sender_id: str, ) -> tuple[str, str]: engine = SeekMorphEngine() corpus = [ KnowledgeItem( item_id="doc-vllm-dspark", title="vLLM DeepSeek-V4.1 DSpark Speculative Decoding Fix", content="Registers packed_modules_mapping across ROCm MI308X and CUDA, restoring 2.95 mean acceptance length.", tenant_id="acme", allowed_roles=("admin", "ml", "infra"), tags=("llm", "gpu", "vllm", "deepseek"), embedding=(0.92, 0.18, 0.05), clicks=86, impressions=180, age_days=1.0, ), KnowledgeItem( item_id="doc-hybrid-rag", title="Enterprise Multi-Tenant Hybrid Search & Table Chunking", content="Combines inverted-index BM25, dense cosine similarity, pre-retrieval RBAC, and table header preservation.", tenant_id="acme", allowed_roles=("admin", "ml", "sales"), tags=("rag", "search", "rbac"), embedding=(0.25, 0.88, 0.15), clicks=64, impressions=150, age_days=5.0, ), KnowledgeItem( item_id="doc-hr-secret", title="Executive Compensation Plan 2026", content="Confidential executive bonus pool and equity allocation.", tenant_id="acme", allowed_roles=("hr_exec",), tags=("hr", "finance"), embedding=(0.05, 0.05, 0.98), clicks=12, impressions=20, age_days=0.5, ), ] engine.add_many(corpus) roles = tuple(r.strip() for r in (roles_csv or "").split(",") if r.strip()) pref_tags = {t.strip(): 1.5 for t in (preferred_tags_csv or "").split(",") if t.strip()} user = UserProfile( user_id="u_demo", tenant_id=(tenant_id or "acme").strip(), roles=roles, interest_tags=pref_tags, ) hits = engine.search_and_recommend(query or "vllm dspark", user=user, top_k=5) if not hits: search_report = "No accessible items matched (filtered by pre-retrieval RBAC or zero lexical/semantic overlap)." else: rows = [ f"### Query Intent: `{hits[0].query_intent}` | Accessible Hits: `{len(hits)}`\n", "| Rank | Item ID | Title | Final Score | Lexical | Semantic | Personal | Recency |", "| :--- | :--- | :--- | :--- | :--- | :--- | :--- | :--- |", ] for i, h in enumerate(hits, 1): rows.append( f"| {i} | `{h.item.item_id}` | {h.item.title} | **{h.final_score:.4f}** | " f"{h.lexical_score:.3f} | {h.semantic_score:.3f} | {h.personal_score:.3f} | " f"{h.recency_score:.3f} |" ) search_report = "\n".join(rows) mem_store = PersonalMemoryStore() mem_store.remember( memory_id="mem-demo-1", user_id="u_demo", tenant_id=(tenant_id or "acme").strip(), title="Hardware & Inference Preference", content="Prefers FP8 speculative decoding benchmarks on MI308X with DSpark.", kind="preference", tags=("llm", "gpu"), ) recalled = mem_store.recall(user, query or "MI308X", top_k=2) runtime = ProactiveTicketRuntime( allowed_actors={"alice", "maintainer"}, tenant_id=(tenant_id or "acme").strip(), eval_mode=eval_mode, ) event = TriggerEvent( trigger_id="trig-demo", source="github", thread_id="issue-101", actor_id=(sender_id or "stranger").strip(), text=query or "Please check issue", addressed=True, ) ticket = runtime.receive(event) agent_report = ( f"### Personal Agent Memory & SuperAgent Gate\n\n" f"- **PersonalMemoryStore Recall (`u_demo`)**: `{recalled[0].item.content if recalled else 'None'}` " f"(clicks=`{recalled[0].item.clicks if recalled else 0}`)\n" f"- **ProactiveTicketRuntime Gate (`actor={sender_id}`, `eval_mode={eval_mode}`)**: " f"**{'ALLOWED (Ticket Running)' if ticket is not None else 'BLOCKED (Filtered by Host Gate)'}**" ) return search_report, agent_report def run_all_5_layers() -> str: layers: list[tuple[str, Any]] = [ ("Layer 1: Engineering Boundary & 20-Thread Concurrency", run_layer_1_engineering_boundary), ("Layer 2: Chunking Structure & Table Header Preservation", run_layer_2_structure_and_table_preservation), ("Layer 3: Information Retrieval (Recall@3 / MRR@3 / NDCG@3)", run_layer_3_information_retrieval_benchmark), ("Layer 4: Multi-Tenant Security & Zero Ghost Postings", run_layer_4_security_and_tenant_isolation), ("Layer 5: Agent Tool Grounding & Parallel Call Normalization", run_layer_5_agent_rag_citation_and_grounding), ] lines = ["### 5-Layer Production Benchmark Report\n"] for name, fn in layers: res = fn() lines.append(f"#### PASS - {name}") for k, v in res.items(): if k != "status": lines.append(f"- **{k}**: `{v}`") lines.append("") return "\n".join(lines) def build_ui(): import gradio as gr with gr.Blocks(title="DocMorph & SeekMorph Studio") as demo: gr.Markdown( "# DocMorph & SeekMorph Interactive Workbench\n" "Structure-aware document chunking (100% table-header preservation across chunks), " "multi-tenant hybrid retrieval (`BM25 + Dense RRF`), personalized reranking, and 5-layer quality verification.\n" "GitHub: [DocMorph](https://github.com/peterzhang987723/DocMorph) | " "[SeekMorph](https://github.com/peterzhang987723/SeekMorph)" ) with gr.Tabs(): with gr.Tab("1. DocMorph: Table-Header Preserving Chunker & Hybrid RAG"): with gr.Row(): btn_md = gr.Button("Load Sample: DeepSeek-V4 Benchmark Table (Markdown)") btn_csv = gr.Button("Load Sample: Enterprise Regional Metrics (CSV)") btn_html = gr.Button("Load Sample: Unclosed HTML Spec (Fault-Tolerant)") with gr.Row(): with gr.Column(scale=1): raw_input = gr.Textbox(label="Input Document", value=SAMPLE_MD, lines=14) with gr.Row(): fmt_input = gr.Dropdown( choices=["md", "csv", "html", "json", "txt"], value="md", label="Document Format", ) max_chars_input = gr.Slider( minimum=140, maximum=800, value=220, step=20, label="Max Characters per Chunk (forces table splitting)", ) with gr.Row(): ns_input = gr.Textbox(value="prod", label="Tenant Namespace") query_input = gr.Textbox(value="MI308X DSpark", label="Hybrid Search Query") with gr.Row(): topk_input = gr.Slider(minimum=1, maximum=5, value=3, step=1, label="Top-K") win_input = gr.Slider(minimum=0, maximum=2, value=0, step=1, label="Context Window Expansion") run_doc_btn = gr.Button("Morph Document & Run Hybrid Search", variant="primary") with gr.Column(scale=1): chunks_out = gr.Markdown(label="Structure-Aware Chunks") search_out = gr.Markdown(label="Hybrid Search Results") jsonl_out = gr.Code(label="Exported JSONL (Ready for RAG Ingestion)", language="json") btn_md.click(lambda: (SAMPLE_MD, "md", 220, "MI308X DSpark"), outputs=[raw_input, fmt_input, max_chars_input, query_input]) btn_csv.click(lambda: (SAMPLE_CSV, "csv", 160, "Beijing 30"), outputs=[raw_input, fmt_input, max_chars_input, query_input]) btn_html.click(lambda: (SAMPLE_HTML, "html", 200, "fdatasync"), outputs=[raw_input, fmt_input, max_chars_input, query_input]) run_doc_btn.click( run_docmorph_pipeline, inputs=[raw_input, fmt_input, max_chars_input, ns_input, query_input, topk_input, win_input], outputs=[chunks_out, search_out, jsonl_out], ) demo.load( run_docmorph_pipeline, inputs=[raw_input, fmt_input, max_chars_input, ns_input, query_input, topk_input, win_input], outputs=[chunks_out, search_out, jsonl_out], ) with gr.Tab("2. SeekMorph: Pre-Retrieval RBAC, Reranking & SuperAgent Gate"): with gr.Row(): with gr.Column(scale=1): sm_query = gr.Textbox(value="vllm dspark mi308x", label="Search Query") sm_tenant = gr.Textbox(value="acme", label="Tenant ID (try 'other' to verify tenant isolation)") sm_roles = gr.Textbox( value="admin, ml", label="User Roles (add 'hr_exec' to unlock confidential HR doc)", ) sm_tags = gr.Textbox(value="llm, gpu", label="User Preferred Tags") sm_eval = gr.Radio(choices=["off", "work", "all"], value="off", label="SuperAgent Eval Mode") sm_sender = gr.Textbox( value="stranger", label="Ticket Sender (try 'maintainer' vs 'stranger')", ) sm_btn = gr.Button("Execute SeekMorph Pipeline", variant="primary") with gr.Column(scale=1): sm_search_out = gr.Markdown() sm_agent_out = gr.Markdown() sm_btn.click( run_seekmorph_pipeline, inputs=[sm_query, sm_tenant, sm_roles, sm_tags, sm_eval, sm_sender], outputs=[sm_search_out, sm_agent_out], ) demo.load( run_seekmorph_pipeline, inputs=[sm_query, sm_tenant, sm_roles, sm_tags, sm_eval, sm_sender], outputs=[sm_search_out, sm_agent_out], ) with gr.Tab("3. Live 5-Layer Production Benchmark"): bench_btn = gr.Button("Run All 5 Verification Layers Live", variant="primary") bench_out = gr.Markdown() bench_btn.click(run_all_5_layers, outputs=[bench_out]) demo.load(run_all_5_layers, outputs=[bench_out]) return demo if __name__ == "__main__": ui = build_ui() ui.launch()