ola13 commited on
Commit
aca9622
1 Parent(s): cbab981

em pagination

Browse files
Files changed (1) hide show
  1. app.py +110 -11
app.py CHANGED
@@ -188,7 +188,7 @@ def format_result_page(
188
  if num_results is not None:
189
  header_html += """<div style='font-family: Arial; color:MediumAquaMarine; text-align: center; line-height: 3em'>
190
  Total number of matches: <b style='color:MediumAquaMarine'>{}</b></div>""".format(
191
- filtered_num_results
192
  )
193
 
194
  return header_html + results_html
@@ -244,8 +244,8 @@ def extract_error_from_payload(payload):
244
  return None
245
 
246
 
247
- def request_payload(query, language, exact_search, num_results=10):
248
- post_data = {"query": query, "k": num_results}
249
  if language != "detect_language":
250
  post_data["lang"] = language
251
  address = "http://34.105.160.81:8080" if exact_search else os.environ.get("address")
@@ -282,6 +282,8 @@ if __name__ == "__main__":
282
  num_results_state = gr.State(0)
283
  exact_search_state = gr.State(False)
284
  lang_state = gr.State("")
 
 
285
 
286
  with gr.Row():
287
  gr.Markdown(value=title)
@@ -329,6 +331,8 @@ if __name__ == "__main__":
329
  )
330
  with gr.Row():
331
  results = gr.HTML(label="Results")
 
 
332
  with gr.Column(visible=False) as flagging_form:
333
  flag_txt = gr.Textbox(
334
  lines=1,
@@ -339,20 +343,20 @@ if __name__ == "__main__":
339
  flag_btn = gr.Button("Flag Results")
340
  flag_btn.click(flag, inputs=[query, lang, k, flag_txt], outputs=[flag_txt])
341
 
342
- def submit(query, lang, k, dropdown_input):
343
- print("submitting", query, lang, k)
344
  query = query.strip()
345
  exact_search = False
346
  if query.startswith('"') and query.endswith('"') and len(query) >= 2:
347
  exact_search = True
348
  query = query[1:-1]
 
349
  else:
350
  query = " ".join(query.split())
351
  if query == "" or query is None:
352
  return None
353
 
354
- results_html = ""
355
- payload = request_payload(query, lang, exact_search, k)
356
  err = extract_error_from_payload(payload)
357
  if err is not None:
358
  return process_error(err)
@@ -361,12 +365,74 @@ if __name__ == "__main__":
361
  processed_results,
362
  highlight_terms,
363
  num_results,
364
- datasets,
365
- ) = extract_results_from_payload(query, lang, payload, exact_search)
 
 
 
 
 
366
  results_html = format_result_page(
367
  lang, processed_results, highlight_terms, num_results, exact_search
368
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370
  return {
371
  processed_results_state: processed_results,
372
  highlight_terms_state: highlight_terms,
@@ -378,6 +444,10 @@ if __name__ == "__main__":
378
  available_datasets: gr.Dropdown.update(
379
  choices=datasets, value=datasets
380
  ),
 
 
 
 
381
  }
382
 
383
  def filter_datasets(
@@ -400,7 +470,7 @@ if __name__ == "__main__":
400
 
401
  query.submit(
402
  fn=submit,
403
- inputs=[query, lang, k, available_datasets],
404
  outputs=[
405
  processed_results_state,
406
  highlight_terms_state,
@@ -410,11 +480,38 @@ if __name__ == "__main__":
410
  flagging_form,
411
  datasets_filter,
412
  available_datasets,
 
 
413
  ],
414
  )
415
  submit_btn.click(
416
  submit,
417
- inputs=[query, lang, k, available_datasets],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
418
  outputs=[
419
  processed_results_state,
420
  highlight_terms_state,
@@ -424,6 +521,8 @@ if __name__ == "__main__":
424
  flagging_form,
425
  datasets_filter,
426
  available_datasets,
 
 
427
  ],
428
  )
429
 
 
188
  if num_results is not None:
189
  header_html += """<div style='font-family: Arial; color:MediumAquaMarine; text-align: center; line-height: 3em'>
190
  Total number of matches: <b style='color:MediumAquaMarine'>{}</b></div>""".format(
191
+ num_results
192
  )
193
 
194
  return header_html + results_html
 
244
  return None
245
 
246
 
247
+ def request_payload(query, language, exact_search, num_results=10, received_results=0):
248
+ post_data = {"query": query, "k": num_results, "received_results": received_results}
249
  if language != "detect_language":
250
  post_data["lang"] = language
251
  address = "http://34.105.160.81:8080" if exact_search else os.environ.get("address")
 
282
  num_results_state = gr.State(0)
283
  exact_search_state = gr.State(False)
284
  lang_state = gr.State("")
285
+ max_page_size_state = gr.State(100)
286
+ received_results_state = gr.State(0)
287
 
288
  with gr.Row():
289
  gr.Markdown(value=title)
 
331
  )
332
  with gr.Row():
333
  results = gr.HTML(label="Results")
334
+ with gr.Row(visible=False) as pagination:
335
+ next_page_btn = gr.Button("Next Page")
336
  with gr.Column(visible=False) as flagging_form:
337
  flag_txt = gr.Textbox(
338
  lines=1,
 
343
  flag_btn = gr.Button("Flag Results")
344
  flag_btn.click(flag, inputs=[query, lang, k, flag_txt], outputs=[flag_txt])
345
 
346
+ def run_query(query, lang, k, dropdown_input, max_page_size, received_results):
 
347
  query = query.strip()
348
  exact_search = False
349
  if query.startswith('"') and query.endswith('"') and len(query) >= 2:
350
  exact_search = True
351
  query = query[1:-1]
352
+ k = max_page_size
353
  else:
354
  query = " ".join(query.split())
355
  if query == "" or query is None:
356
  return None
357
 
358
+ print("submitting", query, lang, k)
359
+ payload = request_payload(query, lang, exact_search, k, received_results)
360
  err = extract_error_from_payload(payload)
361
  if err is not None:
362
  return process_error(err)
 
365
  processed_results,
366
  highlight_terms,
367
  num_results,
368
+ ds,
369
+ ) = extract_results_from_payload(
370
+ query,
371
+ lang,
372
+ payload,
373
+ exact_search,
374
+ )
375
  results_html = format_result_page(
376
  lang, processed_results, highlight_terms, num_results, exact_search
377
  )
378
+ return (
379
+ processed_results,
380
+ highlight_terms,
381
+ num_results,
382
+ exact_search,
383
+ results_html,
384
+ ds,
385
+ )
386
+
387
+ def submit(query, lang, k, dropdown_input, max_page_size):
388
+ (
389
+ processed_results,
390
+ highlight_terms,
391
+ num_results,
392
+ exact_search,
393
+ results_html,
394
+ datasets,
395
+ ) = run_query(query, lang, k, dropdown_input, max_page_size, 0)
396
+ has_more_results = exact_search and (num_results > max_page_size)
397
+ return {
398
+ processed_results_state: processed_results,
399
+ highlight_terms_state: highlight_terms,
400
+ num_results_state: num_results,
401
+ exact_search_state: exact_search,
402
+ results: results_html,
403
+ flagging_form: gr.update(visible=True),
404
+ datasets_filter: gr.update(visible=True),
405
+ available_datasets: gr.Dropdown.update(
406
+ choices=datasets, value=datasets
407
+ ),
408
+ pagination: gr.update(visible=has_more_results),
409
+ received_results_state: len(next(iter(processed_results.values()))),
410
+ }
411
 
412
+ def next_page(
413
+ query,
414
+ lang,
415
+ k,
416
+ dropdown_input,
417
+ max_page_size,
418
+ received_results,
419
+ processed_results,
420
+ ):
421
+ (
422
+ processed_results,
423
+ highlight_terms,
424
+ num_results,
425
+ exact_search,
426
+ results_html,
427
+ datasets,
428
+ ) = run_query(
429
+ query, lang, k, dropdown_input, max_page_size, received_results
430
+ )
431
+ num_processed_results = len(next(iter(processed_results.values())))
432
+ has_more_results = exact_search and (num_results > max_page_size)
433
+ print("num_processed_results", num_processed_results)
434
+ print("has_more_results", has_more_results)
435
+ print("current page", len(list(processed_results.values())[0]))
436
  return {
437
  processed_results_state: processed_results,
438
  highlight_terms_state: highlight_terms,
 
444
  available_datasets: gr.Dropdown.update(
445
  choices=datasets, value=datasets
446
  ),
447
+ pagination: gr.update(
448
+ visible=num_processed_results >= max_page_size and has_more_results
449
+ ),
450
+ received_results_state: received_results + num_processed_results,
451
  }
452
 
453
  def filter_datasets(
 
470
 
471
  query.submit(
472
  fn=submit,
473
+ inputs=[query, lang, k, available_datasets, max_page_size_state],
474
  outputs=[
475
  processed_results_state,
476
  highlight_terms_state,
 
480
  flagging_form,
481
  datasets_filter,
482
  available_datasets,
483
+ pagination,
484
+ received_results_state,
485
  ],
486
  )
487
  submit_btn.click(
488
  submit,
489
+ inputs=[query, lang, k, available_datasets, max_page_size_state],
490
+ outputs=[
491
+ processed_results_state,
492
+ highlight_terms_state,
493
+ num_results_state,
494
+ exact_search_state,
495
+ results,
496
+ flagging_form,
497
+ datasets_filter,
498
+ available_datasets,
499
+ pagination,
500
+ received_results_state,
501
+ ],
502
+ )
503
+
504
+ next_page_btn.click(
505
+ next_page,
506
+ inputs=[
507
+ query,
508
+ lang,
509
+ k,
510
+ available_datasets,
511
+ max_page_size_state,
512
+ received_results_state,
513
+ processed_results_state,
514
+ ],
515
  outputs=[
516
  processed_results_state,
517
  highlight_terms_state,
 
521
  flagging_form,
522
  datasets_filter,
523
  available_datasets,
524
+ pagination,
525
+ received_results_state,
526
  ],
527
  )
528