Spaces:
Runtime error
Runtime error
File size: 2,253 Bytes
83a648d |
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 |
import streamlit as st
import torch
import numpy as np
import pandas as pd
from transformers import AutoTokenizer, AutoModel
st.title("Книжные рекомендации")
# Загрузка модели и токенизатора
model_name = "cointegrated/rubert-tiny2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name, output_hidden_states=True)
# Загрузка датасета и аннотаций к книгам
books = pd.read_csv('book_train.csv')
annot = books['annotation']
# Предобработка аннотаций и получение эмбеддингов
embeddings = []
for annotation in annot:
annotation_tokens = tokenizer.encode_plus(
annotation,
add_special_tokens=True,
max_length=128,
pad_to_max_length=True,
return_tensors='pt'
)
with torch.no_grad():
outputs = model(**annotation_tokens)
hidden_states = outputs.hidden_states
last_hidden_state = hidden_states[-2]
embeddings.append(torch.mean(last_hidden_state, dim=1).squeeze())
# Получение эмбеддинга запроса от пользователя
query = st.text_input("Введите запрос")
query_tokens = tokenizer.encode_plus(
query,
add_special_tokens=True,
max_length=128,
pad_to_max_length=True,
return_tensors='pt'
)
# Проверка, был ли введен запрос
if query:
with torch.no_grad():
query_outputs = model(**query_tokens)
query_hidden_states = query_outputs.hidden_states
query_last_hidden_state = query_hidden_states[-2]
query_embedding = torch.mean(query_last_hidden_state, dim=1).squeeze()
# Вычисление косинусного расстояния между эмбеддингом запроса и каждой аннотацией
cosine_similarities = torch.nn.functional.cosine_similarity(
query_embedding.unsqueeze(0),
torch.stack(embeddings)
)
cosine_similarities = cosine_similarities.numpy()
indices = np.argsort(cosine_similarities)[::-1]
st.header("Рекомендации")
for i in indices[:10]:
st.write(books['title'][i])
|