import http.client as http_client
import json
import logging
import os
import re
import string
import gradio as gr
import requests
def get_docid_html(docid):
data_org, dataset, docid = docid.split("/")
docid_html = """{}/{}""".format(
dataset, data_org + "/" + dataset, docid
)
return docid_html
PII_TAGS = {"KEY", "EMAIL", "USER", "IP_ADDRESS", "ID", "IPv4", "IPv6"}
PII_PREFIX = "PI:"
def process_pii(text):
for tag in PII_TAGS:
text = text.replace(
PII_PREFIX + tag,
"""REDACTED {}""".format(tag),
)
return text
def process_results(results, highlight_terms):
if len(results) == 0:
return """
No results retrieved.
Document ID: {}
Language: {}
{}
Detected language {detected_lang} is not supported.
Please choose a language from the dropdown or type another query.
Detected language: {results[0]["lang"]}
No results for language: {lang}
Raised {type(e).__name__}
Check if a relevant discussion already exists in the Community tab. If not, please open a discussion.
""" return results_html def flag(query, language, num_results, issue_description): try: post_data = {"query": query, "k": num_results, "flag": True, "description": issue_description} if language != "detect_language": post_data["lang"] = language output = requests.post( os.environ.get("address"), headers={"Content-type": "application/json"}, data=json.dumps(post_data), timeout=120, ) results = json.loads(output.text) except: print("Error flagging") return "" description = """#🌏🌖
#gæa
Large scale text corpora search engine.""" if __name__ == "__main__": 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=2, placeholder="Type your query here...", label="Query") with gr.Row(): lang = gr.Dropdown( choices=[ "ar", "ca", "code", "en", "es", "eu", "fr", "id", "indic", "nigercongo", "pt", "vi", "zh", "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") flag_description = """If you choose to flag your search, we will save the query, language and the number of results you requested. Please consider adding any additional context in the box on the right.
""" with gr.Column(visible=False) as flagging_form: flag_txt = gr.Textbox( lines=1, placeholder="Type here...", label="""If you choose to flag your search, we will save the query, language and the number of results you requested. Please consider adding relevant additional context below:""", ) flag_btn = gr.Button("Flag Results") flag_btn.click(flag, inputs=[query, lang, k, flag_txt], outputs=[flag_txt]) def submit(query, lang, k): if query == "": return ["", ""] return { results: scisearch(query, lang, k), flagging_form: gr.update(visible=True), } submit_btn.click(submit, inputs=[query, lang, k], outputs=[results, flagging_form]) demo.launch(enable_queue=True, debug=True)