-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recommend_Me_Stuff.py
78 lines (63 loc) · 2.89 KB
/
Recommend_Me_Stuff.py
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
"""
Main page of the ScribbleHub Explorer.
Users can generate recommended novels using a Content-Based Recommendation System.
"""
import random
import streamlit as st
from tools import utility, constants, cb_engine
from assets import page_elements
def main():
novels_df = utility.init_data_load(constants.Pages.Main) #Split init_load_data into init_session_state and init_data_load pass back relevant data depending on page.
novels_df = novels_df[novels_df.chapters > 5].reset_index(drop=True)
cb_recommender = cb_engine.CBEngine()
st.text_input(value=st.session_state.cb_novel,
placeholder=random.choice(constants.NOVEL_SUGGESTIONS),
help=constants.NOVEL_INPUT_HELP,
label=constants.NOVEL_LABEL,
label_visibility='collapsed',
key="cb_novel_input",
on_change=utility.user_input_change,
args=('cb_novel', 'cb_novel_input'))
page_elements.sort_by_widget('content_sort')
with st.container():
placeholder = st.empty()
_, col, _ = placeholder.columns([3,3,3])
with col:
col.caption("""Input a ScribbleHub Novel ID or URL for a novel you like above :point_up:""")
if st.session_state.cb_novel != "":
novel_id = utility.parse_novel_input(st.session_state.cb_novel)
if utility.is_valid_novel(novel_id, novels_df):
_, col, _ = placeholder.columns([3,2,3])
with col:
with st.spinner("""Buidling Similarity Matrix.
This may take awhile... (๑>ᴗ<๑) """):
similarity_matrix, indices = cb_recommender.fit(novels_df=novels_df)
placeholder.empty()
recommended_novels = cb_recommender.recommend(
novel_id=novel_id,
N=st.session_state.number,
matrix=similarity_matrix,
indices=indices,
novels_df=novels_df)
recommended_novels = utility.sort_recommended_novels(
recommended_novels,
'content_sort')
page_elements.generate_novel_list(recommended_novels)
if __name__ == '__main__':
st.set_page_config(
page_title=constants.Pages.Main.value.get('page_name'),
page_icon=constants.Pages.Main.value.get('icon'),
layout='wide',
initial_sidebar_state='collapsed',
menu_items={
'Get Help': constants.GET_HELP_URL,
'Report a bug': constants.REPORT,
'About': constants.ABOUT_URL
}
)
utility.init_sessions_state()
page_elements.header(constants.Pages.Main)
page_elements.sidebar_menu(constants.Pages.Main)
main()
if constants.BUILD == "TEST":
st.write(st.session_state)