RMakushkin commited on
Commit
767c62b
1 Parent(s): a5132f5

Delete func.py

Browse files
Files changed (1) hide show
  1. func.py +0 -53
func.py DELETED
@@ -1,53 +0,0 @@
1
- import pandas as pd
2
- import numpy as np
3
- import torch
4
- from transformers import BertModel, BertTokenizer
5
- from sklearn.metrics.pairwise import cosine_similarity
6
-
7
-
8
- tokenizer = BertTokenizer.from_pretrained("DeepPavlov/rubert-base-cased-sentence")
9
- model = BertModel.from_pretrained("DeepPavlov/rubert-base-cased-sentence")
10
- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
11
-
12
-
13
- def filter_by_ganre(df: pd.DataFrame, ganre_list: list):
14
- filtered_df = df[df['ganres'].apply(lambda x: any(g in ganre_list for g in(x)))]
15
- filt_ind = filtered_df.index.to_list()
16
- return filt_ind
17
-
18
- # def mean_pooling(model_output, attention_mask):
19
- # token_embeddings = model_output['last_hidden_state']
20
- # input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
21
- # sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, 1)
22
- # sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
23
- # return sum_embeddings / sum_mask
24
-
25
- # def recommendation(filt_ind: list, embeddings: np.array, user_text: str, n=10):
26
- # token_user_text = tokenizer(user_text, return_tensors='pt', padding='max_length', truncation=True, max_length=512)
27
- # user_embeddings = torch.Tensor().to(device)
28
- # model.to(device)
29
- # model.eval()
30
- # with torch.no_grad():
31
- # batch = {k: v.to(device) for k, v in token_user_text.items()}
32
- # outputs = model(**batch)
33
- # user_embeddings = torch.cat([user_embeddings, mean_pooling(outputs, batch['attention_mask'])])
34
- # user_embeddings = user_embeddings.cpu().numpy()
35
- # cosine_similarities = cosine_similarity(embeddings[filt_ind], user_embeddings.reshape(1, -1))
36
- # df_res = pd.DataFrame(cosine_similarities.ravel(), columns=['cos_sim']).sort_values('cos_sim', ascending=False)
37
- # dict_topn = df_res.iloc[:n, :].cos_sim.to_dict()
38
- # return dict_topn
39
-
40
-
41
- def embed_user(filt_ind: list, embeddings:np.array, user_text: str, n=10):
42
- tokens = tokenizer(user_text, return_tensors="pt", padding=True, truncation=True).to(device)
43
- model.to(device)
44
- model.eval()
45
- with torch.no_grad():
46
- outputs = model(**tokens)
47
- user_embedding = outputs.last_hidden_state.mean(dim=1).cpu().numpy().reshape(1, -1)
48
- return user_embedding
49
-
50
- # cosine_similarities = cosine_similarity(embeddings[filt_ind], user_embedding.reshape(1, -1))
51
- # df_res = pd.DataFrame(cosine_similarities.ravel(), columns=['cos_sim']).sort_values('cos_sim', ascending=False)
52
- # dict_topn = df_res.iloc[:n, :].cos_sim.to_dict()
53
- # return dict_topn