-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
266 lines (219 loc) · 9.53 KB
/
main.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import numpy as np
import pandas as pd
from flask import Flask, Response, render_template, request, jsonify
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import json
import bs4 as bs
import urllib.request
import pickle
import requests
from server.mood import get_movie_recommendations
from flask_cors import CORS
app = Flask(__name__)
# CORS configuration
cors = CORS(app, resources={"/recommendations": {"origins": ["http://127.0.0.1:5500"]}})
# ... your other application code ...
# CORS configuration (adjust origins as needed)
app.config['CORS_HEADERS'] = 'Content-Type' # Allow content type header in responses
# load the nlp model and tfidf vectorizer from disk
filename = 'nlp_model.pkl'
clf = pickle.load(open(filename, 'rb'))
vectorizer = pickle.load(open('tranform.pkl','rb'))
def load_dataset(file_path):
try:
return pd.read_csv(file_path)
except FileNotFoundError:
return None
except Exception as e:
print("An error occurred while loading the dataset:", e)
return None
def get_release_year(release_date):
try:
return pd.to_datetime(release_date).year
except:
return None
def get_movie_recommendations(dataset, emotion, preferred_language):
# Map emotion to genre
emotion_to_genre = {
"Happy" : "Comedy",
"Sad" : "Drama",
"Satisfied" : "Animation",
"Angry" : "Action",
"Peaceful" : "Fantasy",
"Fearful" : "Thriller",
"Excited" : "Adventure",
"Depressed" : "Comedy", # Can be improved with a different genre
"Content" : "Mystery",
"Sorrowful" : "Drama"
# Add more mappings as needed
}
genre = emotion_to_genre.get(emotion)
if genre:
# Filter dataset by genre
genre_movies = dataset[dataset['genres'].str.contains(genre, case=False)]
# Filter by preferred language
if preferred_language:
genre_movies = genre_movies[genre_movies['original_language'] == preferred_language]
# Sort by vote_average in descending order to get highest-rated movies first
genre_movies_sorted = genre_movies.sort_values(by='vote_average', ascending=False)
# Get the top 5 movies with highest ratings
top_recommendations = genre_movies_sorted.head(5)[['original_title', 'release_date', 'overview', 'vote_average']].values.tolist()
return top_recommendations
else:
return {'error': 'Sorry, we don\'t have recommendations for that emotion.'}
@app.route('/recommendations', methods=['GET', 'POST'])
def recommendations():
if request.method == 'GET':
# Serve the HTML page for the GET request
return render_template('mood_based.html')
elif request.method == 'POST':
# Handle the POST request for getting recommendations
data = request.get_json()
emotion = data.get('emotion')
language = data.get('language')
if not emotion or not language:
return jsonify({'error': 'Emotion and language are required parameters'}), 400
file_path = 'datasets/movies_metadata.csv' # Adjust the file path as needed
dataset = load_dataset(file_path)
if dataset is None:
return jsonify({'error': 'Failed to load dataset'}), 500
recommendations = get_movie_recommendations(dataset, emotion, language)
return jsonify(recommendations)
def create_similarity():
data = pd.read_csv('main_data.csv')
# creating a count matrix
cv = CountVectorizer()
count_matrix = cv.fit_transform(data['comb'])
# creating a similarity score matrix
similarity = cosine_similarity(count_matrix)
return data,similarity
def rcmd(m):
m = m.lower()
try:
data.head()
similarity.shape
except:
data, similarity = create_similarity()
if m not in data['movie_title'].unique():
return('Sorry! The movie you requested is not in our database. Please check the spelling or try with some other movies')
else:
i = data.loc[data['movie_title']==m].index[0]
lst = list(enumerate(similarity[i]))
lst = sorted(lst, key = lambda x:x[1] ,reverse=True)
lst = lst[1:11] # excluding first item since it is the requested movie itself
l = []
for i in range(len(lst)):
a = lst[i][0]
l.append(data['movie_title'][a])
return l
# converting list of string to list (eg. "["abc","def"]" to ["abc","def"])
def convert_to_list(my_list):
my_list = my_list.split('","')
my_list[0] = my_list[0].replace('["','')
my_list[-1] = my_list[-1].replace('"]','')
return my_list
def get_suggestions():
data = pd.read_csv('main_data.csv')
return list(data['movie_title'].str.capitalize())
# app = Flask(__name__)
@app.route("/home")
def home():
return render_template('index.html')
@app.route("/suggestions")
def suggestions():
suggestions = get_suggestions()
return render_template('home.html',suggestions=suggestions)
# @app.route("/mood")
# def mood():
# return render_template('mood_based.html')
# file_path = 'datasets/movie_metadata.csv'
# dataset = pd.read_csv(file_path)
# @app.route("/mood", methods=[ 'GET', 'POST'])
# def mood():
# if request.method == 'POST':
# # Get emotion and language from the form
# emotion = request.form['emotion']
# preferred_language = request.form['language']
# # Get movie recommendations based on emotion and language
# recommendations = get_movie_recommendations(dataset, emotion, preferred_language)
# # Return the recommendations as JSON
# return {'recommendations': recommendations}
# else:
# # Render the mood_based.html template initially
# return render_template('mood_based.html')
@app.route("/similarity",methods=["POST"])
def similarity():
movie = request.form['name']
rc = rcmd(movie)
if type(rc)==type('string'):
return rc
else:
m_str="---".join(rc)
return m_str
@app.route("/recommend",methods=["POST"])
def recommend():
# getting data from AJAX request
title = request.form['title']
cast_ids = request.form['cast_ids']
cast_names = request.form['cast_names']
cast_chars = request.form['cast_chars']
cast_bdays = request.form['cast_bdays']
cast_bios = request.form['cast_bios']
cast_places = request.form['cast_places']
cast_profiles = request.form['cast_profiles']
imdb_id = request.form['imdb_id']
poster = request.form['poster']
genres = request.form['genres']
overview = request.form['overview']
vote_average = request.form['rating']
vote_count = request.form['vote_count']
release_date = request.form['release_date']
runtime = request.form['runtime']
status = request.form['status']
rec_movies = request.form['rec_movies']
rec_posters = request.form['rec_posters']
# get movie suggestions for auto complete
suggestions = get_suggestions()
# call the convert_to_list function for every string that needs to be converted to list
rec_movies = convert_to_list(rec_movies)
rec_posters = convert_to_list(rec_posters)
cast_names = convert_to_list(cast_names)
cast_chars = convert_to_list(cast_chars)
cast_profiles = convert_to_list(cast_profiles)
cast_bdays = convert_to_list(cast_bdays)
cast_bios = convert_to_list(cast_bios)
cast_places = convert_to_list(cast_places)
# convert string to list (eg. "[1,2,3]" to [1,2,3])
cast_ids = cast_ids.split(',')
cast_ids[0] = cast_ids[0].replace("[","")
cast_ids[-1] = cast_ids[-1].replace("]","")
# rendering the string to python string
for i in range(len(cast_bios)):
cast_bios[i] = cast_bios[i].replace(r'\n', '\n').replace(r'\"','\"')
# combining multiple lists as a dictionary which can be passed to the html file so that it can be processed easily and the order of information will be preserved
movie_cards = {rec_posters[i]: rec_movies[i] for i in range(len(rec_posters))}
casts = {cast_names[i]:[cast_ids[i], cast_chars[i], cast_profiles[i]] for i in range(len(cast_profiles))}
cast_details = {cast_names[i]:[cast_ids[i], cast_profiles[i], cast_bdays[i], cast_places[i], cast_bios[i]] for i in range(len(cast_places))}
# web scraping to get user reviews from IMDB site
sauce = urllib.request.urlopen('https://www.imdb.com/title/{}/reviews?ref_=tt_ov_rt'.format(imdb_id)).read()
soup = bs.BeautifulSoup(sauce,'lxml')
soup_result = soup.find_all("div",{"class":"text show-more__control"})
reviews_list = [] # list of reviews
reviews_status = [] # list of comments (good or bad)
for reviews in soup_result:
if reviews.string:
reviews_list.append(reviews.string)
# passing the review to our model
movie_review_list = np.array([reviews.string])
movie_vector = vectorizer.transform(movie_review_list)
pred = clf.predict(movie_vector)
reviews_status.append('Good' if pred else 'Bad')
# combining reviews and comments into a dictionary
movie_reviews = {reviews_list[i]: reviews_status[i] for i in range(len(reviews_list))}
# passing all the data to the html file
return render_template('recommend.html',title=title,poster=poster,overview=overview,vote_average=vote_average,
vote_count=vote_count,release_date=release_date,runtime=runtime,status=status,genres=genres,
movie_cards=movie_cards,reviews=movie_reviews,casts=casts,cast_details=cast_details)
if __name__ == '__main__':
app.run(debug=True)