Spaces:
Sleeping
Sleeping
Commit
·
ff6cf2b
1
Parent(s):
dddc284
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,9 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
import numpy as np
|
4 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
-
from sentence_transformers import SentenceTransformer, util
|
6 |
-
from transformers import AutoTokenizer, AutoModel
|
7 |
-
from sentence_transformers import SentenceTransformer, InputExample, losses
|
8 |
-
from torch.utils.data import DataLoader
|
9 |
import torch
|
10 |
-
import
|
11 |
-
import re
|
12 |
import faiss
|
|
|
13 |
|
14 |
|
15 |
|
@@ -58,7 +52,11 @@ if page == "какая-то еще":
|
|
58 |
# Загрузка предварительно обученной модели ruBERT
|
59 |
tokenizer = AutoTokenizer.from_pretrained("DeepPavlov/rubert-base-cased-sentence")
|
60 |
model = AutoModel.from_pretrained("DeepPavlov/rubert-base-cased-sentence")
|
61 |
-
|
|
|
|
|
|
|
|
|
62 |
def encode_description(description):
|
63 |
tokens = tokenizer(description, return_tensors="pt")
|
64 |
with torch.no_grad():
|
@@ -66,29 +64,30 @@ if page == "какая-то еще":
|
|
66 |
embeddings = outputs.last_hidden_state.mean(dim=1)
|
67 |
return embeddings.cpu().numpy().astype('float32')
|
68 |
|
69 |
-
embeddings_array = np.load('embeddings.npy')
|
70 |
-
index = faiss.read_index('desc_faiss_index.index')
|
71 |
-
|
72 |
-
df2 = pd.read_csv('data_with_embeddings.csv')
|
73 |
# embeddings = pd.read_pickle('embeddings.pkl')
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
78 |
|
79 |
-
if st.button("
|
80 |
if user_input:
|
81 |
# Векторизация введенного запроса
|
82 |
input_embedding = encode_description(user_input)
|
83 |
|
84 |
# Поиск с использованием Faiss
|
85 |
-
_, sorted_indices = index.search(input_embedding.reshape(1, -1),
|
86 |
|
87 |
# Используйте индексы для извлечения строк из DataFrame
|
88 |
recs = df.iloc[sorted_indices[0]].reset_index(drop=True)
|
89 |
recs.index = recs.index + 1
|
90 |
|
91 |
-
# Вывод рекомендованных фильмов
|
92 |
st.write("Рекомендованные фильмы:")
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import torch
|
4 |
+
from transformers import AutoTokenizer, AutoModel
|
|
|
5 |
import faiss
|
6 |
+
import numpy as np
|
7 |
|
8 |
|
9 |
|
|
|
52 |
# Загрузка предварительно обученной модели ruBERT
|
53 |
tokenizer = AutoTokenizer.from_pretrained("DeepPavlov/rubert-base-cased-sentence")
|
54 |
model = AutoModel.from_pretrained("DeepPavlov/rubert-base-cased-sentence")
|
55 |
+
|
56 |
+
df2 = pd.read_csv('data_with_embeddings.csv')
|
57 |
+
embeddings_array = np.load('embeddings.npy')
|
58 |
+
index = faiss.read_index('desc_faiss_index.index')
|
59 |
+
|
60 |
def encode_description(description):
|
61 |
tokens = tokenizer(description, return_tensors="pt")
|
62 |
with torch.no_grad():
|
|
|
64 |
embeddings = outputs.last_hidden_state.mean(dim=1)
|
65 |
return embeddings.cpu().numpy().astype('float32')
|
66 |
|
|
|
|
|
|
|
|
|
67 |
# embeddings = pd.read_pickle('embeddings.pkl')
|
68 |
|
69 |
+
def main():
|
70 |
+
st.title("Система поиска фильмов")
|
71 |
+
|
72 |
+
# Пользовательский ввод
|
73 |
+
user_input = st.text_input("Введите описание фильма:")
|
74 |
|
75 |
+
if st.button("Искать🔍🎦')"):
|
76 |
if user_input:
|
77 |
# Векторизация введенного запроса
|
78 |
input_embedding = encode_description(user_input)
|
79 |
|
80 |
# Поиск с использованием Faiss
|
81 |
+
_, sorted_indices = index.search(input_embedding.reshape(1, -1), 5) # Изменил на 5
|
82 |
|
83 |
# Используйте индексы для извлечения строк из DataFrame
|
84 |
recs = df.iloc[sorted_indices[0]].reset_index(drop=True)
|
85 |
recs.index = recs.index + 1
|
86 |
|
87 |
+
# Вывод рекомендованных фильмов с изображениями
|
88 |
st.write("Рекомендованные фильмы:")
|
89 |
+
for i in range(5): # Изменил на 5
|
90 |
+
st.image(recs['image_url'].iloc[i], caption=recs[['movie_title', 'description']].iloc[i], use_column_width=True)
|
91 |
+
|
92 |
+
if __name__ == "__main__":
|
93 |
+
main()
|