Spaces:
Sleeping
Sleeping
Commit ·
cabf168
1
Parent(s): 7e46461
Update stri.py
Browse files
stri.py
CHANGED
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
| 4 |
import pandas as pd
|
| 5 |
from transformers import AutoTokenizer, AutoModel
|
| 6 |
import re
|
| 7 |
-
import
|
| 8 |
|
| 9 |
st.title("Книжные рекомендации")
|
| 10 |
|
|
@@ -38,27 +38,30 @@ annot = books['annotation']
|
|
| 38 |
|
| 39 |
# Получение эмбеддингов аннотаций каждой книги в датасете
|
| 40 |
max_len = 128
|
| 41 |
-
token_annot = annot.apply(lambda x: tokenizer.encode(x, add_special_tokens=True,
|
| 42 |
-
|
| 43 |
|
| 44 |
-
padded = np.array([i + [0] * (max_len - len(i)) for i in token_annot.values]) # заполним недостающую длину нулями
|
| 45 |
-
attention_mask = np.where(padded != 0, 1, 0) # создадим маску, отметим где есть значения а где пустота
|
| 46 |
-
# Переведем numpy массивы в тензоры PyTorch
|
| 47 |
-
input_ids = torch.tensor(padded, dtype=torch.long)
|
| 48 |
-
attention_mask = torch.tensor(attention_mask, dtype=torch.long)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
book_embeddings = []
|
| 51 |
-
for inputs, attention_masks in zip(input_ids, attention_mask):
|
| 52 |
-
with torch.no_grad():
|
| 53 |
-
book_embedding = model(inputs.unsqueeze(0), attention_mask=attention_masks.unsqueeze(0))
|
| 54 |
-
book_embedding = book_embedding[0][:, 0, :] #.detach().cpu().numpy()
|
| 55 |
-
book_embeddings.append(np.squeeze(book_embedding))
|
| 56 |
-
|
| 57 |
|
| 58 |
# Определение запроса пользователя
|
| 59 |
query = st.text_input("Введите запрос")
|
| 60 |
|
| 61 |
if st.button('**Generating recommendations**'):
|
|
|
|
|
|
|
|
|
|
| 62 |
query_tokens = tokenizer.encode(query, add_special_tokens=True,
|
| 63 |
truncation=True, max_length=max_len)
|
| 64 |
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
from transformers import AutoTokenizer, AutoModel
|
| 6 |
import re
|
| 7 |
+
import pickle
|
| 8 |
|
| 9 |
st.title("Книжные рекомендации")
|
| 10 |
|
|
|
|
| 38 |
|
| 39 |
# Получение эмбеддингов аннотаций каждой книги в датасете
|
| 40 |
max_len = 128
|
| 41 |
+
# token_annot = annot.apply(lambda x: tokenizer.encode(x, add_special_tokens=True,
|
| 42 |
+
# truncation=True, max_length=max_len))
|
| 43 |
|
| 44 |
+
# padded = np.array([i + [0] * (max_len - len(i)) for i in token_annot.values]) # заполним недостающую длину нулями
|
| 45 |
+
# attention_mask = np.where(padded != 0, 1, 0) # создадим маску, отметим где есть значения а где пустота
|
| 46 |
+
# # Переведем numpy массивы в тензоры PyTorch
|
| 47 |
+
# input_ids = torch.tensor(padded, dtype=torch.long)
|
| 48 |
+
# attention_mask = torch.tensor(attention_mask, dtype=torch.long)
|
| 49 |
+
|
| 50 |
+
# book_embeddings = []
|
| 51 |
+
# for inputs, attention_masks in zip(input_ids, attention_mask):
|
| 52 |
+
# with torch.no_grad():
|
| 53 |
+
# book_embedding = model(inputs.unsqueeze(0), attention_mask=attention_masks.unsqueeze(0))
|
| 54 |
+
# book_embedding = book_embedding[0][:, 0, :] #.detach().cpu().numpy()
|
| 55 |
+
# book_embeddings.append(np.squeeze(book_embedding))
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Определение запроса пользователя
|
| 59 |
query = st.text_input("Введите запрос")
|
| 60 |
|
| 61 |
if st.button('**Generating recommendations**'):
|
| 62 |
+
with open("book_embeddings.pkl", "rb") as f:
|
| 63 |
+
book_embeddings = pickle.load(f)
|
| 64 |
+
|
| 65 |
query_tokens = tokenizer.encode(query, add_special_tokens=True,
|
| 66 |
truncation=True, max_length=max_len)
|
| 67 |
|