Spaces:
Paused
Paused
revert html experiments
Browse files
app.py
CHANGED
@@ -88,7 +88,6 @@ def flag(query, language, num_results, issue_description):
|
|
88 |
|
89 |
|
90 |
def format_result(result, highlight_terms, exact_search, datasets_filter=None):
|
91 |
-
# print("result", result)
|
92 |
text, url, docid = result
|
93 |
if datasets_filter is not None:
|
94 |
datasets_filter = set(datasets_filter)
|
@@ -134,7 +133,8 @@ def format_result(result, highlight_terms, exact_search, datasets_filter=None):
|
|
134 |
language = "FIXME"
|
135 |
result_html = """{}
|
136 |
<span style='font-size:14px; font-family: Arial; color:#7978FF; text-align: left;'>Document ID: {}</span>
|
137 |
-
<
|
|
|
138 |
<!-- <span style='font-size:12px; font-family: Arial; color:MediumAquaMarine'>Language: {}</span><br> -->
|
139 |
<span style='font-family: Arial;'>{}</span><br>
|
140 |
<br>
|
@@ -144,6 +144,58 @@ def format_result(result, highlight_terms, exact_search, datasets_filter=None):
|
|
144 |
return "<p>" + result_html + "</p>"
|
145 |
|
146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
def format_result_page(
|
148 |
results, highlight_terms, num_results, exact_search, datasets_filter=None
|
149 |
):
|
@@ -160,34 +212,32 @@ def format_result_page(
|
|
160 |
def extract_results_from_payload(query, language, payload, exact_search):
|
161 |
results = payload["results"]
|
162 |
|
163 |
-
processed_results =
|
164 |
datasets = set()
|
165 |
highlight_terms = None
|
166 |
num_results = None
|
167 |
-
|
168 |
if exact_search:
|
169 |
highlight_terms = query
|
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
|
179 |
-
|
180 |
-
|
181 |
-
result["
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
|
|
|
|
191 |
|
192 |
return processed_results, highlight_terms, num_results, list(datasets)
|
193 |
|
@@ -277,6 +327,7 @@ if __name__ == "__main__":
|
|
277 |
"vi",
|
278 |
"zh",
|
279 |
"detect_language",
|
|
|
280 |
],
|
281 |
value="en",
|
282 |
label="Language",
|
@@ -293,10 +344,8 @@ if __name__ == "__main__":
|
|
293 |
multiselect=True,
|
294 |
)
|
295 |
with gr.Row():
|
296 |
-
|
297 |
-
|
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 |
|
@@ -344,8 +393,8 @@ if __name__ == "__main__":
|
|
344 |
num_results
|
345 |
)
|
346 |
# print("processed_results", processed_results)
|
347 |
-
results_html =
|
348 |
-
processed_results, highlight_terms, num_results, exact_search
|
349 |
)
|
350 |
return (
|
351 |
processed_results,
|
@@ -366,20 +415,17 @@ if __name__ == "__main__":
|
|
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 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
+ results_html
|
381 |
-
+ [gr.update(visible=False)] * (100 - len(results_html))
|
382 |
-
)
|
383 |
|
384 |
def next_page(
|
385 |
query,
|
@@ -405,23 +451,19 @@ if __name__ == "__main__":
|
|
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 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
]
|
422 |
-
+ results_html
|
423 |
-
+ [gr.update(visible=False)] * (100 - len(results_html))
|
424 |
-
)
|
425 |
|
426 |
def filter_datasets(
|
427 |
lang,
|
@@ -431,14 +473,14 @@ if __name__ == "__main__":
|
|
431 |
exact_search,
|
432 |
datasets_filter,
|
433 |
):
|
434 |
-
results_html =
|
435 |
processed_results,
|
436 |
highlight_terms,
|
437 |
num_results,
|
438 |
exact_search,
|
439 |
datasets_filter,
|
440 |
)
|
441 |
-
return results_html
|
442 |
|
443 |
query.submit(
|
444 |
fn=submit,
|
@@ -452,8 +494,8 @@ if __name__ == "__main__":
|
|
452 |
available_datasets,
|
453 |
pagination,
|
454 |
received_results_state,
|
455 |
-
|
456 |
-
|
457 |
)
|
458 |
submit_btn.click(
|
459 |
submit,
|
@@ -467,8 +509,8 @@ if __name__ == "__main__":
|
|
467 |
available_datasets,
|
468 |
pagination,
|
469 |
received_results_state,
|
470 |
-
|
471 |
-
|
472 |
)
|
473 |
|
474 |
next_page_btn.click(
|
@@ -491,8 +533,8 @@ if __name__ == "__main__":
|
|
491 |
available_datasets,
|
492 |
pagination,
|
493 |
received_results_state,
|
494 |
-
|
495 |
-
|
496 |
)
|
497 |
|
498 |
available_datasets.change(
|
@@ -507,16 +549,4 @@ if __name__ == "__main__":
|
|
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)
|
|
|
88 |
|
89 |
|
90 |
def format_result(result, highlight_terms, exact_search, datasets_filter=None):
|
|
|
91 |
text, url, docid = result
|
92 |
if datasets_filter is not None:
|
93 |
datasets_filter = set(datasets_filter)
|
|
|
133 |
language = "FIXME"
|
134 |
result_html = """{}
|
135 |
<span style='font-size:14px; font-family: Arial; color:#7978FF; text-align: left;'>Document ID: {}</span>
|
136 |
+
<a href="https://forms.gle/AdBLLwRApqcLkHYA8" target="_blank"><button>🏴☠️ Flag result 🏴☠️</button></a><br>
|
137 |
+
|
138 |
<!-- <span style='font-size:12px; font-family: Arial; color:MediumAquaMarine'>Language: {}</span><br> -->
|
139 |
<span style='font-family: Arial;'>{}</span><br>
|
140 |
<br>
|
|
|
144 |
return "<p>" + result_html + "</p>"
|
145 |
|
146 |
|
147 |
+
def format_result_page_old(
|
148 |
+
language, results, highlight_terms, num_results, exact_search, datasets_filter=None
|
149 |
+
) -> gr.HTML:
|
150 |
+
|
151 |
+
filtered_num_results = 0
|
152 |
+
header_html = ""
|
153 |
+
|
154 |
+
if language == "detect_language" and not exact_search:
|
155 |
+
header_html += """<div style='font-family: Arial; color:MediumAquaMarine; text-align: center; line-height: 3em'>
|
156 |
+
Detected language: <b style='color:MediumAquaMarine'>{}</b></div>""".format(
|
157 |
+
list(results.keys())[0]
|
158 |
+
)
|
159 |
+
|
160 |
+
results_html = ""
|
161 |
+
for lang, results_for_lang in results.items():
|
162 |
+
if len(results_for_lang) == 0:
|
163 |
+
if exact_search:
|
164 |
+
results_html += """<div style='font-family: Arial; color:Silver; text-align: left; line-height: 3em'>
|
165 |
+
No results found.</div>"""
|
166 |
+
else:
|
167 |
+
results_html += """<div style='font-family: Arial; color:Silver; text-align: left; line-height: 3em'>
|
168 |
+
No results for language: <b>{}</b></div>""".format(
|
169 |
+
lang
|
170 |
+
)
|
171 |
+
continue
|
172 |
+
results_for_lang_html = ""
|
173 |
+
for result in results_for_lang:
|
174 |
+
result_html = format_result(
|
175 |
+
result, highlight_terms, exact_search, datasets_filter
|
176 |
+
)
|
177 |
+
if result_html != "":
|
178 |
+
filtered_num_results += 1
|
179 |
+
results_for_lang_html += result_html
|
180 |
+
if language == "all" and not exact_search:
|
181 |
+
results_for_lang_html = f"""
|
182 |
+
<details>
|
183 |
+
<summary style='font-family: Arial; color:MediumAquaMarine; text-align: left; line-height: 3em'>
|
184 |
+
Results for language: <b>{lang}</b>
|
185 |
+
</summary>
|
186 |
+
{results_for_lang_html}
|
187 |
+
</details>"""
|
188 |
+
results_html += results_for_lang_html
|
189 |
+
|
190 |
+
if num_results is not None:
|
191 |
+
header_html += """<div style='font-family: Arial; color:MediumAquaMarine; text-align: center; line-height: 3em'>
|
192 |
+
Total number of matches: <b style='color:MediumAquaMarine'>{}</b></div>""".format(
|
193 |
+
num_results
|
194 |
+
)
|
195 |
+
|
196 |
+
return header_html + results_html
|
197 |
+
|
198 |
+
|
199 |
def format_result_page(
|
200 |
results, highlight_terms, num_results, exact_search, datasets_filter=None
|
201 |
):
|
|
|
212 |
def extract_results_from_payload(query, language, payload, exact_search):
|
213 |
results = payload["results"]
|
214 |
|
215 |
+
processed_results = dict()
|
216 |
datasets = set()
|
217 |
highlight_terms = None
|
218 |
num_results = None
|
|
|
219 |
if exact_search:
|
220 |
highlight_terms = query
|
221 |
num_results = payload["num_results"]
|
222 |
+
results = {"dummy": results}
|
223 |
else:
|
224 |
highlight_terms = payload["highlight_terms"]
|
|
|
|
|
|
|
|
|
225 |
|
226 |
+
for lang, results_for_lang in results.items():
|
227 |
+
processed_results[lang] = list()
|
228 |
+
for result in results_for_lang:
|
229 |
+
text = result["text"]
|
230 |
+
url = (
|
231 |
+
result["meta"]["url"]
|
232 |
+
if "meta" in result
|
233 |
+
and result["meta"] is not None
|
234 |
+
and "url" in result["meta"]
|
235 |
+
else None
|
236 |
+
)
|
237 |
+
docid = result["docid"]
|
238 |
+
_, dataset, _ = docid.split("/")
|
239 |
+
datasets.add(dataset)
|
240 |
+
processed_results[lang].append((text, url, docid))
|
241 |
|
242 |
return processed_results, highlight_terms, num_results, list(datasets)
|
243 |
|
|
|
327 |
"vi",
|
328 |
"zh",
|
329 |
"detect_language",
|
330 |
+
"all",
|
331 |
],
|
332 |
value="en",
|
333 |
label="Language",
|
|
|
344 |
multiselect=True,
|
345 |
)
|
346 |
with gr.Row():
|
347 |
+
results_html = gr.HTML(label="Results")
|
348 |
+
|
|
|
|
|
349 |
with gr.Row(visible=False) as pagination:
|
350 |
next_page_btn = gr.Button("Next Page")
|
351 |
|
|
|
393 |
num_results
|
394 |
)
|
395 |
# print("processed_results", processed_results)
|
396 |
+
results_html = format_result_page_old(
|
397 |
+
lang, processed_results, highlight_terms, num_results, exact_search
|
398 |
)
|
399 |
return (
|
400 |
processed_results,
|
|
|
415 |
datasets,
|
416 |
) = run_query(query, lang, k, dropdown_input, max_page_size, 0)
|
417 |
has_more_results = exact_search and (num_results > max_page_size)
|
418 |
+
return [
|
419 |
+
processed_results,
|
420 |
+
highlight_terms,
|
421 |
+
num_results,
|
422 |
+
exact_search,
|
423 |
+
gr.update(visible=True),
|
424 |
+
gr.Dropdown.update(choices=datasets, value=datasets),
|
425 |
+
gr.update(visible=has_more_results),
|
426 |
+
len(processed_results),
|
427 |
+
results_html,
|
428 |
+
]
|
|
|
|
|
|
|
429 |
|
430 |
def next_page(
|
431 |
query,
|
|
|
451 |
print("num_processed_results", num_processed_results)
|
452 |
print("has_more_results", has_more_results)
|
453 |
print("received_results", received_results)
|
454 |
+
return [
|
455 |
+
processed_results,
|
456 |
+
highlight_terms,
|
457 |
+
num_results,
|
458 |
+
exact_search,
|
459 |
+
gr.update(visible=True),
|
460 |
+
gr.Dropdown.update(choices=datasets, value=datasets),
|
461 |
+
gr.update(
|
462 |
+
visible=num_processed_results >= max_page_size and has_more_results
|
463 |
+
),
|
464 |
+
received_results + num_processed_results,
|
465 |
+
results_html,
|
466 |
+
]
|
|
|
|
|
|
|
|
|
467 |
|
468 |
def filter_datasets(
|
469 |
lang,
|
|
|
473 |
exact_search,
|
474 |
datasets_filter,
|
475 |
):
|
476 |
+
results_html = format_result_page_old(
|
477 |
processed_results,
|
478 |
highlight_terms,
|
479 |
num_results,
|
480 |
exact_search,
|
481 |
datasets_filter,
|
482 |
)
|
483 |
+
return results_html
|
484 |
|
485 |
query.submit(
|
486 |
fn=submit,
|
|
|
494 |
available_datasets,
|
495 |
pagination,
|
496 |
received_results_state,
|
497 |
+
results_html,
|
498 |
+
],
|
499 |
)
|
500 |
submit_btn.click(
|
501 |
submit,
|
|
|
509 |
available_datasets,
|
510 |
pagination,
|
511 |
received_results_state,
|
512 |
+
results_html,
|
513 |
+
],
|
514 |
)
|
515 |
|
516 |
next_page_btn.click(
|
|
|
533 |
available_datasets,
|
534 |
pagination,
|
535 |
received_results_state,
|
536 |
+
results_html,
|
537 |
+
],
|
538 |
)
|
539 |
|
540 |
available_datasets.change(
|
|
|
549 |
],
|
550 |
outputs=results_html,
|
551 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
552 |
demo.launch(enable_queue=True, debug=True)
|