-
Notifications
You must be signed in to change notification settings - Fork 1
/
app2.py
91 lines (78 loc) · 2.5 KB
/
app2.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
79
80
81
82
83
84
85
86
87
88
89
90
91
from flask import Flask, render_template, request, url_for
import os
from src.modules import (
create_engine_load_data,
setup_nmf,
process_user_input,
recommend_movies,
youtubeAPIkey,
get_yt_videos
)
class PrefixMiddleware(object):
def __init__(self, app, prefix=''):
self.app = app
self.prefix = prefix
def __call__(self, environ, start_response):
if environ['PATH_INFO'].startswith(self.prefix):
environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):]
environ['SCRIPT_NAME'] = self.prefix
return self.app(environ, start_response)
else:
start_response('404', [('Content-Type', 'text/plain')])
return ["This url does not belong to the app.".encode()]
YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY')
engine, all_ratings = create_engine_load_data()
NMF_Model, genre_movie_matrix, user_movie_id_ratings_matrix = setup_nmf(
all_ratings=all_ratings,
engine=engine,
number_of_genres = 10
)
app = Flask(__name__)
# app.config["APPLICATION_ROOT"] = "/flask"
app.debug = True
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/flask')
@app.route('/')
@app.route('/home')
def home():
return render_template('index.html')
@app.route('/select')
def select():
user_input = request.args.items()
guesses_list = []
for ui in user_input:
if ui[1]:
guesses = process_user_input(user_input=ui, all_ratings=all_ratings)
guesses_list.append(guesses)
return render_template(
'select.html',
guesses_list=guesses_list,
user_input=user_input
)
@app.route('/recommend')
def recommend():
user_movie_title_list = request.args.values()
recom_movie_titles = recommend_movies(
all_ratings=all_ratings,
user_movie_title_list=user_movie_title_list,
user_movie_id_ratings_matrix=user_movie_id_ratings_matrix,
genre_movie_matrix=genre_movie_matrix,
NMF_Model=NMF_Model,
engine=engine,
number_of_recommendations=3
)
yt_results = ""
if YOUTUBE_API_KEY:
youtube = youtubeAPIkey(YOUTUBE_API_KEY)
yt_results = get_yt_videos(youtube, titles=recom_movie_titles)
else:
yt_results = []
for r in recom_movie_titles:
dict = {}
dict['recommendation'] = r
yt_results.append(dict)
return render_template(
'recommend.html',
yt_results=yt_results
)
if __name__ == '__main__':
app.run(port=5000, debug=True)