File size: 3,930 Bytes
8cad083 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | from typing import List, NewType, Optional, Union
import gradio as gr
from spacerini_utils.index import fetch_index_stats
from spacerini_utils.search import _search, init_searcher, SearchResult
HTML = NewType('HTML', str)
searcher = init_searcher(sparse_index_path="sparse_index")
def get_docid_html(docid: Union[int, str]) -> HTML:
docid_html = (
f"<a "
f'class="underline-on-hover"'
'title="This dataset is licensed apache-2.0"'
f'style="color:#2D31FA;"'
'href="https://huggingface.co/datasets/π"'
'target="_blank"><b>πxsum</b></a><span style="color: #7978FF;">/'+f'{docid}</span>'
)
return docid_html
def process_results(results: List[SearchResult], language: str, highlight_terms: Optional[List[str]] = None) -> HTML:
if len(results) == 0:
return """<br><p style='font-family: Arial; color:Silver; text-align: center;'>
No results retrieved.</p><br><hr>"""
results_html = ""
for result in results:
tokens = result["text"].split()
tokens_html = []
if highlight_terms:
for token in tokens:
if token in highlight_terms:
tokens_html.append("<b>{}</b>".format(token))
else:
tokens_html.append(token)
tokens_html = " ".join(tokens_html)
meta_html = (
"""
<p class='underline-on-hover' style='font-size:12px; font-family: Arial; color:#585858; text-align: left;'>
"""
)
docid_html = get_docid_html(result["docid"])
results_html += """{}
<p style='font-size:20px; font-family: Arial; color:#7978FF; text-align: left;'>Document ID: {}</p>
<p style='font-size:14px; font-family: Arial; color:#7978FF; text-align: left;'>Score: {}</p>
<p style='font-size:12px; font-family: Arial; color:MediumAquaMarine'>Language: {}</p>
<p style='font-family: Arial;font-size:15px;'>{}</p>
<br>
""".format(
meta_html, docid_html, result["score"], language, tokens_html
)
return results_html + "<hr>"
def search(query: str, language: str, num_results: int = 10) -> HTML:
results_dict = _search(searcher, query, num_results=num_results)
return process_results(results_dict, language)
stats = fetch_index_stats('sparse_index/')
description = f"""# <h2 style="text-align: center;"> π π None π π </h2>
<p style="text-align: center;font-size:15px;">None</p>
<p style="text-align: center;font-size:20px;">Dataset Statistics: Total Number of Documents = <b>{stats["documents"]}</b>, Number of Terms = <b>{stats["total_terms"]}</b> </p>"""
demo = gr.Blocks(
css=".underline-on-hover:hover { text-decoration: underline; } .flagging { font-size:12px; color:Silver; }"
)
with demo:
with gr.Row():
gr.Markdown(value=description)
with gr.Row():
query = gr.Textbox(lines=1, max_lines=1, placeholder="Type your query here...", label="Query")
with gr.Row():
lang = gr.Dropdown(
choices=[
"en",
"detect_language",
"all",
],
value="en",
label="Language",
)
with gr.Row():
k = gr.Slider(1, 100, value=10, step=1, label="Max Results")
with gr.Row():
submit_btn = gr.Button("Submit")
with gr.Row():
results = gr.HTML(label="Results")
def submit(query: str, lang: str, k: int):
query = query.strip()
if query is None or query == "":
return "", ""
return {
results: search(query, lang, k),
}
query.submit(fn=submit, inputs=[query, lang, k], outputs=[results])
submit_btn.click(submit, inputs=[query, lang, k], outputs=[results])
demo.launch(enable_queue=True, debug=True)
|