justmotes commited on
Commit
fefd41d
·
1 Parent(s): 6536223

Fix: Replace streaming with chained events to resolve ProtocolError

Browse files
Files changed (1) hide show
  1. app.py +33 -16
app.py CHANGED
@@ -239,15 +239,14 @@ def generate_table_html(rows):
239
  </div>
240
  """
241
 
 
 
 
242
  def run_benchmark(query):
243
  print(f"DEBUG: Starting benchmark for query: {query}")
244
 
245
- # 1. Yield Loader
246
- yield LOADER_HTML
247
- time.sleep(0.2) # Ensure loader renders before heavy processing
248
-
249
  try:
250
- # 2. Perform Search (Live)
251
  start_total = time.time()
252
 
253
  # Generate Embedding
@@ -272,7 +271,7 @@ def run_benchmark(query):
272
  end_total = time.time()
273
  latency_ms = (end_total - start_total) * 1000
274
 
275
- # 3. Construct Data Rows
276
 
277
  # Live Row (MiniLM + LightGBM)
278
  # Mocking shards searched based on confidence for demo visual
@@ -320,17 +319,16 @@ def run_benchmark(query):
320
 
321
  all_rows = [live_row] + ref_rows
322
 
323
- print("DEBUG: Yielding final HTML.")
324
- # 4. Yield Final HTML
325
- yield generate_table_html(all_rows)
326
 
327
  except Exception as e:
328
  import traceback
329
  error_msg = traceback.format_exc()
330
  print(f"CRITICAL ERROR in run_benchmark: {error_msg}")
331
 
332
- # Yield Error HTML
333
- yield f"""
334
  <div class="bg-red-50 border border-red-200 rounded-2xl p-6 text-red-800">
335
  <h3 class="font-bold text-lg mb-2 flex items-center gap-2">
336
  <span class="material-symbols-outlined">error</span>
@@ -373,10 +371,29 @@ with gr.Blocks(theme=gr.themes.Base(), css=None, head=HEAD_HTML) as demo:
373
  # Footer Info
374
  gr.HTML(FOOTER_INFO_HTML)
375
 
376
- # Interactions
377
- submit_btn.click(run_benchmark, inputs=[query_input], outputs=[results_area])
378
- query_input.submit(run_benchmark, inputs=[query_input], outputs=[results_area])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
379
 
380
  if __name__ == "__main__":
381
- # Disable queue to prevent h11 LocalProtocolError on HF Spaces
382
- demo.launch()
 
239
  </div>
240
  """
241
 
242
+ def show_loader():
243
+ return LOADER_HTML
244
+
245
  def run_benchmark(query):
246
  print(f"DEBUG: Starting benchmark for query: {query}")
247
 
 
 
 
 
248
  try:
249
+ # Perform Search (Live)
250
  start_total = time.time()
251
 
252
  # Generate Embedding
 
271
  end_total = time.time()
272
  latency_ms = (end_total - start_total) * 1000
273
 
274
+ # Construct Data Rows
275
 
276
  # Live Row (MiniLM + LightGBM)
277
  # Mocking shards searched based on confidence for demo visual
 
319
 
320
  all_rows = [live_row] + ref_rows
321
 
322
+ print("DEBUG: Returning final HTML.")
323
+ return generate_table_html(all_rows)
 
324
 
325
  except Exception as e:
326
  import traceback
327
  error_msg = traceback.format_exc()
328
  print(f"CRITICAL ERROR in run_benchmark: {error_msg}")
329
 
330
+ # Return Error HTML
331
+ return f"""
332
  <div class="bg-red-50 border border-red-200 rounded-2xl p-6 text-red-800">
333
  <h3 class="font-bold text-lg mb-2 flex items-center gap-2">
334
  <span class="material-symbols-outlined">error</span>
 
371
  # Footer Info
372
  gr.HTML(FOOTER_INFO_HTML)
373
 
374
+ # Interactions: Chained Events
375
+ # 1. Show Loader immediately
376
+ # 2. Run Benchmark and show results
377
+
378
+ submit_btn.click(
379
+ show_loader,
380
+ inputs=None,
381
+ outputs=[results_area]
382
+ ).then(
383
+ run_benchmark,
384
+ inputs=[query_input],
385
+ outputs=[results_area]
386
+ )
387
+
388
+ query_input.submit(
389
+ show_loader,
390
+ inputs=None,
391
+ outputs=[results_area]
392
+ ).then(
393
+ run_benchmark,
394
+ inputs=[query_input],
395
+ outputs=[results_area]
396
+ )
397
 
398
  if __name__ == "__main__":
399
+ demo.queue().launch()