ola13 commited on
Commit
35b3cef
1 Parent(s): a949c83
Files changed (1) hide show
  1. app.py +67 -59
app.py CHANGED
@@ -133,7 +133,8 @@ def format_result(result, highlight_terms, exact_search, datasets_filter=None):
133
  docid_html = get_docid_html(docid)
134
  language = "FIXME"
135
  result_html = """{}
136
- <span style='font-size:14px; font-family: Arial; color:#7978FF; text-align: left;'>Document ID: {}</span><br>
 
137
  <!-- <span style='font-size:12px; font-family: Arial; color:MediumAquaMarine'>Language: {}</span><br> -->
138
  <span style='font-family: Arial;'>{}</span><br>
139
  <br>
@@ -169,6 +170,10 @@ def extract_results_from_payload(query, language, payload, exact_search):
169
  num_results = payload["num_results"]
170
  else:
171
  highlight_terms = payload["highlight_terms"]
 
 
 
 
172
 
173
  for result in results:
174
  text = result["text"]
@@ -287,20 +292,13 @@ if __name__ == "__main__":
287
  label="Datasets Filter",
288
  multiselect=True,
289
  )
290
- with gr.Row() as results_row:
291
- header_html = gr.HTML(label="Header")
292
- results_html = gr.HTML(label="Results")
 
 
293
  with gr.Row(visible=False) as pagination:
294
  next_page_btn = gr.Button("Next Page")
295
- with gr.Column(visible=False) as flagging_form:
296
- flag_txt = gr.Textbox(
297
- lines=1,
298
- placeholder="Type here...",
299
- label="""If you choose to flag your search, we will save the query, language and the number of results
300
- you requested. Please consider adding relevant additional context below:""",
301
- )
302
- flag_btn = gr.Button("Flag Results")
303
- flag_btn.click(flag, inputs=[query, lang, k, flag_txt], outputs=[flag_txt])
304
 
305
  def run_query(query, lang, k, dropdown_input, max_page_size, received_results):
306
  query = query.strip()
@@ -346,7 +344,7 @@ if __name__ == "__main__":
346
  num_results
347
  )
348
  # print("processed_results", processed_results)
349
- results_html_new = format_result_page(
350
  processed_results, highlight_terms, num_results, exact_search
351
  )
352
  return (
@@ -354,7 +352,7 @@ if __name__ == "__main__":
354
  highlight_terms,
355
  num_results,
356
  exact_search,
357
- results_html_new,
358
  ds,
359
  )
360
 
@@ -364,24 +362,24 @@ if __name__ == "__main__":
364
  highlight_terms,
365
  num_results,
366
  exact_search,
367
- results_html_new,
368
  datasets,
369
  ) = run_query(query, lang, k, dropdown_input, max_page_size, 0)
370
  has_more_results = exact_search and (num_results > max_page_size)
371
- return {
372
- processed_results_state: processed_results,
373
- highlight_terms_state: highlight_terms,
374
- num_results_state: num_results,
375
- exact_search_state: exact_search,
376
- results_html: results_html_new,
377
- flagging_form: gr.update(visible=True),
378
- datasets_filter: gr.update(visible=True),
379
- available_datasets: gr.Dropdown.update(
380
- choices=datasets, value=datasets
381
- ),
382
- pagination: gr.update(visible=has_more_results),
383
- received_results_state: len(processed_results),
384
- }
385
 
386
  def next_page(
387
  query,
@@ -397,7 +395,7 @@ if __name__ == "__main__":
397
  highlight_terms,
398
  num_results,
399
  exact_search,
400
- results_html_new,
401
  datasets,
402
  ) = run_query(
403
  query, lang, k, dropdown_input, max_page_size, received_results
@@ -407,22 +405,23 @@ if __name__ == "__main__":
407
  print("num_processed_results", num_processed_results)
408
  print("has_more_results", has_more_results)
409
  print("received_results", received_results)
410
- return {
411
- processed_results_state: processed_results,
412
- highlight_terms_state: highlight_terms,
413
- num_results_state: num_results,
414
- exact_search_state: exact_search,
415
- results_html: results_html_new,
416
- flagging_form: gr.update(visible=True),
417
- datasets_filter: gr.update(visible=True),
418
- available_datasets: gr.Dropdown.update(
419
- choices=datasets, value=datasets
420
- ),
421
- pagination: gr.update(
422
- visible=num_processed_results >= max_page_size and has_more_results
423
- ),
424
- received_results_state: received_results + num_processed_results,
425
- }
 
426
 
427
  def filter_datasets(
428
  lang,
@@ -432,14 +431,14 @@ if __name__ == "__main__":
432
  exact_search,
433
  datasets_filter,
434
  ):
435
- results_html_new = format_result_page(
436
  processed_results,
437
  highlight_terms,
438
  num_results,
439
  exact_search,
440
  datasets_filter,
441
  )
442
- return {results_html: results_html_new}
443
 
444
  query.submit(
445
  fn=submit,
@@ -449,13 +448,12 @@ if __name__ == "__main__":
449
  highlight_terms_state,
450
  num_results_state,
451
  exact_search_state,
452
- results_html,
453
- flagging_form,
454
  datasets_filter,
455
  available_datasets,
456
  pagination,
457
  received_results_state,
458
- ],
 
459
  )
460
  submit_btn.click(
461
  submit,
@@ -465,13 +463,12 @@ if __name__ == "__main__":
465
  highlight_terms_state,
466
  num_results_state,
467
  exact_search_state,
468
- results_html,
469
- flagging_form,
470
  datasets_filter,
471
  available_datasets,
472
  pagination,
473
  received_results_state,
474
- ],
 
475
  )
476
 
477
  next_page_btn.click(
@@ -490,13 +487,12 @@ if __name__ == "__main__":
490
  highlight_terms_state,
491
  num_results_state,
492
  exact_search_state,
493
- results_html,
494
- flagging_form,
495
  datasets_filter,
496
  available_datasets,
497
  pagination,
498
  received_results_state,
499
- ],
 
500
  )
501
 
502
  available_datasets.change(
@@ -509,6 +505,18 @@ if __name__ == "__main__":
509
  exact_search_state,
510
  available_datasets,
511
  ],
512
- outputs=[results_html],
 
 
 
 
 
 
 
 
 
 
 
 
513
  )
514
  demo.launch(enable_queue=True, debug=True)
 
133
  docid_html = get_docid_html(docid)
134
  language = "FIXME"
135
  result_html = """{}
136
+ <span style='font-size:14px; font-family: Arial; color:#7978FF; text-align: left;'>Document ID: {}</span>
137
+ <button type="button" onclick="alert('Hello world!')">Flag result</button><br>
138
  <!-- <span style='font-size:12px; font-family: Arial; color:MediumAquaMarine'>Language: {}</span><br> -->
139
  <span style='font-family: Arial;'>{}</span><br>
140
  <br>
 
170
  num_results = payload["num_results"]
171
  else:
172
  highlight_terms = payload["highlight_terms"]
173
+ results = []
174
+ for lang, res_for_lang in payload["results"].items():
175
+ for result in res_for_lang:
176
+ results.append(result)
177
 
178
  for result in results:
179
  text = result["text"]
 
292
  label="Datasets Filter",
293
  multiselect=True,
294
  )
295
+ with gr.Row():
296
+ header_html = gr.HTML(label="Header", value="hello")
297
+ results_html = []
298
+ for i in range(100):
299
+ results_html.append(gr.HTML(label="Results"))
300
  with gr.Row(visible=False) as pagination:
301
  next_page_btn = gr.Button("Next Page")
 
 
 
 
 
 
 
 
 
302
 
303
  def run_query(query, lang, k, dropdown_input, max_page_size, received_results):
304
  query = query.strip()
 
344
  num_results
345
  )
346
  # print("processed_results", processed_results)
347
+ results_html = format_result_page(
348
  processed_results, highlight_terms, num_results, exact_search
349
  )
350
  return (
 
352
  highlight_terms,
353
  num_results,
354
  exact_search,
355
+ results_html,
356
  ds,
357
  )
358
 
 
362
  highlight_terms,
363
  num_results,
364
  exact_search,
365
+ results_html,
366
  datasets,
367
  ) = run_query(query, lang, k, dropdown_input, max_page_size, 0)
368
  has_more_results = exact_search and (num_results > max_page_size)
369
+ return (
370
+ [
371
+ processed_results,
372
+ highlight_terms,
373
+ num_results,
374
+ exact_search,
375
+ gr.update(visible=True),
376
+ gr.Dropdown.update(choices=datasets, value=datasets),
377
+ gr.update(visible=has_more_results),
378
+ len(processed_results),
379
+ ]
380
+ + results_html
381
+ + [gr.update(visible=False)] * (100 - len(results_html))
382
+ )
383
 
384
  def next_page(
385
  query,
 
395
  highlight_terms,
396
  num_results,
397
  exact_search,
398
+ results_html,
399
  datasets,
400
  ) = run_query(
401
  query, lang, k, dropdown_input, max_page_size, received_results
 
405
  print("num_processed_results", num_processed_results)
406
  print("has_more_results", has_more_results)
407
  print("received_results", received_results)
408
+ return (
409
+ [
410
+ processed_results,
411
+ highlight_terms,
412
+ num_results,
413
+ exact_search,
414
+ gr.update(visible=True),
415
+ gr.Dropdown.update(choices=datasets, value=datasets),
416
+ gr.update(
417
+ visible=num_processed_results >= max_page_size
418
+ and has_more_results
419
+ ),
420
+ received_results + num_processed_results,
421
+ ]
422
+ + results_html
423
+ + [gr.update(visible=False)] * (100 - len(results_html))
424
+ )
425
 
426
  def filter_datasets(
427
  lang,
 
431
  exact_search,
432
  datasets_filter,
433
  ):
434
+ results_html = format_result_page(
435
  processed_results,
436
  highlight_terms,
437
  num_results,
438
  exact_search,
439
  datasets_filter,
440
  )
441
+ return results_html + [gr.update(visible=False)] * (100 - len(results_html))
442
 
443
  query.submit(
444
  fn=submit,
 
448
  highlight_terms_state,
449
  num_results_state,
450
  exact_search_state,
 
 
451
  datasets_filter,
452
  available_datasets,
453
  pagination,
454
  received_results_state,
455
+ ]
456
+ + results_html,
457
  )
458
  submit_btn.click(
459
  submit,
 
463
  highlight_terms_state,
464
  num_results_state,
465
  exact_search_state,
 
 
466
  datasets_filter,
467
  available_datasets,
468
  pagination,
469
  received_results_state,
470
+ ]
471
+ + results_html,
472
  )
473
 
474
  next_page_btn.click(
 
487
  highlight_terms_state,
488
  num_results_state,
489
  exact_search_state,
 
 
490
  datasets_filter,
491
  available_datasets,
492
  pagination,
493
  received_results_state,
494
+ ]
495
+ + results_html,
496
  )
497
 
498
  available_datasets.change(
 
505
  exact_search_state,
506
  available_datasets,
507
  ],
508
+ outputs=results_html,
509
+ )
510
+ results_html[0].change(
511
+ filter_datasets,
512
+ inputs=[
513
+ lang,
514
+ processed_results_state,
515
+ highlight_terms_state,
516
+ num_results_state,
517
+ exact_search_state,
518
+ available_datasets,
519
+ ],
520
+ outputs=results_html,
521
  )
522
  demo.launch(enable_queue=True, debug=True)