justmotes commited on
Commit
9dd1bf5
·
1 Parent(s): e6a96ed

Fix: Add try-except block to catch runtime errors and fix CSS

Browse files
Files changed (1) hide show
  1. app.py +88 -73
app.py CHANGED
@@ -65,8 +65,9 @@ HEAD_HTML = """
65
  .search-row {
66
  display: flex !important;
67
  flex-direction: row !important;
68
- align-items: flex-start !important; /* Align top to handle potential textarea growth */
69
  gap: 1rem !important;
 
70
  }
71
 
72
  /* Loader Overlay */
@@ -244,86 +245,100 @@ def run_benchmark(query):
244
  # 1. Yield Loader
245
  yield LOADER_HTML
246
 
247
- # 2. Perform Search (Live)
248
- start_total = time.time()
249
-
250
- # Generate Embedding
251
  try:
 
 
 
 
252
  print("DEBUG: Generating embedding...")
253
  query_vec = get_embedding(query)
254
  print("DEBUG: Embedding generated.")
255
- except Exception as e:
256
- print(f"ERROR: Embedding failed: {e}")
257
- query_vec = [0.0] * VECTOR_SIZE # Dummy
258
 
259
- # Router Prediction
260
- if router:
261
- print("DEBUG: Predicting cluster...")
262
- target_cluster, confidence = router.predict(query_vec)
263
- print(f"DEBUG: Predicted cluster {target_cluster} with confidence {confidence}")
264
- else:
265
- print("DEBUG: No router loaded, using mock.")
266
- target_cluster, confidence = 0, 0.95 # Mock
 
 
 
 
 
267
 
268
- # Search
269
- print("DEBUG: Searching Qdrant...")
270
- results, mode = vector_db.search_hybrid(query_vec, target_cluster, confidence)
271
- print(f"DEBUG: Search complete. Found {len(results)} results.")
272
-
273
- end_total = time.time()
274
- latency_ms = (end_total - start_total) * 1000
275
-
276
- # 3. Construct Data Rows
277
-
278
- # Live Row (MiniLM + LightGBM)
279
- # Mocking shards searched based on confidence for demo visual
280
- shards_searched = 2 if confidence > 0.8 else 33
281
- total_shards = 33
282
- direct_time = latency_ms * (total_shards / shards_searched) * 1.2 # Estimate baseline
283
-
284
- live_row = {
285
- "embedding": "MiniLM-L6-v2 (Active)",
286
- "router": "LightGBM",
287
- "optimizedTime": f"{latency_ms:.1f} ms",
288
- "shardsSearched": f"{shards_searched} / {total_shards}",
289
- "totalShards": total_shards,
290
- "accuracy": f"{confidence:.2f}",
291
- "confDisplay": f"{confidence*100:.1f}%",
292
- "directTime": f"{direct_time:.1f} ms",
293
- "efficiency": f"+{((1 - latency_ms/direct_time)*100):.1f}%"
294
- }
295
-
296
- # Reference Rows (Static)
297
- ref_rows = [
298
- {
299
- "embedding": "Gemma 300M",
300
  "router": "LightGBM",
301
- "optimizedTime": "128 ms",
302
- "shardsSearched": "9 / 16",
303
- "totalShards": 16,
304
- "accuracy": "0.97",
305
- "confDisplay": "97.1%",
306
- "directTime": "220 ms",
307
- "efficiency": "+41.8%"
308
- },
309
- {
310
- "embedding": "Qwen 600M",
311
- "router": "XGBoost",
312
- "optimizedTime": "109 ms",
313
- "shardsSearched": "7 / 16",
314
- "totalShards": 16,
315
- "accuracy": "0.90",
316
- "confDisplay": "90.1%",
317
- "directTime": "235 ms",
318
- "efficiency": "+53.6%"
319
  }
320
- ]
321
-
322
- all_rows = [live_row] + ref_rows
323
-
324
- print("DEBUG: Yielding final HTML.")
325
- # 4. Yield Final HTML
326
- yield generate_table_html(all_rows)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
 
328
  # --- Gradio App ---
329
  with gr.Blocks(theme=gr.themes.Base(), css=None, head=HEAD_HTML) as demo:
 
65
  .search-row {
66
  display: flex !important;
67
  flex-direction: row !important;
68
+ align-items: flex-start !important;
69
  gap: 1rem !important;
70
+ flex-wrap: nowrap !important; /* Prevent wrapping */
71
  }
72
 
73
  /* Loader Overlay */
 
245
  # 1. Yield Loader
246
  yield LOADER_HTML
247
 
 
 
 
 
248
  try:
249
+ # 2. Perform Search (Live)
250
+ start_total = time.time()
251
+
252
+ # Generate Embedding
253
  print("DEBUG: Generating embedding...")
254
  query_vec = get_embedding(query)
255
  print("DEBUG: Embedding generated.")
 
 
 
256
 
257
+ # Router Prediction
258
+ if router:
259
+ print("DEBUG: Predicting cluster...")
260
+ target_cluster, confidence = router.predict(query_vec)
261
+ print(f"DEBUG: Predicted cluster {target_cluster} with confidence {confidence}")
262
+ else:
263
+ print("DEBUG: No router loaded, using mock.")
264
+ target_cluster, confidence = 0, 0.95 # Mock
265
+
266
+ # Search
267
+ print("DEBUG: Searching Qdrant...")
268
+ results, mode = vector_db.search_hybrid(query_vec, target_cluster, confidence)
269
+ print(f"DEBUG: Search complete. Found {len(results)} results.")
270
 
271
+ end_total = time.time()
272
+ latency_ms = (end_total - start_total) * 1000
273
+
274
+ # 3. Construct Data Rows
275
+
276
+ # Live Row (MiniLM + LightGBM)
277
+ # Mocking shards searched based on confidence for demo visual
278
+ shards_searched = 2 if confidence > 0.8 else 33
279
+ total_shards = 33
280
+ direct_time = latency_ms * (total_shards / shards_searched) * 1.2 # Estimate baseline
281
+
282
+ live_row = {
283
+ "embedding": "MiniLM-L6-v2 (Active)",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
  "router": "LightGBM",
285
+ "optimizedTime": f"{latency_ms:.1f} ms",
286
+ "shardsSearched": f"{shards_searched} / {total_shards}",
287
+ "totalShards": total_shards,
288
+ "accuracy": f"{confidence:.2f}",
289
+ "confDisplay": f"{confidence*100:.1f}%",
290
+ "directTime": f"{direct_time:.1f} ms",
291
+ "efficiency": f"+{((1 - latency_ms/direct_time)*100):.1f}%"
 
 
 
 
 
 
 
 
 
 
 
292
  }
293
+
294
+ # Reference Rows (Static)
295
+ ref_rows = [
296
+ {
297
+ "embedding": "Gemma 300M",
298
+ "router": "LightGBM",
299
+ "optimizedTime": "128 ms",
300
+ "shardsSearched": "9 / 16",
301
+ "totalShards": 16,
302
+ "accuracy": "0.97",
303
+ "confDisplay": "97.1%",
304
+ "directTime": "220 ms",
305
+ "efficiency": "+41.8%"
306
+ },
307
+ {
308
+ "embedding": "Qwen 600M",
309
+ "router": "XGBoost",
310
+ "optimizedTime": "109 ms",
311
+ "shardsSearched": "7 / 16",
312
+ "totalShards": 16,
313
+ "accuracy": "0.90",
314
+ "confDisplay": "90.1%",
315
+ "directTime": "235 ms",
316
+ "efficiency": "+53.6%"
317
+ }
318
+ ]
319
+
320
+ all_rows = [live_row] + ref_rows
321
+
322
+ print("DEBUG: Yielding final HTML.")
323
+ # 4. Yield Final HTML
324
+ yield generate_table_html(all_rows)
325
+
326
+ except Exception as e:
327
+ import traceback
328
+ error_msg = traceback.format_exc()
329
+ print(f"CRITICAL ERROR in run_benchmark: {error_msg}")
330
+
331
+ # Yield Error HTML
332
+ yield f"""
333
+ <div class="bg-red-50 border border-red-200 rounded-2xl p-6 text-red-800">
334
+ <h3 class="font-bold text-lg mb-2 flex items-center gap-2">
335
+ <span class="material-symbols-outlined">error</span>
336
+ Runtime Error
337
+ </h3>
338
+ <p class="mb-4">An error occurred while running the benchmark:</p>
339
+ <pre class="bg-red-100 p-4 rounded-lg text-xs font-mono overflow-x-auto">{error_msg}</pre>
340
+ </div>
341
+ """
342
 
343
  # --- Gradio App ---
344
  with gr.Blocks(theme=gr.themes.Base(), css=None, head=HEAD_HTML) as demo: