Spaces:
Runtime error
Runtime error
Commit
Β·
9b1e59f
1
Parent(s):
347896a
Upload 23 files
Browse files- .gitattributes +9 -0
- README.md +5 -5
- app.py +201 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.fdm +0 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.fdt +3 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.fdx +0 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.fnm +0 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.nvd +3 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.nvm +0 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.si +0 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.tvd +3 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.tvm +0 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.tvx +3 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.doc +3 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.dvd +3 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.dvm +0 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.pos +3 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.tim +3 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.tip +3 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.tmd +0 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/segments_2 +0 -0
- lucene-index.miracl-v1.0-ru.20221004.2b2856/write.lock +0 -0
- packages.txt +1 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
|
@@ -32,3 +32,12 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.doc filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.dvd filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.pos filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.tim filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.tip filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.fdt filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.nvd filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.tvd filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.tvx filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
---
|
| 2 |
-
title: Miracl Russian
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: streamlit
|
| 7 |
-
sdk_version: 1.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import http.client as http_client
|
| 2 |
+
import json
|
| 3 |
+
import logging
|
| 4 |
+
import os
|
| 5 |
+
import pprint
|
| 6 |
+
import re
|
| 7 |
+
import time
|
| 8 |
+
import string
|
| 9 |
+
|
| 10 |
+
import streamlit as st
|
| 11 |
+
|
| 12 |
+
import streamlit.components.v1 as components
|
| 13 |
+
from typing import Callable, Optional, Tuple, Union
|
| 14 |
+
from pyserini import util
|
| 15 |
+
from pyserini.search import LuceneSearcher, FaissSearcher, AutoQueryEncoder
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
VERSION = '1.0'
|
| 19 |
+
st.set_page_config(page_title="Miracl Search - Russian", layout="wide")
|
| 20 |
+
|
| 21 |
+
os.makedirs(os.path.join(os.getcwd(),".streamlit"), exist_ok = True)
|
| 22 |
+
with open(os.path.join(os.getcwd(),".streamlit/config.toml"), "w") as file:
|
| 23 |
+
file.write(
|
| 24 |
+
'[theme]\nbase="light"'
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
Searcher = Union[FaissSearcher, LuceneSearcher]
|
| 28 |
+
LANG_MAPPING = {'Russian':'ru'}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
st.sidebar.markdown(
|
| 32 |
+
"""
|
| 33 |
+
<style>
|
| 34 |
+
.aligncenter {
|
| 35 |
+
text-align: center;
|
| 36 |
+
font-weight: bold;
|
| 37 |
+
font-size: 30px;
|
| 38 |
+
}
|
| 39 |
+
</style>
|
| 40 |
+
<p class="aligncenter">MIRACL Russian Demo</p>
|
| 41 |
+
<p class="aligncenter">πππ</p>
|
| 42 |
+
<p style="text-align: center;"> MIRACL is a multilingual dataset for ad hoc retrieval that consists of 18 different languages, collectively encompassing over three billion native speakers around the world.</p>
|
| 43 |
+
""",
|
| 44 |
+
unsafe_allow_html=True,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
st.sidebar.markdown(
|
| 48 |
+
"""
|
| 49 |
+
<style>
|
| 50 |
+
.aligncenter {
|
| 51 |
+
text-align: center;
|
| 52 |
+
}
|
| 53 |
+
</style>
|
| 54 |
+
<p style='text-align: center'>
|
| 55 |
+
<a href="https://github.com/project-miracl" >GitHub</a> | <a href="https://arxiv.org/abs/2210.09984" >Paper</a>
|
| 56 |
+
</p>
|
| 57 |
+
""",
|
| 58 |
+
unsafe_allow_html=True,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
query = st.sidebar.text_input(label='Search query', value='')
|
| 62 |
+
language = 'Russian'
|
| 63 |
+
|
| 64 |
+
max_results = st.sidebar.slider(
|
| 65 |
+
"Maximum Number of Results",
|
| 66 |
+
min_value=1,
|
| 67 |
+
max_value=1000,
|
| 68 |
+
step=1,
|
| 69 |
+
value=10,
|
| 70 |
+
help="Maximum Number of Documents to return",
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def _load_sparse_searcher(language: str, k1: Optional[float]=None, b: Optional[float]=None) -> (Searcher):
|
| 75 |
+
searcher = LuceneSearcher(f'lucene-index.miracl-v1.0-{language}.20221004.2b2856')
|
| 76 |
+
searcher.set_language(language)
|
| 77 |
+
if k1 is not None and b is not None:
|
| 78 |
+
searcher.set_bm25(k1, b)
|
| 79 |
+
retriever_name = f'BM25 (k1={k1}, b={b})'
|
| 80 |
+
else:
|
| 81 |
+
retriever_name = 'BM25'
|
| 82 |
+
|
| 83 |
+
return searcher
|
| 84 |
+
|
| 85 |
+
def search(query, language, num_results=10):
|
| 86 |
+
searcher = _load_sparse_searcher(language=LANG_MAPPING[language])
|
| 87 |
+
|
| 88 |
+
t_0 = time.time()
|
| 89 |
+
search_results = searcher.search(query, k=num_results)
|
| 90 |
+
search_time = time.time() - t_0
|
| 91 |
+
|
| 92 |
+
results_dict ={"docs": [], "doc_ids": [], "score":[], "lang": language}
|
| 93 |
+
for i, result in enumerate(search_results):
|
| 94 |
+
result = json.loads(result.raw)
|
| 95 |
+
results_dict["docs"].append(result["text"])
|
| 96 |
+
results_dict["doc_ids"].append(result["docid"])
|
| 97 |
+
results_dict["score"].append(search_results[i].score)
|
| 98 |
+
|
| 99 |
+
return results_dict, search_time
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def highlight_string(paragraph: str, highlight_terms: list) -> str:
|
| 104 |
+
for term in highlight_terms:
|
| 105 |
+
paragraph = re.sub(f"\\b{term}\\b", f"<b>{term}</b>", paragraph, flags=re.I)
|
| 106 |
+
return paragraph
|
| 107 |
+
|
| 108 |
+
def process_results(hits: dict, highlight_terms: list) -> str:
|
| 109 |
+
hit_list = []
|
| 110 |
+
for i in range(len(hits['doc_ids'])):
|
| 111 |
+
res_head = f"""
|
| 112 |
+
<div class='searchresult'>
|
| 113 |
+
<h2>{i+1}. Document ID: {hits['doc_ids'][i]}</h2>
|
| 114 |
+
<p>Language: <string>{hits['lang']}</string>, Score: {round(hits['score'][i], 2)}</p>
|
| 115 |
+
<p>{highlight_string(hits['docs'][i], highlight_terms)}</p>
|
| 116 |
+
</div>
|
| 117 |
+
<hr>
|
| 118 |
+
"""
|
| 119 |
+
hit_list.append(res_head)
|
| 120 |
+
return " ".join(hit_list)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
if st.sidebar.button("Search"):
|
| 125 |
+
hits, search_time = search(query, language, max_results)
|
| 126 |
+
html_results = process_results(hits, [])
|
| 127 |
+
rendered_results = f"""
|
| 128 |
+
<div id="searchresultsarea">
|
| 129 |
+
<br>
|
| 130 |
+
<p id="searchresultsnumber">About {max_results} results</p>
|
| 131 |
+
{html_results}
|
| 132 |
+
</div>
|
| 133 |
+
"""
|
| 134 |
+
st.markdown("""
|
| 135 |
+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
|
| 136 |
+
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
|
| 137 |
+
""",
|
| 138 |
+
unsafe_allow_html=True)
|
| 139 |
+
st.markdown(
|
| 140 |
+
"""
|
| 141 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
| 142 |
+
""",
|
| 143 |
+
unsafe_allow_html=True)
|
| 144 |
+
st.markdown(
|
| 145 |
+
f"""
|
| 146 |
+
<div class="row no-gutters mt-3 align-items-center">
|
| 147 |
+
<h2> Search Results </h2>
|
| 148 |
+
</div>
|
| 149 |
+
""",
|
| 150 |
+
unsafe_allow_html=True)
|
| 151 |
+
components.html(
|
| 152 |
+
"""
|
| 153 |
+
<style>
|
| 154 |
+
#searchresultsarea {
|
| 155 |
+
font-family: 'Arial';
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
#searchresultsnumber {
|
| 159 |
+
font-size: 0.8rem;
|
| 160 |
+
color: gray;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
.searchresult h2 {
|
| 164 |
+
font-size: 19px;
|
| 165 |
+
line-height: 18px;
|
| 166 |
+
font-weight: normal;
|
| 167 |
+
color: rgb(7, 111, 222);
|
| 168 |
+
margin-bottom: 0px;
|
| 169 |
+
margin-top: 25px;
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
.searchresult a {
|
| 173 |
+
font-size: 12px;
|
| 174 |
+
line-height: 12px;
|
| 175 |
+
color: green;
|
| 176 |
+
margin-bottom: 0px;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
.dark-mode {
|
| 180 |
+
color: white;
|
| 181 |
+
}
|
| 182 |
+
</style>
|
| 183 |
+
<script>
|
| 184 |
+
function load_image(id){
|
| 185 |
+
console.log(id)
|
| 186 |
+
var x = document.getElementById(id);
|
| 187 |
+
console.log(x)
|
| 188 |
+
if (x.style.display === "none") {
|
| 189 |
+
x.style.display = "block";
|
| 190 |
+
} else {
|
| 191 |
+
x.style.display = "none";
|
| 192 |
+
}
|
| 193 |
+
};
|
| 194 |
+
function myFunction() {
|
| 195 |
+
var element = document.body;
|
| 196 |
+
element.classList.toggle("dark-mode");
|
| 197 |
+
}
|
| 198 |
+
</script>
|
| 199 |
+
<button onclick="myFunction()">Toggle dark mode</button>
|
| 200 |
+
""" + rendered_results, height=800, scrolling=True
|
| 201 |
+
)
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.fdm
ADDED
|
Binary file (3.31 kB). View file
|
|
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.fdt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e9a24916c46f5d810837d71fbb5cf948e9e4295ffe0fcc5e2569855b41923d3c
|
| 3 |
+
size 3625609256
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.fdx
ADDED
|
Binary file (307 kB). View file
|
|
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.fnm
ADDED
|
Binary file (340 Bytes). View file
|
|
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.nvd
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c4619070e83a13a327606447f5d13750f78868f46cd6468015cb20be17050e9f
|
| 3 |
+
size 9543977
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.nvm
ADDED
|
Binary file (103 Bytes). View file
|
|
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.si
ADDED
|
Binary file (545 Bytes). View file
|
|
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.tvd
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4292bf148c701f797b7393a741c1449e6a7181fcadf6f1d8991dbc13b79e738a
|
| 3 |
+
size 2529209363
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.tvm
ADDED
|
Binary file (24.5 kB). View file
|
|
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h.tvx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a7cb336d7c927e9e2b3467c07ecd82d8235627450878128f3614b60c9f088a9d
|
| 3 |
+
size 1869016
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.doc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c833a3e31a3260c9238683684ec1257cafdac23eb3b5bfd53485d3b07fdb0cf0
|
| 3 |
+
size 513445021
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.dvd
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:95f3ddb47a3cebad041382f29fee2752cab272adb98abfaeb9160c2c5bdae322
|
| 3 |
+
size 100162744
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.dvm
ADDED
|
Binary file (3.22 kB). View file
|
|
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.pos
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b3dc588945773d2177919d1875021d2a1d8996608c1080973301d0f74c0ceca5
|
| 3 |
+
size 324867577
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.tim
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3bcebb476797de9e8fe6e191c3cef12ce9ad9e43bccbffc28cb80f3a578ce713
|
| 3 |
+
size 97808122
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.tip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:00770b7ea02d910493f1121b1d8046d147705c17c93ba219e55dadb864bb229a
|
| 3 |
+
size 3153946
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/_h_Lucene90_0.tmd
ADDED
|
Binary file (295 Bytes). View file
|
|
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/segments_2
ADDED
|
Binary file (154 Bytes). View file
|
|
|
lucene-index.miracl-v1.0-ru.20221004.2b2856/write.lock
ADDED
|
File without changes
|
packages.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
openjdk-11-jdk
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pyserini
|
| 2 |
+
faiss-cpu
|
| 3 |
+
torch
|