Spaces:
Paused
Paused
filtering
Browse files
app.py
CHANGED
@@ -79,8 +79,14 @@ def flag(query, language, num_results, issue_description):
|
|
79 |
return ""
|
80 |
|
81 |
|
82 |
-
def format_result(result, highlight_terms, exact_search):
|
83 |
text, url, docid = result
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
if exact_search:
|
85 |
query_start = text.find(highlight_terms)
|
86 |
query_end = query_start + len(highlight_terms)
|
@@ -119,7 +125,7 @@ def format_result(result, highlight_terms, exact_search):
|
|
119 |
|
120 |
|
121 |
def format_result_page(
|
122 |
-
language, results, highlight_terms, num_results, exact_search
|
123 |
) -> gr.HTML:
|
124 |
header_html = ""
|
125 |
|
@@ -141,7 +147,7 @@ def format_result_page(
|
|
141 |
results_for_lang_html = ""
|
142 |
for result in results_for_lang:
|
143 |
results_for_lang_html += format_result(
|
144 |
-
result, highlight_terms, exact_search
|
145 |
)
|
146 |
if language == "all" and not exact_search:
|
147 |
results_for_lang_html = f"""
|
@@ -160,6 +166,7 @@ def extract_results_from_payload(query, language, payload, exact_search):
|
|
160 |
results = payload["results"]
|
161 |
|
162 |
processed_results = dict()
|
|
|
163 |
highlight_terms = None
|
164 |
num_results = None
|
165 |
|
@@ -185,9 +192,11 @@ def extract_results_from_payload(query, language, payload, exact_search):
|
|
185 |
else None
|
186 |
)
|
187 |
docid = result["docid"]
|
|
|
|
|
188 |
processed_results[lang].append((text, url, docid))
|
189 |
|
190 |
-
return processed_results, highlight_terms, num_results
|
191 |
|
192 |
|
193 |
def process_error(error_type):
|
@@ -238,6 +247,12 @@ if __name__ == "__main__":
|
|
238 |
)
|
239 |
|
240 |
with demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
with gr.Row():
|
242 |
gr.Markdown(value=description)
|
243 |
with gr.Row():
|
@@ -272,20 +287,13 @@ if __name__ == "__main__":
|
|
272 |
with gr.Row():
|
273 |
k = gr.Slider(1, 100, value=10, step=1, label="Max Results")
|
274 |
with gr.Row():
|
275 |
-
""
|
276 |
-
with gr.Column(scale=1):
|
277 |
-
exact_search = gr.Checkbox(
|
278 |
-
value=False, label="Exact Search", variant="compact"
|
279 |
-
)
|
280 |
-
"""
|
281 |
-
with gr.Column(scale=4):
|
282 |
-
submit_btn = gr.Button("Submit")
|
283 |
with gr.Row(visible=False) as datasets_filter:
|
284 |
available_datasets = gr.Dropdown(
|
285 |
type="value",
|
286 |
choices=[],
|
287 |
value=[],
|
288 |
-
label="Datasets",
|
289 |
multiselect=True,
|
290 |
)
|
291 |
with gr.Row():
|
@@ -310,25 +318,29 @@ if __name__ == "__main__":
|
|
310 |
else:
|
311 |
query = " ".join(query.split())
|
312 |
if query == "" or query is None:
|
313 |
-
return
|
314 |
|
315 |
results_html = ""
|
316 |
payload = request_payload(query, lang, exact_search, k)
|
317 |
err = extract_error_from_payload(payload)
|
318 |
if err is not None:
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
return {
|
|
|
|
|
|
|
|
|
332 |
results: results_html,
|
333 |
flagging_form: gr.update(visible=True),
|
334 |
datasets_filter: gr.update(visible=True),
|
@@ -337,19 +349,63 @@ if __name__ == "__main__":
|
|
337 |
),
|
338 |
}
|
339 |
|
340 |
-
def filter_datasets(
|
341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
342 |
|
343 |
query.submit(
|
344 |
fn=submit,
|
345 |
inputs=[query, lang, k, available_datasets],
|
346 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
347 |
)
|
348 |
submit_btn.click(
|
349 |
submit,
|
350 |
inputs=[query, lang, k, available_datasets],
|
351 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
352 |
)
|
353 |
|
354 |
-
available_datasets.change(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
355 |
demo.launch(enable_queue=True, debug=True)
|
|
|
79 |
return ""
|
80 |
|
81 |
|
82 |
+
def format_result(result, highlight_terms, exact_search, datasets_filter=None):
|
83 |
text, url, docid = result
|
84 |
+
if datasets_filter is not None:
|
85 |
+
datasets_filter = set(datasets_filter)
|
86 |
+
dataset = docid.split("/")[1]
|
87 |
+
if not dataset in datasets_filter:
|
88 |
+
return ""
|
89 |
+
|
90 |
if exact_search:
|
91 |
query_start = text.find(highlight_terms)
|
92 |
query_end = query_start + len(highlight_terms)
|
|
|
125 |
|
126 |
|
127 |
def format_result_page(
|
128 |
+
language, results, highlight_terms, num_results, exact_search, datasets_filter=None
|
129 |
) -> gr.HTML:
|
130 |
header_html = ""
|
131 |
|
|
|
147 |
results_for_lang_html = ""
|
148 |
for result in results_for_lang:
|
149 |
results_for_lang_html += format_result(
|
150 |
+
result, highlight_terms, exact_search, datasets_filter
|
151 |
)
|
152 |
if language == "all" and not exact_search:
|
153 |
results_for_lang_html = f"""
|
|
|
166 |
results = payload["results"]
|
167 |
|
168 |
processed_results = dict()
|
169 |
+
datasets = set()
|
170 |
highlight_terms = None
|
171 |
num_results = None
|
172 |
|
|
|
192 |
else None
|
193 |
)
|
194 |
docid = result["docid"]
|
195 |
+
_, dataset, _ = docid.split("/")
|
196 |
+
datasets.add(dataset)
|
197 |
processed_results[lang].append((text, url, docid))
|
198 |
|
199 |
+
return processed_results, highlight_terms, num_results, list(datasets)
|
200 |
|
201 |
|
202 |
def process_error(error_type):
|
|
|
247 |
)
|
248 |
|
249 |
with demo:
|
250 |
+
processed_results_state = gr.State([])
|
251 |
+
highlight_terms_state = gr.State([])
|
252 |
+
num_results_state = gr.State(0)
|
253 |
+
exact_search_state = gr.State(False)
|
254 |
+
lang_state = gr.State("")
|
255 |
+
|
256 |
with gr.Row():
|
257 |
gr.Markdown(value=description)
|
258 |
with gr.Row():
|
|
|
287 |
with gr.Row():
|
288 |
k = gr.Slider(1, 100, value=10, step=1, label="Max Results")
|
289 |
with gr.Row():
|
290 |
+
submit_btn = gr.Button("Submit")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
291 |
with gr.Row(visible=False) as datasets_filter:
|
292 |
available_datasets = gr.Dropdown(
|
293 |
type="value",
|
294 |
choices=[],
|
295 |
value=[],
|
296 |
+
label="Datasets Filter",
|
297 |
multiselect=True,
|
298 |
)
|
299 |
with gr.Row():
|
|
|
318 |
else:
|
319 |
query = " ".join(query.split())
|
320 |
if query == "" or query is None:
|
321 |
+
return None
|
322 |
|
323 |
results_html = ""
|
324 |
payload = request_payload(query, lang, exact_search, k)
|
325 |
err = extract_error_from_payload(payload)
|
326 |
if err is not None:
|
327 |
+
return process_error(err)
|
328 |
+
|
329 |
+
(
|
330 |
+
processed_results,
|
331 |
+
highlight_terms,
|
332 |
+
num_results,
|
333 |
+
datasets,
|
334 |
+
) = extract_results_from_payload(query, lang, payload, exact_search)
|
335 |
+
results_html = format_result_page(
|
336 |
+
lang, processed_results, highlight_terms, num_results, exact_search
|
337 |
+
)
|
338 |
+
|
339 |
return {
|
340 |
+
processed_results_state: processed_results,
|
341 |
+
highlight_terms_state: highlight_terms,
|
342 |
+
num_results_state: num_results,
|
343 |
+
exact_search_state: exact_search,
|
344 |
results: results_html,
|
345 |
flagging_form: gr.update(visible=True),
|
346 |
datasets_filter: gr.update(visible=True),
|
|
|
349 |
),
|
350 |
}
|
351 |
|
352 |
+
def filter_datasets(
|
353 |
+
lang,
|
354 |
+
processed_results,
|
355 |
+
highlight_terms,
|
356 |
+
num_results,
|
357 |
+
exact_search,
|
358 |
+
datasets_filter,
|
359 |
+
):
|
360 |
+
results_html = format_result_page(
|
361 |
+
lang,
|
362 |
+
processed_results,
|
363 |
+
highlight_terms,
|
364 |
+
num_results,
|
365 |
+
exact_search,
|
366 |
+
datasets_filter,
|
367 |
+
)
|
368 |
+
return {results: results_html}
|
369 |
|
370 |
query.submit(
|
371 |
fn=submit,
|
372 |
inputs=[query, lang, k, available_datasets],
|
373 |
+
outputs=[
|
374 |
+
processed_results_state,
|
375 |
+
highlight_terms_state,
|
376 |
+
num_results_state,
|
377 |
+
exact_search_state,
|
378 |
+
results,
|
379 |
+
flagging_form,
|
380 |
+
datasets_filter,
|
381 |
+
available_datasets,
|
382 |
+
],
|
383 |
)
|
384 |
submit_btn.click(
|
385 |
submit,
|
386 |
inputs=[query, lang, k, available_datasets],
|
387 |
+
outputs=[
|
388 |
+
processed_results_state,
|
389 |
+
highlight_terms_state,
|
390 |
+
num_results_state,
|
391 |
+
exact_search_state,
|
392 |
+
results,
|
393 |
+
flagging_form,
|
394 |
+
datasets_filter,
|
395 |
+
available_datasets,
|
396 |
+
],
|
397 |
)
|
398 |
|
399 |
+
available_datasets.change(
|
400 |
+
filter_datasets,
|
401 |
+
inputs=[
|
402 |
+
lang,
|
403 |
+
processed_results_state,
|
404 |
+
highlight_terms_state,
|
405 |
+
num_results_state,
|
406 |
+
exact_search_state,
|
407 |
+
available_datasets,
|
408 |
+
],
|
409 |
+
outputs=[results],
|
410 |
+
)
|
411 |
demo.launch(enable_queue=True, debug=True)
|