A-Roucher
commited on
Commit
•
72c879a
1
Parent(s):
c9fa165
feat: filter selection by author
Browse files
app.py
CHANGED
@@ -3,56 +3,70 @@ from sentence_transformers import SentenceTransformer
|
|
3 |
import datasets
|
4 |
import faiss
|
5 |
import torch
|
|
|
|
|
6 |
|
7 |
-
st.sidebar.text_input("Type your quote here")
|
8 |
|
9 |
-
|
|
|
|
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
encoder = SentenceTransformer(model_name)
|
15 |
|
16 |
-
|
17 |
-
dataset["quote"],
|
18 |
-
batch_size=4,
|
19 |
-
show_progress_bar=True,
|
20 |
-
convert_to_numpy=True,
|
21 |
-
normalize_embeddings=True,
|
22 |
-
)
|
23 |
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
sentence = "Knowledge of history is power."
|
34 |
-
sentence_embedding = encoder.encode([sentence])
|
35 |
-
# scores, samples = dataset_embeddings.search(
|
36 |
-
# sentence_embedding, k=10
|
37 |
-
# )
|
38 |
-
sentence_embedding_tensor = torch.Tensor(sentence_embedding)
|
39 |
-
dataset_embeddings_tensor = torch.Tensor(embeddings)
|
40 |
-
from sentence_transformers.util import semantic_search
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
3 |
import datasets
|
4 |
import faiss
|
5 |
import torch
|
6 |
+
from sentence_transformers.util import semantic_search
|
7 |
+
import time
|
8 |
|
|
|
9 |
|
10 |
+
if "initialized" not in st.session_state:
|
11 |
+
dataset = datasets.load_dataset('A-Roucher/english_historical_quotes', download_mode="force_redownload")
|
12 |
+
st.session_state.dataset = datasets.Dataset.from_dict(dataset['train'][:100])
|
13 |
|
14 |
+
st.session_state.all_authors = list(set(st.session_state.dataset['author']))
|
15 |
+
model_name = "sentence-transformers/all-MiniLM-L6-v2" # BAAI/bge-small-en-v1.5" # "Cohere/Cohere-embed-english-light-v3.0" # "sentence-transformers/all-MiniLM-L6-v2"
|
16 |
+
st.session_state.encoder = SentenceTransformer(model_name)
|
17 |
+
st.session_state.embeddings = st.session_state.encoder.encode(
|
18 |
+
st.session_state.dataset["quote"],
|
19 |
+
batch_size=4,
|
20 |
+
show_progress_bar=True,
|
21 |
+
convert_to_numpy=True,
|
22 |
+
normalize_embeddings=True,
|
23 |
+
)
|
24 |
+
st.session_state.initialized=True
|
25 |
|
26 |
+
dataset_embeddings_tensor = torch.Tensor(st.session_state.embeddings)
|
|
|
27 |
|
28 |
+
sentence = "Knowledge of history is power."
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
def search(query, selected_authors):
|
31 |
+
start = time.time()
|
32 |
+
if len(query.strip()) == 0:
|
33 |
+
return ""
|
34 |
+
|
35 |
+
query_embedding = st.session_state.encoder.encode([query])
|
36 |
+
sentence_embedding_tensor = torch.Tensor(query_embedding)
|
37 |
|
38 |
+
if len(selected_authors) == 0:
|
39 |
+
author_indexes = [i for i in range(len(st.session_state.dataset))]
|
40 |
+
else:
|
41 |
+
author_indexes = [i for i in range(len(st.session_state.dataset)) if st.session_state.dataset['author'][i] in selected_authors]
|
42 |
+
hits = semantic_search(sentence_embedding_tensor, dataset_embeddings_tensor[author_indexes, :], top_k=5)
|
43 |
|
44 |
+
indices = [author_indexes[i['corpus_id']] for i in hits[0]]
|
45 |
|
46 |
+
if len(indices) == 0:
|
47 |
+
return ""
|
48 |
+
result = "\n\n"
|
49 |
+
for i in indices:
|
50 |
+
result += f"###### {st.session_state.dataset['author'][i]}\n> {st.session_state.dataset['quote'][i]}\n----\n"
|
51 |
+
delay = "%.3f" % (time.time() - start)
|
52 |
+
return f"_Computation time: **{delay} seconds**_{result}"
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
st.markdown(
|
56 |
+
"""
|
57 |
+
<style>
|
58 |
+
div[data-testid="column"]
|
59 |
+
{
|
60 |
+
align-self:flex-end;
|
61 |
+
}
|
62 |
+
</style>
|
63 |
+
""",unsafe_allow_html=True
|
64 |
+
)
|
65 |
+
col1, col2 = st.columns([8, 2])
|
66 |
+
text_input = col1.text_input("Type your idea here:")
|
67 |
+
submit_button = col2.button("_Search quotes!_")
|
68 |
+
selected_authors = st.multiselect("(Optional) - Restrict search to these authors:", st.session_state.all_authors)
|
69 |
+
|
70 |
+
if submit_button:
|
71 |
+
st.markdown(search(text_input, selected_authors))
|
72 |
+
|