Spaces:
Sleeping
Sleeping
Commit
·
8d176bf
1
Parent(s):
d3d9b91
Update app.py
Browse files
app.py
CHANGED
@@ -65,25 +65,20 @@ if page == "какая-то еще":
|
|
65 |
outputs = model(**tokens)
|
66 |
embeddings = outputs.last_hidden_state.mean(dim=1)
|
67 |
return embeddings
|
68 |
-
|
69 |
-
# Чтение данных из файла
|
70 |
df2 = pd.read_csv('data_with_embeddings.csv')
|
71 |
-
|
72 |
-
# Обработка пустых значений
|
73 |
-
df2['description_embedding'] = df2['description_embedding'].apply(lambda x: [] if pd.isna(x) else x)
|
74 |
-
|
75 |
-
# Объединение тензоров в один большой тензор
|
76 |
-
embeddings_tensor = torch.stack(df2['description_embedding'].apply(torch.tensor).tolist())
|
77 |
-
|
78 |
-
# Получение ввода пользователя
|
79 |
user_input = st.text_area('Введите описание фильма')
|
80 |
-
|
81 |
-
# Код для получения вектора вложения ввода пользователя
|
82 |
input_embedding = encode_description(user_input)
|
|
|
|
|
83 |
|
84 |
-
#
|
85 |
similarity_scores = cosine_similarity(input_embedding.view(1, -1).detach().numpy(), embeddings_tensor.reshape(embeddings_tensor.shape[0], -1))[0]
|
|
|
|
|
86 |
sorted_indices = similarity_scores.argsort()[::-1]
|
|
|
|
|
87 |
recs = df2.iloc[sorted_indices[:10]].reset_index(drop=True)
|
88 |
recs.index = recs.index + 1
|
89 |
-
st.write(recs[['movie_title', 'description']])
|
|
|
65 |
outputs = model(**tokens)
|
66 |
embeddings = outputs.last_hidden_state.mean(dim=1)
|
67 |
return embeddings
|
|
|
|
|
68 |
df2 = pd.read_csv('data_with_embeddings.csv')
|
69 |
+
embeddings = pd.read_pickle('embeddings.pkl')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
user_input = st.text_area('Введите описание фильма')
|
|
|
|
|
71 |
input_embedding = encode_description(user_input)
|
72 |
+
embeddings_tensor = torch.stack([torch.Tensor(ast.literal_eval(embedding_str)) for embedding_str in df2['description_embedding']]).numpy()
|
73 |
+
|
74 |
|
75 |
+
# Рассчитайте косинусное сходство
|
76 |
similarity_scores = cosine_similarity(input_embedding.view(1, -1).detach().numpy(), embeddings_tensor.reshape(embeddings_tensor.shape[0], -1))[0]
|
77 |
+
|
78 |
+
# Получение индексов отсортированных значений
|
79 |
sorted_indices = similarity_scores.argsort()[::-1]
|
80 |
+
|
81 |
+
# Используйте индексы для извлечения строк из DataFrame
|
82 |
recs = df2.iloc[sorted_indices[:10]].reset_index(drop=True)
|
83 |
recs.index = recs.index + 1
|
84 |
+
st.write(recs[['movie_title', 'description']])
|