Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import streamlit as st
|
3 |
+
import requests
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
# set page setting
|
7 |
+
st.set_page_config(page_title='TopMovies')
|
8 |
+
|
9 |
+
# set history var
|
10 |
+
if 'history' not in st.session_state:
|
11 |
+
st.session_state.history = []
|
12 |
+
|
13 |
+
# import preprocessed data
|
14 |
+
data = pd.read_csv("./data/tags.csv")
|
15 |
+
|
16 |
+
# import similarity (to be cached)
|
17 |
+
def importSim(filename):
|
18 |
+
sim = pickle.load(open(filename, 'rb'))
|
19 |
+
return sim
|
20 |
+
|
21 |
+
similarity = importSim('similarity.pkl')
|
22 |
+
|
23 |
+
# recommender function
|
24 |
+
def recommend_image(movie, sim):
|
25 |
+
poster = []
|
26 |
+
plot = []
|
27 |
+
# index from dataframe
|
28 |
+
index = data[data['title'] == movie].index[0]
|
29 |
+
dist = dict(enumerate(sim[index]))
|
30 |
+
dist = dict(sorted(dist.items(), reverse=True, key = lambda item: item[1]))
|
31 |
+
#index from 1 because the forst is the movie itself
|
32 |
+
cnt = 0
|
33 |
+
for key in dist:
|
34 |
+
cnt = cnt+1
|
35 |
+
if cnt < 11:
|
36 |
+
title = data.iloc[key].title
|
37 |
+
posterRes, plotRes = get_poster_plot(title)
|
38 |
+
poster.append(posterRes)
|
39 |
+
plot.append(plotRes)
|
40 |
+
else:
|
41 |
+
break
|
42 |
+
|
43 |
+
return poster[1:], plot[1:]
|
44 |
+
|
45 |
+
# get poster
|
46 |
+
def get_poster_plot(title):
|
47 |
+
r = requests.get("http://www.omdbapi.com/?i=tt3896198&apikey=37765f04&t=" + title).json()
|
48 |
+
posterElement = r["Poster"]
|
49 |
+
plotElement = r["Plot"]
|
50 |
+
return posterElement, plotElement
|
51 |
+
|
52 |
+
# update last viewed list
|
53 |
+
def update_las_viewed():
|
54 |
+
if len(st.session_state.history) > 3:
|
55 |
+
st.session_state.history.pop()
|
56 |
+
|
57 |
+
# sidebar
|
58 |
+
st.sidebar.write("""
|
59 |
+
This is a content based recommender system. Pick a movie from the list or search for it and then wait for the reccomendations.
|
60 |
+
You will get six movies, posters and plots.
|
61 |
+
""")
|
62 |
+
|
63 |
+
# title
|
64 |
+
st.write("# Movie Recommendation System")
|
65 |
+
st.write("Pick a movie from the list and enjoy some new stuffs!")
|
66 |
+
|
67 |
+
# select box
|
68 |
+
title = st.selectbox("", data["title"])
|
69 |
+
if title not in st.session_state.history:
|
70 |
+
st.session_state.history.insert(0, title)
|
71 |
+
update_las_viewed()
|
72 |
+
|
73 |
+
# show data on selected
|
74 |
+
colSelected = st.columns(1)
|
75 |
+
with colSelected:
|
76 |
+
st.image(get_poster_plot(title)[0])
|
77 |
+
|
78 |
+
# recommend
|
79 |
+
with st.spinner("Getting the best movies..."):
|
80 |
+
recs, plots = recommend_image(title, similarity)
|
81 |
+
|
82 |
+
# recommendation cols
|
83 |
+
st.write("## Wath to watch next....")
|
84 |
+
col1, col2, col3 = st.columns(3)
|
85 |
+
with col1:
|
86 |
+
st.image(recs[0])
|
87 |
+
st.write(plots[0])
|
88 |
+
with col2:
|
89 |
+
st.image(recs[1])
|
90 |
+
st.write(plots[1])
|
91 |
+
with col3:
|
92 |
+
st.image(recs[2])
|
93 |
+
st.write(plots[2])
|
94 |
+
|
95 |
+
col4, col5, col6 = st.columns(3)
|
96 |
+
with col4:
|
97 |
+
st.image(recs[3])
|
98 |
+
st.write(plots[3])
|
99 |
+
with col5:
|
100 |
+
st.image(recs[4])
|
101 |
+
st.write(plots[4])
|
102 |
+
with col6:
|
103 |
+
st.image(recs[5])
|
104 |
+
st.write(plots[5])
|
105 |
+
|
106 |
+
col7, col8, col9 = st.columns(3)
|
107 |
+
with col7:
|
108 |
+
st.image(recs[6])
|
109 |
+
st.write(plots[6])
|
110 |
+
with col8:
|
111 |
+
st.image(recs[7])
|
112 |
+
st.write(plots[7])
|
113 |
+
with col9:
|
114 |
+
st.image(recs[8])
|
115 |
+
st.write(plots[8])
|
116 |
+
|
117 |
+
# last viewed
|
118 |
+
st.write("## Last viewed:")
|
119 |
+
r1, r2, r3 = st.columns(3)
|
120 |
+
with r1:
|
121 |
+
try:
|
122 |
+
st.image(get_poster_plot(st.session_state.history[0])[0])
|
123 |
+
except IndexError:
|
124 |
+
pass
|
125 |
+
|
126 |
+
with r2:
|
127 |
+
try:
|
128 |
+
st.image(get_poster_plot(st.session_state.history[1])[0])
|
129 |
+
except IndexError:
|
130 |
+
pass
|
131 |
+
|
132 |
+
with r3:
|
133 |
+
try:
|
134 |
+
st.image(get_poster_plot(st.session_state.history[2])[0])
|
135 |
+
except IndexError:
|
136 |
+
pass
|
137 |
+
|