Spaces:
Running
Running
faiss added
Browse files- app.py +14 -9
- requirements.txt +1 -0
app.py
CHANGED
@@ -6,6 +6,7 @@ import numpy as np
|
|
6 |
import torch
|
7 |
from transformers import AutoTokenizer, AutoModel
|
8 |
from joblib import load
|
|
|
9 |
|
10 |
tokenizer = AutoTokenizer.from_pretrained("cointegrated/rubert-tiny2")
|
11 |
model = AutoModel.from_pretrained("cointegrated/rubert-tiny2")
|
@@ -22,29 +23,33 @@ def embed_bert_cls(text, model, tokenizer):
|
|
22 |
return embeddings[0].cpu().numpy()
|
23 |
|
24 |
embeded_list = load('embeded_list.joblib')
|
|
|
|
|
|
|
25 |
text = st.text_input('Введите текст')
|
26 |
count_visible = st.number_input("Введите количество отображаемых элементов", 1, 10, 5, step=1)
|
27 |
if st.button("Найти", type="primary"):
|
28 |
st.write('Количество фильмов в выборке 4950')
|
29 |
if text and count_visible:
|
30 |
embeded_text = embed_bert_cls(text, model, tokenizer).reshape(1,-1)
|
31 |
-
|
|
|
32 |
for i in range(count_visible):
|
33 |
col1, col2 = st.columns(2)
|
34 |
with col1:
|
35 |
-
st.header(films.iloc[
|
36 |
-
st.write(films.iloc[
|
37 |
-
st.write(f'Мера схожести евклидова расстояния {
|
38 |
with col2:
|
39 |
try:
|
40 |
-
st.image(films.iloc[
|
41 |
except:
|
42 |
st.write('Нет картинки')
|
43 |
st.header('Самый не подходящий запрос')
|
44 |
col3, col4 = st.columns(2)
|
45 |
with col3:
|
46 |
-
st.header(films.iloc[
|
47 |
-
st.write(films.iloc[
|
48 |
-
st.write(f'Мера схожести евклидова расстояния {
|
49 |
with col4:
|
50 |
-
st.image(films.iloc[
|
|
|
6 |
import torch
|
7 |
from transformers import AutoTokenizer, AutoModel
|
8 |
from joblib import load
|
9 |
+
import faiss
|
10 |
|
11 |
tokenizer = AutoTokenizer.from_pretrained("cointegrated/rubert-tiny2")
|
12 |
model = AutoModel.from_pretrained("cointegrated/rubert-tiny2")
|
|
|
23 |
return embeddings[0].cpu().numpy()
|
24 |
|
25 |
embeded_list = load('embeded_list.joblib')
|
26 |
+
index = faiss.IndexFlatL2(embeded_list.shape[1])
|
27 |
+
index.add(embeded_list.astype('float32'))
|
28 |
+
|
29 |
text = st.text_input('Введите текст')
|
30 |
count_visible = st.number_input("Введите количество отображаемых элементов", 1, 10, 5, step=1)
|
31 |
if st.button("Найти", type="primary"):
|
32 |
st.write('Количество фильмов в выборке 4950')
|
33 |
if text and count_visible:
|
34 |
embeded_text = embed_bert_cls(text, model, tokenizer).reshape(1,-1)
|
35 |
+
D, I = index.search(embeded_text, index.ntotal)
|
36 |
+
# cossim = pairwise_distances(embeded_text, embeded_list)[0]
|
37 |
for i in range(count_visible):
|
38 |
col1, col2 = st.columns(2)
|
39 |
with col1:
|
40 |
+
st.header(films.iloc[I[0]].iloc[i][2])
|
41 |
+
st.write(films.iloc[I[0]].iloc[i][3].replace('\xa0', ' '))
|
42 |
+
st.write(f'Мера схожести евклидова расстояния {D[0][i]:4f}')
|
43 |
with col2:
|
44 |
try:
|
45 |
+
st.image(films.iloc[I[0]].iloc[i][1])
|
46 |
except:
|
47 |
st.write('Нет картинки')
|
48 |
st.header('Самый не подходящий запрос')
|
49 |
col3, col4 = st.columns(2)
|
50 |
with col3:
|
51 |
+
st.header(films.iloc[I[0]].iloc[-1][2])
|
52 |
+
st.write(films.iloc[I[0]].iloc[-1][3].replace('\xa0', ' '))
|
53 |
+
st.write(f'Мера схожести евклидова расстояния {D[0][i]:.4f}')
|
54 |
with col4:
|
55 |
+
st.image(films.iloc[I[0]].iloc[-1][1])
|
requirements.txt
CHANGED
@@ -10,6 +10,7 @@ charset-normalizer==3.2.0
|
|
10 |
click==8.1.7
|
11 |
datasets==2.14.5
|
12 |
dill==0.3.7
|
|
|
13 |
filelock==3.12.4
|
14 |
frozenlist==1.4.0
|
15 |
fsspec==2023.6.0
|
|
|
10 |
click==8.1.7
|
11 |
datasets==2.14.5
|
12 |
dill==0.3.7
|
13 |
+
faiss-cpu==1.7.4
|
14 |
filelock==3.12.4
|
15 |
frozenlist==1.4.0
|
16 |
fsspec==2023.6.0
|