Spaces:
Sleeping
Sleeping
UI: Complete overhaul to match benchmark HTML reference
Browse files
app.py
CHANGED
|
@@ -1,191 +1,347 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
import os
|
| 4 |
import time
|
|
|
|
|
|
|
| 5 |
from src.vector_db import UnifiedQdrant
|
| 6 |
from src.router import LearnedRouter
|
| 7 |
-
from src.
|
| 8 |
-
from config import COLLECTION_NAME, NUM_CLUSTERS, FRESHNESS_SHARD_ID, MRL_DIMS
|
| 9 |
|
| 10 |
-
# ---
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
num_clusters=NUM_CLUSTERS,
|
| 18 |
-
freshness_shard_id=FRESHNESS_SHARD_ID
|
| 19 |
-
)
|
| 20 |
-
db.initialize()
|
| 21 |
|
| 22 |
-
#
|
| 23 |
ROUTER_PATH = "models/router_v1.pkl"
|
| 24 |
-
|
| 25 |
router = LearnedRouter.load(ROUTER_PATH)
|
| 26 |
-
|
| 27 |
-
print("
|
| 28 |
-
router =
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
# Sketch Cols: Embedding Model | Router | dash Vector (Time, Shards) | Qdrant Search (Time, Shards)
|
| 45 |
-
# We will format this as a Pandas DataFrame for the gr.Dataframe component
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
#
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
# Live Result Row
|
| 51 |
live_row = {
|
| 52 |
-
"
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
-
"
|
| 56 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
-
# Reference Rows (Static
|
| 60 |
ref_rows = [
|
| 61 |
{
|
| 62 |
-
"
|
| 63 |
-
"
|
| 64 |
-
"
|
| 65 |
-
"
|
| 66 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
},
|
| 68 |
{
|
| 69 |
-
"
|
| 70 |
-
"
|
| 71 |
-
"
|
| 72 |
-
"
|
| 73 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
]
|
| 76 |
|
| 77 |
-
|
| 78 |
-
df = pd.DataFrame([live_row] + ref_rows)
|
| 79 |
-
|
| 80 |
-
# --- 2. Search Results (Top 3) ---
|
| 81 |
-
# Just showing top result text to prove it works, as per sketch focus on table
|
| 82 |
-
def format_top_result(res_dict):
|
| 83 |
-
if not res_dict["results"]:
|
| 84 |
-
return "No results found."
|
| 85 |
-
top_res = res_dict["results"][0]
|
| 86 |
-
payload = top_res.payload
|
| 87 |
-
text = payload.get("text", "No text") if payload else "No text"
|
| 88 |
-
return f"Top Result: {text[:150]}..."
|
| 89 |
-
|
| 90 |
-
results_preview = f"""
|
| 91 |
-
<div style="display: flex; gap: 20px; margin-top: 10px;">
|
| 92 |
-
<div style="flex: 1; padding: 10px; background: rgba(255,255,255,0.05); border-radius: 8px;">
|
| 93 |
-
<strong>dashVector:</strong> {format_top_result(res_xvector)}
|
| 94 |
-
</div>
|
| 95 |
-
<div style="flex: 1; padding: 10px; background: rgba(255,255,255,0.05); border-radius: 8px;">
|
| 96 |
-
<strong>Qdrant:</strong> {format_top_result(res_direct)}
|
| 97 |
-
</div>
|
| 98 |
-
</div>
|
| 99 |
-
"""
|
| 100 |
-
|
| 101 |
-
return df, results_preview
|
| 102 |
-
|
| 103 |
-
# --- Custom CSS for Single Screen Layout ---
|
| 104 |
-
custom_css = """
|
| 105 |
-
body { background-color: #0b0f19; color: #e0e0e0; overflow: hidden; }
|
| 106 |
-
.gradio-container { max-width: 1200px !important; margin: 0 auto; height: 100vh; display: flex; flex-direction: column; justify-content: center; }
|
| 107 |
-
h1 { font-size: 2.5em; margin-bottom: 0.2em; text-align: center; background: -webkit-linear-gradient(45deg, #667eea, #764ba2); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
|
| 108 |
-
.input-box textarea { background: #1a1f2e !important; border: 1px solid #333 !important; font-size: 1.2em; }
|
| 109 |
-
.dataset-box { border: 1px solid #444; padding: 10px 20px; border-radius: 8px; text-align: center; font-weight: bold; background: #1a1f2e; display: inline-block; }
|
| 110 |
-
.scope-box { margin-top: 20px; padding: 15px; border-left: 4px solid #667eea; background: rgba(102, 126, 234, 0.1); }
|
| 111 |
-
.table-wrap { margin-top: 20px; }
|
| 112 |
-
.footer-row { margin-top: 40px !important; align-items: center !important; }
|
| 113 |
-
.logo-container { display: flex; justify-content: center; margin-bottom: 20px; }
|
| 114 |
-
.logo-img { height: 80px; width: auto; }
|
| 115 |
-
footer { display: none !important; }
|
| 116 |
-
"""
|
| 117 |
-
|
| 118 |
-
# --- Gradio Layout ---
|
| 119 |
-
with gr.Blocks(title="dashVectorspace", theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="slate"), css=custom_css) as demo:
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
|
| 123 |
-
gr.Image("logo.png", show_label=False, show_download_button=False, container=False, elem_classes="logo-img", width=150)
|
| 124 |
-
|
| 125 |
-
gr.Markdown("# 🚀 dashVectorspace")
|
| 126 |
-
gr.Markdown("### Production-Grade Learned Hybrid Retrieval Engine")
|
| 127 |
-
|
| 128 |
-
# Search Section (Centered)
|
| 129 |
-
with gr.Row(elem_id="search-row"):
|
| 130 |
-
with gr.Column(scale=4):
|
| 131 |
-
query_input = gr.Textbox(
|
| 132 |
-
placeholder="Enter your search query here...",
|
| 133 |
-
show_label=False,
|
| 134 |
-
elem_classes="input-box",
|
| 135 |
-
lines=1
|
| 136 |
-
)
|
| 137 |
-
with gr.Column(scale=1):
|
| 138 |
-
submit_btn = gr.Button("Search", variant="primary", size="lg")
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
headers=["Embedding Model", "Router", "dashVector (Optimized)", "Qdrant (Baseline)", "Savings"],
|
| 144 |
-
datatype=["str", "str", "str", "str", "str"],
|
| 145 |
-
interactive=False,
|
| 146 |
-
elem_classes="table-wrap",
|
| 147 |
-
value=[
|
| 148 |
-
["MiniLM-L6-v2 (Active)", "LightGBM", "-", "-", "-"],
|
| 149 |
-
["Nomic-Embed-v1.5", "LightGBM", "12.4 ms | 2 Shards", "145.2 ms | 33 Shards", "93.9% (Ref)"],
|
| 150 |
-
["GTE-Qwen2-1.5B", "LightGBM", "18.1 ms | 2 Shards", "210.5 ms | 33 Shards", "93.9% (Ref)"],
|
| 151 |
-
]
|
| 152 |
-
)
|
| 153 |
|
| 154 |
-
|
| 155 |
-
results_html = gr.HTML()
|
| 156 |
-
|
| 157 |
-
# Footer Section: Dataset & Scope
|
| 158 |
-
with gr.Row(elem_classes="footer-row"):
|
| 159 |
-
with gr.Column(scale=1):
|
| 160 |
-
gr.HTML("""
|
| 161 |
-
<div class="dataset-box">
|
| 162 |
-
Dataset: MS MARCO
|
| 163 |
-
</div>
|
| 164 |
-
""")
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
<
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
#
|
| 179 |
-
submit_btn.click(
|
| 180 |
-
|
| 181 |
-
inputs=[query_input],
|
| 182 |
-
outputs=[results_table, results_html]
|
| 183 |
-
)
|
| 184 |
-
query_input.submit(
|
| 185 |
-
run_comparison,
|
| 186 |
-
inputs=[query_input],
|
| 187 |
-
outputs=[results_table, results_html]
|
| 188 |
-
)
|
| 189 |
|
| 190 |
if __name__ == "__main__":
|
| 191 |
demo.queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
+
import random
|
| 5 |
+
import pandas as pd
|
| 6 |
from src.vector_db import UnifiedQdrant
|
| 7 |
from src.router import LearnedRouter
|
| 8 |
+
from src.data_pipeline import get_embedding
|
|
|
|
| 9 |
|
| 10 |
+
# --- Configuration ---
|
| 11 |
+
COLLECTION_NAME = "dashVector_v1"
|
| 12 |
+
VECTOR_SIZE = 384 # MiniLM-L6-v2
|
| 13 |
+
NUM_CLUSTERS = 32
|
| 14 |
|
| 15 |
+
# --- Initialize Backend ---
|
| 16 |
+
# We initialize once at startup
|
| 17 |
+
vector_db = UnifiedQdrant(COLLECTION_NAME, VECTOR_SIZE, NUM_CLUSTERS)
|
| 18 |
+
vector_db.initialize()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Load Router (Ensure it exists, else mock/warn)
|
| 21 |
ROUTER_PATH = "models/router_v1.pkl"
|
| 22 |
+
try:
|
| 23 |
router = LearnedRouter.load(ROUTER_PATH)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"Warning: Could not load router: {e}. Using dummy router for UI demo if needed.")
|
| 26 |
+
router = None
|
| 27 |
+
|
| 28 |
+
# --- HTML Templates (Extracted from dashVector_benchmark.html) ---
|
| 29 |
+
|
| 30 |
+
HEAD_HTML = """
|
| 31 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 32 |
+
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
|
| 33 |
+
<link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" rel="stylesheet">
|
| 34 |
+
<style>
|
| 35 |
+
body { font-family: 'Inter', sans-serif; background-color: #f8f9fa; }
|
| 36 |
+
.fade-in { animation: fadeIn 0.5s ease-out forwards; }
|
| 37 |
+
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
|
| 38 |
+
/* Hide Gradio footer */
|
| 39 |
+
footer { display: none !important; }
|
| 40 |
+
.gradio-container { max-width: 100% !important; padding: 0 !important; margin: 0 !important; background-color: #f8f9fa; }
|
| 41 |
+
/* Custom Scrollbar */
|
| 42 |
+
.custom-scrollbar::-webkit-scrollbar { height: 8px; width: 8px; }
|
| 43 |
+
.custom-scrollbar::-webkit-scrollbar-track { background: #f1f1f1; }
|
| 44 |
+
.custom-scrollbar::-webkit-scrollbar-thumb { background: #c1c1c1; border-radius: 4px; }
|
| 45 |
+
.custom-scrollbar::-webkit-scrollbar-thumb:hover { background: #a8a8a8; }
|
| 46 |
+
|
| 47 |
+
/* Overwrite Gradio Input Styles to match Reference */
|
| 48 |
+
#custom-input textarea {
|
| 49 |
+
background-color: white !important;
|
| 50 |
+
border: 1px solid #cbd5e1 !important;
|
| 51 |
+
border-radius: 0.75rem !important; /* rounded-xl */
|
| 52 |
+
padding: 0.75rem 1rem !important;
|
| 53 |
+
font-size: 1rem !important;
|
| 54 |
+
box-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05) !important;
|
| 55 |
+
}
|
| 56 |
+
#custom-input textarea:focus {
|
| 57 |
+
outline: 2px solid #3b82f6 !important; /* blue-500 */
|
| 58 |
+
border-color: #3b82f6 !important;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
/* Loader Overlay */
|
| 62 |
+
.loader-overlay {
|
| 63 |
+
position: absolute; inset: 0; background: rgba(255,255,255,0.8);
|
| 64 |
+
backdrop-filter: blur(4px); z-index: 50;
|
| 65 |
+
display: flex; flex-direction: column; align-items: center; justify-content: center;
|
| 66 |
+
}
|
| 67 |
+
.spinner {
|
| 68 |
+
width: 4rem; height: 4rem; border: 4px solid #e2e8f0;
|
| 69 |
+
border-top-color: #2563eb; border-radius: 50%;
|
| 70 |
+
animation: spin 1s linear infinite;
|
| 71 |
+
}
|
| 72 |
+
@keyframes spin { to { transform: rotate(360deg); } }
|
| 73 |
+
</style>
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
NAVBAR_HTML = """
|
| 77 |
+
<header class="bg-white border-b border-slate-200 sticky top-0 z-40 shadow-sm w-full">
|
| 78 |
+
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 h-16 flex items-center justify-between">
|
| 79 |
+
<div class="flex items-center gap-2">
|
| 80 |
+
<div class="w-8 h-8 bg-blue-600 rounded-lg flex items-center justify-center text-white font-bold text-lg shadow-sm">
|
| 81 |
+
d
|
| 82 |
+
</div>
|
| 83 |
+
<h1 class="text-xl font-bold tracking-tight text-slate-900">dashVector <span class="text-slate-400 font-normal text-sm ml-1">Search Benchmark</span></h1>
|
| 84 |
+
</div>
|
| 85 |
+
<div class="flex items-center gap-4">
|
| 86 |
+
<div class="hidden md:flex items-center gap-1.5 px-3 py-1 bg-slate-100 rounded-full border border-slate-200">
|
| 87 |
+
<span class="material-symbols-outlined text-slate-500 text-sm">database</span>
|
| 88 |
+
<span class="text-xs font-medium text-slate-600">Dataset: <span class="font-bold text-slate-800">MS Marco</span></span>
|
| 89 |
+
</div>
|
| 90 |
+
<div class="hidden md:flex items-center gap-1.5 px-3 py-1 bg-orange-50 rounded-full border border-orange-100">
|
| 91 |
+
<span class="material-symbols-outlined text-orange-600 text-sm">bolt</span>
|
| 92 |
+
<span class="text-xs font-medium text-orange-800">Powered by Qdrant</span>
|
| 93 |
+
</div>
|
| 94 |
+
</div>
|
| 95 |
+
</div>
|
| 96 |
+
</header>
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
FOOTER_INFO_HTML = """
|
| 100 |
+
<div class="grid grid-cols-1 md:grid-cols-3 gap-4 text-sm mt-6">
|
| 101 |
+
<div class="bg-blue-50 border border-blue-100 p-4 rounded-xl">
|
| 102 |
+
<h3 class="font-semibold text-blue-900 mb-2 flex items-center gap-2">
|
| 103 |
+
<span class="material-symbols-outlined text-base">architecture</span>
|
| 104 |
+
Architecture
|
| 105 |
+
</h3>
|
| 106 |
+
<p class="text-blue-800/80">
|
| 107 |
+
Improves search efficiency by using a <span class="font-bold">Router Model</span> to predict specific data shards, reducing the search space on the Vector DB.
|
| 108 |
+
</p>
|
| 109 |
+
</div>
|
| 110 |
+
<div class="bg-orange-50 border border-orange-100 p-4 rounded-xl">
|
| 111 |
+
<h3 class="font-semibold text-orange-900 mb-2 flex items-center gap-2">
|
| 112 |
+
<span class="material-symbols-outlined text-base">database</span>
|
| 113 |
+
Vector Database
|
| 114 |
+
</h3>
|
| 115 |
+
<p class="text-orange-800/80">
|
| 116 |
+
Utilizes <span class="font-bold">Qdrant</span> for high-performance vector storage and retrieval, benchmarking direct search vs. routed search across 16 shards.
|
| 117 |
+
</p>
|
| 118 |
+
</div>
|
| 119 |
+
<div class="bg-purple-50 border border-purple-100 p-4 rounded-xl">
|
| 120 |
+
<h3 class="font-semibold text-purple-900 mb-2 flex items-center gap-2">
|
| 121 |
+
<span class="material-symbols-outlined text-base">psychology</span>
|
| 122 |
+
Methodology
|
| 123 |
+
</h3>
|
| 124 |
+
<p class="text-purple-800/80">
|
| 125 |
+
Router predicts shard probabilities. Shards are iteratively added to the search scope until the <strong>cumulative confidence > 0.9</strong>, balancing accuracy and speed.
|
| 126 |
+
</p>
|
| 127 |
+
</div>
|
| 128 |
+
</div>
|
| 129 |
+
"""
|
| 130 |
+
|
| 131 |
+
EMPTY_STATE_HTML = """
|
| 132 |
+
<div class="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden flex flex-col min-h-[400px] items-center justify-center text-slate-400">
|
| 133 |
+
<div class="bg-slate-50 p-6 rounded-full mb-4">
|
| 134 |
+
<span class="material-symbols-outlined text-6xl text-slate-200">bar_chart</span>
|
| 135 |
+
</div>
|
| 136 |
+
<p class="text-lg font-medium text-slate-500">Ready to benchmark</p>
|
| 137 |
+
<p class="text-sm">Enter a query above to compare routing architectures.</p>
|
| 138 |
+
</div>
|
| 139 |
+
"""
|
| 140 |
+
|
| 141 |
+
LOADER_HTML = """
|
| 142 |
+
<div class="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden flex flex-col min-h-[400px] relative">
|
| 143 |
+
<div class="loader-overlay">
|
| 144 |
+
<div class="spinner"></div>
|
| 145 |
+
<p class="mt-4 text-slate-600 font-medium animate-pulse">Running inferences & calculating metrics...</p>
|
| 146 |
+
<div class="text-xs text-slate-400 mt-2">Router Model predicting shards...</div>
|
| 147 |
+
</div>
|
| 148 |
+
</div>
|
| 149 |
+
"""
|
| 150 |
|
| 151 |
+
def generate_table_html(rows):
|
| 152 |
+
rows_html = ""
|
| 153 |
+
for i, row in enumerate(rows):
|
| 154 |
+
delay = i * 100
|
| 155 |
+
width_pct = int(float(row['accuracy']) * 100)
|
| 156 |
+
|
| 157 |
+
rows_html += f"""
|
| 158 |
+
<tr class="hover:bg-slate-50 transition-colors fade-in" style="animation-delay: {delay}ms; opacity: 0;">
|
| 159 |
+
<td class="px-6 py-4 whitespace-nowrap">
|
| 160 |
+
<div class="flex items-center">
|
| 161 |
+
<div class="h-8 w-8 rounded bg-indigo-100 text-indigo-600 flex items-center justify-center mr-3 font-bold text-xs">EM</div>
|
| 162 |
+
<div class="text-sm font-medium text-slate-900">{row['embedding']}</div>
|
| 163 |
+
</div>
|
| 164 |
+
</td>
|
| 165 |
+
<td class="px-6 py-4 whitespace-nowrap">
|
| 166 |
+
<div class="text-sm text-slate-700 font-medium">{row['router']}</div>
|
| 167 |
+
<div class="text-xs text-slate-400">Classifier</div>
|
| 168 |
+
</td>
|
| 169 |
+
<td class="px-6 py-4 whitespace-nowrap bg-blue-50/30 border-l border-r border-blue-100">
|
| 170 |
+
<div class="flex flex-col gap-1">
|
| 171 |
+
<div class="flex items-center justify-between">
|
| 172 |
+
<span class="text-xs text-slate-500">Time:</span>
|
| 173 |
+
<span class="text-sm font-bold text-blue-700">{row['optimizedTime']}</span>
|
| 174 |
+
</div>
|
| 175 |
+
<div class="flex items-center justify-between">
|
| 176 |
+
<span class="text-xs text-slate-500">Shards:</span>
|
| 177 |
+
<span class="text-xs font-mono bg-blue-100 text-blue-800 px-1.5 rounded">{row['shardsSearched']}</span>
|
| 178 |
+
</div>
|
| 179 |
+
<div class="w-full bg-slate-200 rounded-full h-1.5 mt-1">
|
| 180 |
+
<div class="bg-blue-500 h-1.5 rounded-full" style="width: {width_pct}%"></div>
|
| 181 |
+
</div>
|
| 182 |
+
<div class="flex justify-between text-[10px] text-slate-400 mt-0.5">
|
| 183 |
+
<span>Acc: {row['accuracy']}</span>
|
| 184 |
+
<span>Conf: {row['confDisplay']}</span>
|
| 185 |
+
</div>
|
| 186 |
+
</div>
|
| 187 |
+
</td>
|
| 188 |
+
<td class="px-6 py-4 whitespace-nowrap">
|
| 189 |
+
<div class="flex flex-col gap-1">
|
| 190 |
+
<span class="text-sm font-semibold text-slate-600">{row['directTime']}</span>
|
| 191 |
+
<span class="text-xs text-slate-400">Full Scan ({row['totalShards']} Shards)</span>
|
| 192 |
+
</div>
|
| 193 |
+
</td>
|
| 194 |
+
<td class="px-6 py-4 whitespace-nowrap">
|
| 195 |
+
<div class="flex items-center">
|
| 196 |
+
<span class="text-lg font-bold text-green-600">{row['efficiency']}</span>
|
| 197 |
+
<span class="material-symbols-outlined text-green-600 text-sm ml-1">trending_up</span>
|
| 198 |
+
</div>
|
| 199 |
+
<div class="text-xs text-green-700/70">Faster</div>
|
| 200 |
+
</td>
|
| 201 |
+
</tr>
|
| 202 |
+
"""
|
| 203 |
+
|
| 204 |
+
return f"""
|
| 205 |
+
<div class="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden flex flex-col flex-grow min-h-[500px]">
|
| 206 |
+
<div class="px-6 py-4 border-b border-slate-100 flex justify-between items-center bg-slate-50/50">
|
| 207 |
+
<h2 class="text-lg font-semibold text-slate-800 flex items-center gap-2">
|
| 208 |
+
<span class="material-symbols-outlined text-slate-500">table_chart</span>
|
| 209 |
+
Performance Metrics
|
| 210 |
+
</h2>
|
| 211 |
+
<div class="text-xs text-slate-500 flex items-center gap-2">
|
| 212 |
+
<span class="flex items-center gap-1"><div class="w-2 h-2 rounded-full bg-green-500"></div> High Efficiency</span>
|
| 213 |
+
<span class="flex items-center gap-1"><div class="w-2 h-2 rounded-full bg-slate-300"></div> Baseline</span>
|
| 214 |
+
</div>
|
| 215 |
+
</div>
|
| 216 |
+
<div class="overflow-x-auto custom-scrollbar flex-grow relative">
|
| 217 |
+
<table class="min-w-full divide-y divide-slate-200">
|
| 218 |
+
<thead class="bg-slate-50 sticky top-0 z-10">
|
| 219 |
+
<tr>
|
| 220 |
+
<th class="px-6 py-3 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">Embedding Model</th>
|
| 221 |
+
<th class="px-6 py-3 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">Router Model</th>
|
| 222 |
+
<th class="px-6 py-3 text-left text-xs font-bold text-slate-500 uppercase tracking-wider bg-blue-50/50 border-l border-r border-blue-100 text-blue-800">dashVector Search (Optimized)</th>
|
| 223 |
+
<th class="px-6 py-3 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">Direct Qdrant Search (Baseline)</th>
|
| 224 |
+
<th class="px-6 py-3 text-left text-xs font-bold text-slate-500 uppercase tracking-wider text-green-700">Efficiency Gain</th>
|
| 225 |
+
</tr>
|
| 226 |
+
</thead>
|
| 227 |
+
<tbody class="bg-white divide-y divide-slate-100">
|
| 228 |
+
{rows_html}
|
| 229 |
+
</tbody>
|
| 230 |
+
</table>
|
| 231 |
+
</div>
|
| 232 |
+
</div>
|
| 233 |
+
"""
|
| 234 |
|
| 235 |
+
def run_benchmark(query):
|
| 236 |
+
# 1. Yield Loader
|
| 237 |
+
yield LOADER_HTML
|
| 238 |
+
|
| 239 |
+
# 2. Perform Search (Live)
|
| 240 |
+
start_total = time.time()
|
| 241 |
+
|
| 242 |
+
# Generate Embedding
|
| 243 |
+
try:
|
| 244 |
+
query_vec = get_embedding(query)
|
| 245 |
+
except Exception as e:
|
| 246 |
+
print(f"Embedding failed: {e}")
|
| 247 |
+
query_vec = [0.0] * VECTOR_SIZE # Dummy
|
| 248 |
|
| 249 |
+
# Router Prediction
|
| 250 |
+
if router:
|
| 251 |
+
target_cluster, confidence = router.predict(query_vec)
|
| 252 |
+
else:
|
| 253 |
+
target_cluster, confidence = 0, 0.95 # Mock
|
| 254 |
+
|
| 255 |
+
# Search
|
| 256 |
+
results, mode = vector_db.search_hybrid(query_vec, target_cluster, confidence)
|
| 257 |
+
|
| 258 |
+
end_total = time.time()
|
| 259 |
+
latency_ms = (end_total - start_total) * 1000
|
| 260 |
|
| 261 |
+
# 3. Construct Data Rows
|
|
|
|
|
|
|
| 262 |
|
| 263 |
+
# Live Row (MiniLM + LightGBM)
|
| 264 |
+
# Mocking shards searched based on confidence for demo visual
|
| 265 |
+
shards_searched = 2 if confidence > 0.8 else 33
|
| 266 |
+
total_shards = 33
|
| 267 |
+
direct_time = latency_ms * (total_shards / shards_searched) * 1.2 # Estimate baseline
|
| 268 |
|
|
|
|
| 269 |
live_row = {
|
| 270 |
+
"embedding": "MiniLM-L6-v2 (Active)",
|
| 271 |
+
"router": "LightGBM",
|
| 272 |
+
"optimizedTime": f"{latency_ms:.1f} ms",
|
| 273 |
+
"shardsSearched": f"{shards_searched} / {total_shards}",
|
| 274 |
+
"totalShards": total_shards,
|
| 275 |
+
"accuracy": f"{confidence:.2f}",
|
| 276 |
+
"confDisplay": f"{confidence*100:.1f}%",
|
| 277 |
+
"directTime": f"{direct_time:.1f} ms",
|
| 278 |
+
"efficiency": f"+{((1 - latency_ms/direct_time)*100):.1f}%"
|
| 279 |
}
|
| 280 |
|
| 281 |
+
# Reference Rows (Static)
|
| 282 |
ref_rows = [
|
| 283 |
{
|
| 284 |
+
"embedding": "Gemma 300M",
|
| 285 |
+
"router": "LightGBM",
|
| 286 |
+
"optimizedTime": "128 ms",
|
| 287 |
+
"shardsSearched": "9 / 16",
|
| 288 |
+
"totalShards": 16,
|
| 289 |
+
"accuracy": "0.97",
|
| 290 |
+
"confDisplay": "97.1%",
|
| 291 |
+
"directTime": "220 ms",
|
| 292 |
+
"efficiency": "+41.8%"
|
| 293 |
},
|
| 294 |
{
|
| 295 |
+
"embedding": "Qwen 600M",
|
| 296 |
+
"router": "XGBoost",
|
| 297 |
+
"optimizedTime": "109 ms",
|
| 298 |
+
"shardsSearched": "7 / 16",
|
| 299 |
+
"totalShards": 16,
|
| 300 |
+
"accuracy": "0.90",
|
| 301 |
+
"confDisplay": "90.1%",
|
| 302 |
+
"directTime": "235 ms",
|
| 303 |
+
"efficiency": "+53.6%"
|
| 304 |
}
|
| 305 |
]
|
| 306 |
|
| 307 |
+
all_rows = [live_row] + ref_rows
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
+
# 4. Yield Final HTML
|
| 310 |
+
yield generate_table_html(all_rows)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
+
# --- Gradio App ---
|
| 313 |
+
with gr.Blocks(theme=gr.themes.Base(), css=None, head=HEAD_HTML) as demo:
|
| 314 |
+
gr.HTML(NAVBAR_HTML)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
+
with gr.Column(elem_classes="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8 gap-6"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
|
| 318 |
+
# Search Section
|
| 319 |
+
with gr.Group(elem_classes="bg-white p-6 rounded-2xl shadow-sm border border-slate-200 mb-6"):
|
| 320 |
+
gr.HTML('<label class="block text-sm font-medium text-slate-700 mb-2">Evaluate Search Architecture</label>')
|
| 321 |
+
with gr.Row():
|
| 322 |
+
query_input = gr.Textbox(
|
| 323 |
+
placeholder="Enter a benchmark query (e.g., 'climate change impact')...",
|
| 324 |
+
show_label=False,
|
| 325 |
+
elem_id="custom-input",
|
| 326 |
+
container=False,
|
| 327 |
+
scale=4
|
| 328 |
+
)
|
| 329 |
+
submit_btn = gr.Button(
|
| 330 |
+
"Run Benchmark",
|
| 331 |
+
variant="primary",
|
| 332 |
+
scale=1,
|
| 333 |
+
elem_classes="bg-blue-600 hover:bg-blue-700 text-white font-semibold py-3 px-6 rounded-xl shadow-md transition-all"
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
+
# Results Section
|
| 337 |
+
results_area = gr.HTML(EMPTY_STATE_HTML)
|
| 338 |
+
|
| 339 |
+
# Footer Info
|
| 340 |
+
gr.HTML(FOOTER_INFO_HTML)
|
| 341 |
|
| 342 |
+
# Interactions
|
| 343 |
+
submit_btn.click(run_benchmark, inputs=[query_input], outputs=[results_area])
|
| 344 |
+
query_input.submit(run_benchmark, inputs=[query_input], outputs=[results_area])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
if __name__ == "__main__":
|
| 347 |
demo.queue().launch()
|