-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
242 lines (183 loc) · 7.76 KB
/
app.py
File metadata and controls
242 lines (183 loc) · 7.76 KB
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
import pandas as pd
import numpy as np
import difflib
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from flask import Flask, jsonify, request
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import requests
import json
from flask_cors import CORS
import pickle
import tensorflow as tf
import re
from tensorflow.keras.preprocessing.sequence import pad_sequences
import ast
import nltk
import random
import os
from dotenv import load_dotenv
load_dotenv()
api_key = os.environ.get('RAPIDAPI_KEY')
api_host = os.environ.get('RAPIDAPI_HOST')
# nltk.download('punkt')
# nltk.download('stopwords')
STOPWORDS = set(stopwords.words('english'))
app = Flask(__name__)
CORS(app)
genre_emotion_mapping = {
'action': ['joy', 'surprise'],
'adventure': ['joy', 'surprise'],
'animation': ['joy', 'surprise'],
'comedy': ['joy'],
'crime': ['fear', 'sadness'],
'documentary': ['joy', 'surprise'],
'drama': ['sadness', 'joy'],
'family': ['joy', 'love'],
'fantasy': ['joy', 'surprise'],
'foreign': ['joy', 'surprise'],
'history': ['sadness', 'surprise'],
'horror': ['fear', 'surprise'],
'music': ['joy', 'surprise'],
'mystery': ['surprise', 'fear'],
'romance': ['love', 'joy'],
'science fiction': ['surprise', 'joy'],
'tv movie': ['joy', 'surprise'],
'thriller': ['fear', 'surprise'],
'war': ['fear', 'sadness'],
'western': ['joy', 'surprise']
}
STOPWORDS = set(stopwords.words('english'))
MAX_LEN = 35
emotions_to_labels = {'anger': 0, 'love': 1, 'fear': 2, 'joy': 3, 'sadness': 4,'surprise': 5}
labels_to_emotions = {j:i for i,j in emotions_to_labels.items()}
movies_data = pd.read_csv("data/movies_data_updated_without_specific_line.csv", low_memory=False)
list_of_all_titles = movies_data['title'].tolist()
list_of_all_indices = movies_data.index.tolist()
list_of_all_genres = movies_data['genres'].tolist()
list_of_all_overview = movies_data['overview'].tolist()
selected_features = ['genres', 'overview', 'tagline', 'title', 'imdb_id']
for feature in selected_features:
movies_data[feature] = movies_data[feature].fillna('')
combined_features = movies_data['genres'] + ' ' + movies_data['overview'] + ' ' + movies_data['tagline'] + ' ' + \
movies_data['title'] + ' ' + movies_data['imdb_id']
vectorizer = TfidfVectorizer()
feature_vectors = vectorizer.fit_transform(combined_features)
similarity = cosine_similarity(feature_vectors)
def movie_poster(imdb_id):
url = "https://moviesdatabase.p.rapidapi.com/titles/x/titles-by-ids"
querystring = {"idsList": imdb_id}
headers = {
"X-RapidAPI-Key": api_key,
"X-RapidAPI-Host": api_host
}
response = requests.request("GET", url, headers=headers, params=querystring)
data = json.loads(response.text)
url = data['results'][0]['primaryImage']['url']
return url
def suggest(movie_name, emotion,re_suggest):
listm = []
movie_name = movie_name.lower()
movie_name_tokens = word_tokenize(movie_name)
movie_name_tokens = [word for word in movie_name_tokens if word not in STOPWORDS]
movie_name = ' '.join(movie_name_tokens)
find_close_match = difflib.get_close_matches(movie_name, list_of_all_titles)
if not find_close_match:
emotion_type=emotion
emotion_type = emotion_type.lower()
emotion_type_tokens = word_tokenize(movie_name)
emotion_type_tokens = [word for word in emotion_type_tokens if word not in STOPWORDS]
emotion_type = ' '.join(emotion_type_tokens)
find_close_match = difflib.get_close_matches(emotion_type, list_of_all_overview)
if not find_close_match:
listm = randomrec()
return listm
if find_close_match:
close_match = find_close_match[0]
index_of_the_movie = list_of_all_indices[list_of_all_titles.index(close_match)]
similarity_score = list(enumerate(similarity[index_of_the_movie]))
sorted_similar_movies = sorted(similarity_score, key=lambda x: x[1], reverse=True)
n=0
if re_suggest==1:
n=5
for movie in sorted_similar_movies[n:15]:
index = movie[0]
title_from_index = movies_data.loc[movies_data.index == index, 'title'].values
title_from_index = list(title_from_index)
imdb_id_from_index = movies_data.loc[movies_data.index == index, 'imdb_id'].values
imdb_id_from_index = list(imdb_id_from_index)
genre_from_index = movies_data.loc[movies_data.index == index, 'genres'].values
poster_url = movie_poster(imdb_id_from_index[0])
genres = get_genre_by_emotion(emotion)
genre_list = ast.literal_eval(genre_from_index[0])
genre_list = [genre.lower() for genre in genre_list]
genre_list = [genre for genre in genre_list if genre in genres]
num_genre_matches = len(genre_list)
listm.append({'movie_title': list_of_all_titles[index],
'imdb_id': movies_data.loc[index, 'imdb_id'],
'movie_image': poster_url,
'genres': genre_from_index[0],
'genre_matches': num_genre_matches})
listm = sorted(listm, key=lambda x: x['genre_matches'], reverse=True)
return listm[0:5]
def randomrec():
random_movie_list = []
for _ in range(5):
random_index = random.randint(0, len(list_of_all_titles) - 1)
print(random_index)
imdb_id_from_index = movies_data.loc[movies_data.index == random_index, 'imdb_id'].values
imdb_id_from_index = list(imdb_id_from_index)
poster_url = movie_poster(imdb_id_from_index[0])
random_movie_list.append({'movie_title': list_of_all_titles[random_index],
'imdb_id': movies_data.loc[random_index, 'imdb_id'],
'movie_image': poster_url
})
return random_movie_list
def text_preprocess(text, stop_words=False):
text = re.sub(r'\W+', ' ', text).lower()
tokens = word_tokenize(text)
if stop_words:
tokens = [token for token in tokens if token not in STOPWORDS]
return tokens
def predict_emotion(texts):
# daha önceden eğittiğimiz lstm modelini yüklüyoruz
model = tf.keras.models.load_model('\my_trained_model.h5',compile=True)
with open('\loaded_tokenizer.pkl', 'rb') as handle:
tokenizer = pickle.load(handle)
texts_prepr = [text_preprocess(t) for t in texts]
sequences = tokenizer.texts_to_sequences(texts_prepr)
pad = pad_sequences(sequences, maxlen=MAX_LEN)
predictions = model.predict(pad)
labels = np.argmax(predictions, axis=1)
for i, lbl in enumerate(labels):
print(f'\'{texts[i]}\' --> {labels_to_emotions[lbl]}')
return labels_to_emotions[lbl]
def get_genre_by_emotion(emotion):
genres = []
for genre, emotions in genre_emotion_mapping.items():
if emotion in emotions:
genres.append(genre)
return genres
@app.route('/suggest', methods=['POST'])
def index():
req_data = request.get_json()
movie_name = req_data.get('movie_name')
emotion_text = req_data.get('emotion_text')
re_suggest = req_data.get('re_suggest')
suprise = req_data.get('supriseme')
if suprise ==1:
data = randomrec()
return jsonify(data)
elif suprise ==0:
if emotion_text:
predicted_emotion = predict_emotion([emotion_text])
if movie_name:
data = suggest(movie_name,predicted_emotion,re_suggest)
return jsonify(data)
else:
return jsonify(error='Invalid movie_name'), 400
else:
return jsonify(error='Error'), 400
if __name__ == '__main__':
app.run(debug=True, port=5655)