-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
180 lines (159 loc) · 4.5 KB
/
Copy pathapp.py
File metadata and controls
180 lines (159 loc) · 4.5 KB
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
# import std libraries
import numpy as np
import pandas as pd
import time
from IPython.display import HTML
import pickle
import json
import sklearn
import streamlit as st
from st_aggrid import AgGrid
from utils import DISTANCE_MODEL, NMF_MODEL
from recommenders import recommend_neighborhood, recommend_nmf, ratings
BEST_MOVIES = pd.read_csv("best_movies.csv")
BEST_MOVIES.rename(
index=lambda x: x+1,
inplace=True
)
TITLES = ["---"] + list(BEST_MOVIES['title'].sort_values())
# sidebar
with st.sidebar:
# title
st.title("It's movie time!")
# image
st.image('movie_time.jpg')
# blank space
st.write("")
# selectbox
page = st.selectbox(
"what would you like?",
[
"welcome page",
"popular movies",
"rate some movies",
"recommended movies"
]
)
if page == "welcome page":
# slogan
st.write("""
*Movies are like magic tricks (Jeff Bridges)*
""")
# blank space
st.write("")
# image
st.image('movie_pics.png')
##########################################################
# Popular Movies
##########################################################
elif page == "popular movies":
# title
st.title("Popular Movies")
col1,col2,col3,col4 = st.columns([10,1,5,5])
with col1:
n = st.slider(
label="how many movies?",
min_value=1,
max_value=10
)
with col3:
st.markdown("####")
genre = st.checkbox("include genres")
with col4:
st.markdown("###")
show_button = st.button(label="show movies")
if genre:
popular_movies = BEST_MOVIES[['movie_title','genres']]
else:
popular_movies = BEST_MOVIES[['movie_title']]
st.markdown("###")
if show_button:
st.write(
HTML(popular_movies.head(n).to_html(escape=False))
)
##########################################################
# Rate Movies
##########################################################
elif page == "rate some movies":
# title
st.title("Rate Movies")
#
col1,col2,col3 = st.columns([10,1,5])
with col1:
m1 = st.selectbox("movie 1", TITLES)
st.write("")
m2 = st.selectbox("movie 2", TITLES)
st.write("")
m3 = st.selectbox("movie 3", TITLES)
st.write("")
m4 = st.selectbox("movie 4", TITLES)
st.write("")
m5 = st.selectbox("movie 5", TITLES)
with col3:
r1 = st.slider(
label="rating 1",
min_value=1,
max_value=5,
value=3
)
r2 = st.slider(
label="rating 2",
min_value=1,
max_value=5,
value=3
)
r3 = st.slider(
label="rating 3",
min_value=1,
max_value=5,
value=3
)
r4 = st.slider(
label="rating 4",
min_value=1,
max_value=5,
value=3
)
r5 = st.slider(
label="rating 5",
min_value=1,
max_value=5,
value=3
)
query_movies = [m1,m2,m3,m4,m5]
query_ratings = [r1,r2,r3,r4,r5]
user_query = dict(zip(query_movies,query_ratings))
# get user query
st.markdown("###")
user_query_button = st.button(label="save user query")
if user_query_button:
json.dump(
user_query,
open("user_query.json",'w')
)
st.write("")
st.write("user query saved successfully")
##########################################################
# Movie Recommendations
##########################################################
else:
# title
st.title("Movie Recommendations")
col1,col2,col3,col4,col5 = st.columns([1,5,1,5,1])
with col2:
recommender = st.radio(
"recommender type",
["NMF Recommender","Distance Recommender"]
)
with col4:
st.write("###")
recommend_button = st.button(label="recommed movies")
#load user query
user_query = json.load(open("user_query.json"))
if recommend_button:
if recommender == "NMF Recommender":
recommenders = recommend_nmf(user_query, NMF_MODEL, k=10)
elif recommender == "Distance Recommender":
recommenders = recommend_neighborhood(user_query, DISTANCE_MODEL, ratings, k=10)
for title in recommenders:
st.markdown(f'### {title}')