File size: 3,188 Bytes
b0a71e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3244f3
b0a71e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pickle
import streamlit as st
import requests
import pandas as pd

# set page setting
st.set_page_config(page_title='TopMovies')

# set history var
if 'history' not in st.session_state:
    st.session_state.history = []

# import preprocessed data
data = pd.read_csv("./data/tags.csv")

# import similarity (to be cached)
def importSim(filename):
    sim = pickle.load(open(filename, 'rb'))
    return sim

similarity = importSim('similarity.pkl')

# recommender function
def recommend_image(movie, sim):
    poster = []
    plot = []
    # index from dataframe
    index = data[data['title'] == movie].index[0]
    dist = dict(enumerate(sim[index]))
    dist = dict(sorted(dist.items(), reverse=True, key = lambda item: item[1]))
    #index from 1 because the forst is the movie itself
    cnt = 0
    for key in dist: 
        cnt = cnt+1
        if cnt < 11:
            title = data.iloc[key].title
            posterRes, plotRes = get_poster_plot(title) 
            poster.append(posterRes)
            plot.append(plotRes)
        else:
            break
        
    return poster[1:], plot[1:]

# get poster
def get_poster_plot(title):
    r = requests.get("http://www.omdbapi.com/?i=tt3896198&apikey=37765f04&t=" + title).json()
    posterElement = r["Poster"]
    plotElement = r["Plot"]
    return posterElement, plotElement

# update last viewed list
def update_las_viewed():
    if len(st.session_state.history) > 3:
        st.session_state.history.pop()

# sidebar
st.sidebar.write("""
This is a content based recommender system. Pick a movie from the list or search for it and then wait for the reccomendations.
You will get six movies, posters and plots.
""")

# title
st.write("# Movie Recommendation System")
st.write("Pick a movie from the list and enjoy some new stuffs!")

# select box
title = st.selectbox("", data["title"])
if title not in st.session_state.history:
    st.session_state.history.insert(0, title)
update_las_viewed()

# recommend
with st.spinner("Getting the best movies..."):
    recs, plots = recommend_image(title, similarity)

# recommendation cols
st.write("## What to watch next....")
col1, col2, col3 = st.columns(3)
with col1:
    st.image(recs[0])
    st.write(plots[0])
with col2:
    st.image(recs[1])
    st.write(plots[1])
with col3:
    st.image(recs[2])
    st.write(plots[2])

col4, col5, col6 = st.columns(3)
with col4:
    st.image(recs[3])
    st.write(plots[3])
with col5:
    st.image(recs[4])
    st.write(plots[4])
with col6:
    st.image(recs[5])
    st.write(plots[5])

col7, col8, col9 = st.columns(3)
with col7:
    st.image(recs[6])
    st.write(plots[6])
with col8:
    st.image(recs[7])
    st.write(plots[7])
with col9:
    st.image(recs[8])
    st.write(plots[8])

# last viewed
st.write("## Last viewed:")
r1, r2, r3 = st.columns(3)
with r1:
    try:
        st.image(get_poster_plot(st.session_state.history[0])[0])
    except IndexError:
        pass
    
with r2:
    try:
        st.image(get_poster_plot(st.session_state.history[1])[0])
    except IndexError:
        pass
    
with r3:
    try:
        st.image(get_poster_plot(st.session_state.history[2])[0])
    except IndexError:
        pass