-
Notifications
You must be signed in to change notification settings - Fork 28
/
app.py
60 lines (47 loc) · 2.04 KB
/
app.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
import flask
#import difflib
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
app = flask.Flask(__name__, template_folder='templates')
df2 = pd.read_csv('./model/tmdb.csv')
count = CountVectorizer(stop_words='english')
count_matrix = count.fit_transform(df2['soup'])
cosine_sim2 = cosine_similarity(count_matrix, count_matrix)
df2 = df2.reset_index()
indices = pd.Series(df2.index, index=df2['title'])
all_titles = [df2['title'][i] for i in range(len(df2['title']))]
def get_recommendations(title):
cosine_sim = cosine_similarity(count_matrix, count_matrix)
idx = indices[title]
sim_scores = list(enumerate(cosine_sim[idx]))
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
sim_scores = sim_scores[1:11]
movie_indices = [i[0] for i in sim_scores]
tit = df2['title'].iloc[movie_indices]
dat = df2['release_date'].iloc[movie_indices]
return_df = pd.DataFrame(columns=['Title', 'Year'])
return_df['Title'] = tit
return_df['Year'] = dat
return return_df
# Set up the main route
@app.route('/', methods=['GET', 'POST'])
def main():
if flask.request.method == 'GET':
return (flask.render_template('index.html'))
if flask.request.method == 'POST':
m_name = flask.request.form['movie_name']
m_name = m_name.title()
# check = difflib.get_close_matches(m_name,all_titles,cutout=0.50,n=1)
if m_name not in all_titles:
return (flask.render_template('negative.html', name=m_name))
else:
result_final = get_recommendations(m_name)
names = []
dates = []
for i in range(len(result_final)):
names.append(result_final.iloc[i][0])
dates.append(result_final.iloc[i][1])
return flask.render_template('positive.html', movie_names=names, movie_date=dates, search_name=m_name)
if __name__ == '__main__':
app.run()