Spaces:
Sleeping
Sleeping
File size: 4,845 Bytes
53b90ec ecb5b52 53b90ec |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
import streamlit as st
import pandas as pd
import torch
import numpy as np
from transformers import AutoTokenizer, AutoModel
import faiss
from streamlit.errors import StreamlitAPIException
import urllib.parse
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
# Load model and tokenizer
model_name = "sentence-transformers/msmarco-distilbert-base-v3"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
# Load data
books = pd.read_csv('data/data_final_version.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()
# Load embeddings
embeddings = np.loadtxt('embeddings.txt')
embeddings_tensor = [torch.tensor(embedding) for embedding in embeddings]
# Create Faiss index
embeddings_matrix = np.stack(embeddings)
index = faiss.IndexFlatIP(embeddings_matrix.shape[1])
index.add(embeddings_matrix)
# CSS стили для заднего фона
background_image = """
<style>
.stApp {
background-image: url("https://img.freepik.com/premium-photo/blur-image-book_9563-1100.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
</style>
"""
# Вставляем CSS стили в приложение Streamlit
st.markdown(background_image, unsafe_allow_html=True)
# Вставляем CSS стили для окошка с прозрачным фоном
transparent_title = """
<style>
.transparent-title {
background-color: rgba(255, 255, 255, 0.7);
padding: 10px;
border-radius: 5px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
</style>
"""
transparent_box = """
<style>
.transparent-box {
background-color: rgba(255, 255, 255, 0.7);
padding: 10px;
border-radius: 5px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
</style>
"""
# Вставляем CSS стили в приложение Streamlit
st.markdown(transparent_title, unsafe_allow_html=True)
st.markdown(transparent_box, unsafe_allow_html=True)
# Streamlit interface
st.markdown('<h1 class="transparent-title">🎓📚Приложение для рекомендаций книг📚🎓</h1>', unsafe_allow_html=True)
# Далее ваш код Streamlit
text = st.text_input('Введите ваш запрос для поиска книг:')
num_results = st.number_input('Количество результатов:', min_value=1, max_value=20, value=3)
recommend_button = st.button('Получить рекомендации')
if text and recommend_button: # Check if the user entered text and clicked the button
# Embed the query and search for nearest vectors using 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() # Vector of the recommended book
similarity = np.dot(query_embedding, recommended_embedding) / (np.linalg.norm(query_embedding) * np.linalg.norm(recommended_embedding)) # Cosine similarity
similarity_percent = similarity * 100
col1, col2 = st.columns([1, 3])
with col1:
image_url = books['image_url'][i]
if pd.isna(image_url) or not image_url or image_url.strip() == '':
st.write("Обложка не найдена")
else:
try:
st.image(image_url, use_column_width=True)
except Exception as e:
st.write("Обложка не найдена")
st.write(e)
with col2:
# Выводим информацию о книге на прозрачном фоне
st.markdown(f"""
<div class="transparent-box">
<p><b>Название книги:</b> {books['title'][i]}</p>
<p><b>Автор:</b> {books['author'][i]}</p>
<p><b>Описание:</b>{books['annotation'][i]}")
<p><b>Оценка сходства:</b> {similarity_percent:.2f}%</p>
</div>
""", unsafe_allow_html=True)
st.write("---")
|