File size: 3,755 Bytes
d80f9cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5607361
d80f9cb
 
 
 
 
 
 
 
 
 
 
b0b1309
d80f9cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import random as rd
import webbrowser as wb
import numpy as np
from assets.find import find_similar,df,lems_eng,lems_rus,clean

st.set_page_config(
    page_title="Умный поиск книг",
    page_icon="📖",
    layout="wide",
)
with st.expander('Исходный датафрейм'):
        #print(list(df.columns))#df.columns())
        columns= st.multiselect('Выберите колонки для отображения',options=list(df.columns),default=list(df.columns))
        
        df.loc[:,columns]#'df.columns,default=df.columns)
        #df.loc[:,columns]
st.title('Умный поиск книг')
#negability= st.checkbox('Негативный промт (beta)')
with st.form(key='search_form'):
    
    input=st.text_input('Введите поисковый запрос','Успешный успех, как заработать много денег')

    # if negability:
    #     neg=st.text_input('Введите отрицательный запрос')


    search_but=st.form_submit_button('Искать')

items_per_page=st.number_input('Количество книг на странице',min_value=1,max_value=10,value=5)
# if search_but:
#     st.session_state.clicked = True
#st.toast('Уфф')
@st.cache_data(experimental_allow_widgets=True)
def books_show(books_idx,sim,n=items_per_page):
    col=[]
    books=df.copy().iloc[books_idx][:n]
    for author in books['author']:
        if author.find('Донцова')!=-1:
            #st.toast('Уфф')
            pass
    books['sims']=sims[:n]
    with st.expander('Датафрейм с результатами'):
        books.loc[:,columns.__add__(['sims'])]
    #print(books.index)
    for i,book_id in enumerate(books_idx[:n]):
        pic_col,text_col=st.columns([0.2,0.8])
        '---'
        
        url=books.loc[book_id][0]
        #url
        pic_col.image(books.loc[book_id,'image_url'],use_column_width=True)
        pic_col.markdown(f'<a href={url} target="_blank">Ссылка на книгу</a>', unsafe_allow_html=True)
        pic_col.markdown(f'**Степень похожести:** {books.loc[book_id,"sims"]*100:.4f}%')

        #col[i][0].button('Купить',key=books['page_url'][i],on_click=lambda: wb.open_new_tab(books['page_url'][i]))

        text_col.markdown('## ' + books.loc[book_id, 'title'])
        text_col.markdown('**Автор:** ' + books.loc[book_id, 'author'])
        text_col.markdown('**Жанр:** ' + books.loc[book_id, 'genre'])
        text_col.markdown('**Аннотация:** ' + books.loc[book_id, 'annotation'])
  
if search_but:
    neg_mark=input.find(' -')
    if neg_mark==-1:
        cleaned_input=clean(lems_eng(lems_rus(input)))
    else:
        cleaned_input=clean(lems_eng(lems_rus(input[:neg_mark])))
        cleaned_neg=clean(lems_eng(lems_rus(input[neg_mark+2:])))
    #print(cleaned_neg.split(),df.loc[15390,'lemmatized'].split())
    with st.spinner('Wait for it...'):
        if neg_mark!=-1:
            st.markdown(f'**Лемматизированный запрос:** {cleaned_input} \n\n **Лемматизированый негативный запрос:** {cleaned_neg}')
            sims,books_idx=find_similar(cleaned_input,50)
            for book in books_idx:
                if any(word in cleaned_neg.split() for word in df.loc[book,'lemmatized'].split()):
                    books_idx=np.delete(books_idx,np.where(books_idx==book))        
        else:
            st.markdown(f'**Лемматизированный запрос:** {cleaned_input}')
            sims,books_idx=find_similar(input)
        print(f'Похожести:\n{sims}\nИндексы:\n{books_idx}')
        books_show(books_idx,sims)