Spaces:
Running
Running
File size: 12,100 Bytes
ae7e3fa a7cb90f ae7e3fa a7cb90f ae7e3fa |
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
import streamlit as st
import numpy as np
import pandas as pd
from sentence_transformers import SentenceTransformer
import faiss
# title itself
col1, col2, col3 = st.columns(spec=[0.4, 0.5, 0.2])
with col1:
st.image('images/book03.PNG', width=160)
with col2:
st.subheader("Умный поиск книг")
st.divider()
@st.cache_resource
def load_resources():
# Загрузка модели
model = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
model1 = SentenceTransformer('sentence-transformers/paraphrase-multilingual-mpnet-base-v2')
model2 = SentenceTransformer('intfloat/multilingual-e5-large')
# Загрузка FAISS-индекса
index = faiss.read_index("indexes/index1.faiss")
index1 = faiss.read_index('indexes/index2.faiss')
index2 = faiss.read_index('indexes/index3.faiss')
df = pd.read_csv('dff.csv')
return model, model1, model2, index, index1, index2, df
model, model1,model2, index, index1, index2, df = load_resources()
user_query = st.text_input(
"📖 Введите описание книги, которую хотите найти:",
placeholder="Например: детектив о загадочном убийстве в маленьком городке"
)
num_results = st.slider(
"Количество рекомендаций:",
min_value=1,
max_value=20,
value=5,
help="Выберите сколько книг показать в результатах"
)
search_button = st.button("Искать")
# --- Обработка и вывод результатов ---
if search_button and user_query:
with st.spinner("Ищем лучшие варианты..."):
try:
# Преобразование запроса в вектор
test_embedding = model.encode([user_query])
faiss.normalize_L2(test_embedding)
# Поиск в индексе
distances, indices = index.search(test_embedding, num_results)
# Отображение результатов
st.subheader(f"Найденные варианты для запроса Модель 1: '{user_query}'")
for i, (idx, score) in enumerate(zip(indices[0], distances[0])):
book = df.iloc[idx]
# Создаем карточку для каждой книги
with st.container():
cols = st.columns([0.2, 0.8])
with cols[0]:
try:
if pd.notna(book['image_url']): # Проверяем, что URL не пустой
st.image(
book['image_url'],
width=120,
caption="Обложка книги",
use_container_width="auto"
)
else:
st.warning("Обложка недоступна")
st.image("images/reklama.jpg", width=120) # Если есть колонка с изображениями
except Exception as e:
st.error(f"Ошибка загрузки обложки: {str(e)}")
st.image("images/reklama.jpg", width=120)
with cols[1]:
st.markdown(f"**{book['title']}**")
st.caption(f"Автор: {book['author']}")
st.write(f"**Сходство:** {score:.3f}")
# Добавляем раскрывающуюся аннотацию
annotation = book['annotation']
short_length = 150 # Количество символов для сокращённой версии
if len(annotation) > short_length:
short_annotation = annotation[:short_length] + "..."
with st.expander(f"Аннотация: {short_annotation}"):
st.write(annotation)
else:
st.write(f"**Аннотация:** {annotation}")
st.divider()
except Exception as e:
st.error(f"Ошибка при поиске: {str(e)}")
elif search_button and not user_query:
st.warning("Пожалуйста, введите описание для поиска!")
if search_button and user_query:
with st.spinner("Ищем лучшие варианты..."):
try:
# Преобразование запроса в вектор
test_embedding = model1.encode([user_query])
faiss.normalize_L2(test_embedding)
# Поиск в индексе
distances1, indices1 = index1.search(test_embedding, num_results)
# Отображение результатов
st.subheader(f"Найденные варианты для запроса Модель 2: '{user_query}'")
for i, (idx, score) in enumerate(zip(indices1[0], distances1[0])):
book = df.iloc[idx]
# Создаем карточку для каждой книги
with st.container():
cols = st.columns([0.2, 0.8])
with cols[0]:
try:
if pd.notna(book['image_url']): # Проверяем, что URL не пустой
st.image(
book['image_url'],
width=120,
caption="Обложка книги",
use_container_width="auto"
)
else:
st.warning("Обложка недоступна")
st.image("images/reklama.jpg", width=120) # Если есть колонка с изображениями
except Exception as e:
st.error(f"Ошибка загрузки обложки: {str(e)}")
st.image("images/reklama.jpg", width=120)
with cols[1]:
st.markdown(f"**{book['title']}**")
st.caption(f"Автор: {book['author']}")
st.write(f"**Сходство:** {score:.3f}")
# Добавляем раскрывающуюся аннотацию
annotation = book['annotation']
short_length = 150 # Количество символов для сокращённой версии
if len(annotation) > short_length:
short_annotation = annotation[:short_length] + "..."
with st.expander(f"Аннотация: {short_annotation}"):
st.write(annotation)
else:
st.write(f"**Аннотация:** {annotation}")
st.divider()
except Exception as e:
st.error(f"Ошибка при поиске: {str(e)}")
elif search_button and not user_query:
pass
if search_button and user_query:
with st.spinner("Ищем лучшие варианты..."):
try:
# Преобразование запроса в вектор
test_embedding = model2.encode([user_query])
faiss.normalize_L2(test_embedding)
# Поиск в индексе
distances2, indices2 = index2.search(test_embedding, num_results)
# Отображение результатов
st.subheader(f"Найденные варианты для запроса Модель 3: '{user_query}'")
for i, (idx, score) in enumerate(zip(indices2[0], distances2[0])):
book = df.iloc[idx]
# Создаем карточку для каждой книги
with st.container():
cols = st.columns([0.2, 0.8])
with cols[0]:
try:
if pd.notna(book['image_url']): # Проверяем, что URL не пустой
st.image(
book['image_url'],
width=120,
caption="Обложка книги",
use_container_width="auto"
)
else:
st.warning("Обложка недоступна")
st.image("images/reklama.jpg", width=120) # Если есть колонка с изображениями
except Exception as e:
st.error(f"Ошибка загрузки обложки: {str(e)}")
st.image("images/reklama.jpg", width=120)
with cols[1]:
st.markdown(f"**{book['title']}**")
st.caption(f"Автор: {book['author']}")
st.write(f"**Сходство:** {score:.3f}")
# Добавляем раскрывающуюся аннотацию
annotation = book['annotation']
short_length = 150 # Количество символов для сокращённой версии
if len(annotation) > short_length:
short_annotation = annotation[:short_length] + "..."
with st.expander(f"Аннотация: {short_annotation}"):
st.write(annotation)
else:
st.write(f"**Аннотация:** {annotation}")
st.divider()
except Exception as e:
st.error(f"Ошибка при поиске: {str(e)}")
elif search_button and not user_query:
pass
if search_button and user_query:
with st.spinner("Сравниваем полученные варианты..."):
try:
col1, col2, col3, col4 = st.columns(spec=[0.1, 0.3, 0.3, 0.3])
with col1:
st.write('#')
with col2:
st.write('Model-1')
with col3:
st.write('Model-2')
with col4:
st.write('Model-3')
for i in range(0, num_results):
col1, col2, col3, col4 = st.columns(spec=[0.1, 0.3, 0.3, 0.3])
with col1:
st.write(str(i))
with col2:
st.image(df.loc[indices[0][i], 'image_url'], width=100)
with col3:
st.image(df.loc[indices1[0][i], 'image_url'], width=100)
with col4:
st.image(df.loc[indices2[0][i], 'image_url'], width=100)
st.divider()
except Exception as e:
st.error(f"Ошибка при поиске: {str(e)}")
|