Spaces:
Sleeping
Sleeping
File size: 2,782 Bytes
4f93011 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import streamlit as st
import pandas as pd
import torch
import numpy as np
from tqdm import tqdm
from transformers import AutoTokenizer, AutoModel
import faiss
model_name = "cointegrated/rubert-tiny2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
df = pd.read_csv('final_data.csv')
MAX_LEN = 300
def embed_bert_cls(text, model=model, tokenizer=tokenizer):
t = tokenizer(text,
padding=True,
truncation=True,
return_tensors='pt',
max_length=MAX_LEN)
with torch.no_grad():
model_output = model(**{k: v.to(model.device) for k, v in t.items()})
embeddings = model_output.last_hidden_state[:, 0, :]
embeddings = torch.nn.functional.normalize(embeddings)
return embeddings[0].cpu().squeeze()
embeddings = np.loadtxt('embeddings.txt')
embeddings_tensor = [torch.tensor(embedding) for embedding in embeddings]
# Создание индекса Faiss
embeddings_matrix = np.stack(embeddings)
index = faiss.IndexFlatIP(embeddings_matrix.shape[1])
index.add(embeddings_matrix)
st.title('Приложение для рекомендации книг')
text = st.text_input('Введите запрос:')
num_results = st.number_input('Введите количество рекомендаций:', min_value=1, max_value=50, value=3)
# Add a button to trigger the recommendation process
recommend_button = st.button('Получить рекомендации')
if text and recommend_button: # Check if the user entered text and clicked the button
# Встраивание запроса и поиск ближайших векторов с использованием Faiss
query_embedding = embed_bert_cls(text)
query_embedding = query_embedding.numpy().astype('float32')
_, indices = index.search(np.expand_dims(query_embedding, axis=0), num_results)
st.subheader('Топ рекомендуемых книг:')
for i in indices[0]:
recommended_embedding = embeddings_tensor[i].numpy() # Вектор рекомендованной книги
similarity = np.dot(query_embedding, recommended_embedding) # Косинусное сходство
similarity_percent = similarity * 100
col1, col2 = st.columns([1, 3])
with col1:
st.image(df['image'][i], use_column_width=True)
with col2:
st.write(f"**Название книги:** {df['title'][i]}")
st.write(f"**Автор:** {df['author'][i]}")
st.write(f"**Описание:** {df['annotation'][i]}")
st.write(f"**Оценка сходства:** {similarity_percent:.2f}%")
st.write("---")
|