Spaces:
Build error
Build error
Commit
·
3bf5220
1
Parent(s):
f4aee1b
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,26 +55,26 @@ if page == "какая-то еще":
|
|
| 55 |
# Загрузка предварительно обученной модели ruBERT
|
| 56 |
tokenizer = AutoTokenizer.from_pretrained("DeepPavlov/rubert-base-cased-sentence")
|
| 57 |
model = AutoModel.from_pretrained("DeepPavlov/rubert-base-cased-sentence")
|
|
|
|
| 58 |
def encode_description(description):
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
embeddings = pd.read_pickle('embeddings.pkl')
|
| 67 |
user_input = st.text_area('Введите описание фильма')
|
| 68 |
input_embedding = encode_description(user_input)
|
| 69 |
-
|
| 70 |
|
| 71 |
-
# Рассчитайте косинусное сходство
|
| 72 |
similarity_scores = cosine_similarity(input_embedding.view(1, -1).detach().numpy(), embeddings_tensor.reshape(embeddings_tensor.shape[0], -1))[0]
|
| 73 |
|
| 74 |
-
# Получение индексов отсортированных значений
|
| 75 |
sorted_indices = similarity_scores.argsort()[::-1]
|
| 76 |
|
| 77 |
-
# Используйте индексы для извлечения строк из DataFrame
|
| 78 |
recs = df.iloc[sorted_indices[:10]].reset_index(drop=True)
|
| 79 |
recs.index = recs.index + 1
|
| 80 |
-
st.write(recs[['movie_title', 'description']])
|
|
|
|
| 55 |
# Загрузка предварительно обученной модели ruBERT
|
| 56 |
tokenizer = AutoTokenizer.from_pretrained("DeepPavlov/rubert-base-cased-sentence")
|
| 57 |
model = AutoModel.from_pretrained("DeepPavlov/rubert-base-cased-sentence")
|
| 58 |
+
|
| 59 |
def encode_description(description):
|
| 60 |
+
tokens = tokenizer(description, return_tensors="pt")
|
| 61 |
+
with torch.no_grad():
|
| 62 |
+
outputs = model(**tokens)
|
| 63 |
+
embeddings = outputs.last_hidden_state.mean(dim=1)
|
| 64 |
+
return embeddings
|
| 65 |
|
|
|
|
| 66 |
embeddings = pd.read_pickle('embeddings.pkl')
|
| 67 |
user_input = st.text_area('Введите описание фильма')
|
| 68 |
input_embedding = encode_description(user_input)
|
| 69 |
+
embeddings_tensor = torch.stack(df['description_embedding'].tolist()).numpy()
|
| 70 |
|
| 71 |
+
# Рассчитайте косинусное сходство
|
| 72 |
similarity_scores = cosine_similarity(input_embedding.view(1, -1).detach().numpy(), embeddings_tensor.reshape(embeddings_tensor.shape[0], -1))[0]
|
| 73 |
|
| 74 |
+
# Получение индексов отсортированных значений
|
| 75 |
sorted_indices = similarity_scores.argsort()[::-1]
|
| 76 |
|
| 77 |
+
# Используйте индексы для извлечения строк из DataFrame
|
| 78 |
recs = df.iloc[sorted_indices[:10]].reset_index(drop=True)
|
| 79 |
recs.index = recs.index + 1
|
| 80 |
+
st.write(recs[['movie_title', 'description']])
|