-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
329 lines (272 loc) · 17.5 KB
/
app.py
File metadata and controls
329 lines (272 loc) · 17.5 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from flask import Flask, request, jsonify, render_template
import numpy as np
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import TfidfVectorizer
import networkx as nx
import matplotlib.pyplot as plt
from typing import List, Dict, Tuple, Optional
import time # Import time for timestamp
# Import the HybridRecommender class from BlazeTrail.py
from BlazeTrail import HybridRecommender
app = Flask(__name__)
# Initialize the recommender system
recommender = HybridRecommender()
# Load the full dataset from BlazeTrail.py
ratings_data = [
# User 1
{"user_id": "user1", "item_id": "movie1", "rating": 5.0},
{"user_id": "user1", "item_id": "movie2", "rating": 4.0},
{"user_id": "user1", "item_id": "movie3", "rating": 2.5},
{"user_id": "user1", "item_id": "movie8", "rating": 4.2},
{"user_id": "user1", "item_id": "movie12", "rating": 3.7},
# User 2
{"user_id": "user2", "item_id": "movie1", "rating": 4.5},
{"user_id": "user2", "item_id": "movie4", "rating": 4.0},
{"user_id": "user2", "item_id": "movie5", "rating": 3.5},
{"user_id": "user2", "item_id": "movie9", "rating": 4.8},
{"user_id": "user2", "item_id": "movie15", "rating": 2.3},
# User 3
{"user_id": "user3", "item_id": "movie2", "rating": 4.0},
{"user_id": "user3", "item_id": "movie4", "rating": 5.0},
{"user_id": "user3", "item_id": "movie6", "rating": 4.5},
{"user_id": "user3", "item_id": "movie10", "rating": 3.9},
{"user_id": "user3", "item_id": "movie14", "rating": 4.2},
# User 4
{"user_id": "user4", "item_id": "movie1", "rating": 3.5},
{"user_id": "user4", "item_id": "movie3", "rating": 4.0},
{"user_id": "user4", "item_id": "movie5", "rating": 3.0},
{"user_id": "user4", "item_id": "movie7", "rating": 4.5},
{"user_id": "user4", "item_id": "movie11", "rating": 5.0},
# User 5
{"user_id": "user5", "item_id": "movie2", "rating": 3.5},
{"user_id": "user5", "item_id": "movie6", "rating": 4.0},
{"user_id": "user5", "item_id": "movie8", "rating": 5.0},
{"user_id": "user5", "item_id": "movie13", "rating": 3.8},
{"user_id": "user5", "item_id": "movie16", "rating": 4.1},
# User 6
{"user_id": "user6", "item_id": "movie3", "rating": 4.2},
{"user_id": "user6", "item_id": "movie7", "rating": 3.9},
{"user_id": "user6", "item_id": "movie12", "rating": 4.7},
{"user_id": "user6", "item_id": "movie17", "rating": 3.5},
{"user_id": "user6", "item_id": "movie21", "rating": 4.0},
# User 7
{"user_id": "user7", "item_id": "movie4", "rating": 3.8},
{"user_id": "user7", "item_id": "movie8", "rating": 4.3},
{"user_id": "user7", "item_id": "movie13", "rating": 3.7},
{"user_id": "user7", "item_id": "movie18", "rating": 4.9},
{"user_id": "user7", "item_id": "movie22", "rating": 3.4},
# User 8
{"user_id": "user8", "item_id": "movie5", "rating": 4.1},
{"user_id": "user8", "item_id": "movie9", "rating": 3.6},
{"user_id": "user8", "item_id": "movie14", "rating": 4.5},
{"user_id": "user8", "item_id": "movie19", "rating": 3.2},
{"user_id": "user8", "item_id": "movie23", "rating": 4.8},
# User 9
{"user_id": "user9", "item_id": "movie6", "rating": 3.9},
{"user_id": "user9", "item_id": "movie10", "rating": 4.4},
{"user_id": "user9", "item_id": "movie15", "rating": 3.3},
{"user_id": "user9", "item_id": "movie20", "rating": 4.7},
{"user_id": "user9", "item_id": "movie24", "rating": 3.5},
# User 10
{"user_id": "user10", "item_id": "movie1", "rating": 4.2},
{"user_id": "user10", "item_id": "movie7", "rating": 3.7},
{"user_id": "user10", "item_id": "movie11", "rating": 4.6},
{"user_id": "user10", "item_id": "movie16", "rating": 3.4},
{"user_id": "user10", "item_id": "movie25", "rating": 4.3},
# Users 11-20 (fewer ratings per user)
{"user_id": "user11", "item_id": "movie2", "rating": 3.8},
{"user_id": "user11", "item_id": "movie12", "rating": 4.1},
{"user_id": "user11", "item_id": "movie22", "rating": 3.6},
{"user_id": "user12", "item_id": "movie3", "rating": 4.3},
{"user_id": "user12", "item_id": "movie13", "rating": 3.2},
{"user_id": "user12", "item_id": "movie23", "rating": 4.5},
{"user_id": "user13", "item_id": "movie4", "rating": 3.9},
{"user_id": "user13", "item_id": "movie14", "rating": 4.7},
{"user_id": "user13", "item_id": "movie24", "rating": 3.4},
{"user_id": "user14", "item_id": "movie5", "rating": 4.4},
{"user_id": "user14", "item_id": "movie15", "rating": 3.1},
{"user_id": "user14", "item_id": "movie25", "rating": 4.6},
{"user_id": "user15", "item_id": "movie6", "rating": 3.7},
{"user_id": "user15", "item_id": "movie16", "rating": 4.2},
{"user_id": "user15", "item_id": "movie26", "rating": 3.8},
{"user_id": "user16", "item_id": "movie7", "rating": 4.5},
{"user_id": "user16", "item_id": "movie17", "rating": 3.3},
{"user_id": "user16", "item_id": "movie27", "rating": 4.1},
{"user_id": "user17", "item_id": "movie8", "rating": 3.6},
{"user_id": "user17", "item_id": "movie18", "rating": 4.8},
{"user_id": "user17", "item_id": "movie28", "rating": 3.5},
{"user_id": "user18", "item_id": "movie9", "rating": 4.0},
{"user_id": "user18", "item_id": "movie19", "rating": 3.9},
{"user_id": "user18", "item_id": "movie29", "rating": 4.3},
{"user_id": "user19", "item_id": "movie10", "rating": 3.4},
{"user_id": "user19", "item_id": "movie20", "rating": 4.6},
{"user_id": "user19", "item_id": "movie30", "rating": 3.7},
{"user_id": "user20", "item_id": "movie1", "rating": 4.2},
{"user_id": "user20", "item_id": "movie11", "rating": 3.5},
{"user_id": "user20", "item_id": "movie21", "rating": 4.4},
]
item_features = [
{"item_id": "movie1", "title": "The Matrix", "features": "sci-fi action cyberpunk dystopian future AI virtual-reality"},
{"item_id": "movie2", "title": "Nanban", "features": "comedy drama friendship college life love"},
{"item_id": "movie3", "title": "The Godfather", "features": "crime drama mafia family power revenge classic"},
{"item_id": "movie4", "title": "Natpe Thunai", "features": "comedy drama sport intresting "},
{"item_id": "movie5", "title": "The Dark Knight", "features": "superhero action crime thriller batman joker"},
{"item_id": "movie6", "title": "Blade Runner 2049", "features": "sci-fi dystopian future AI cyberpunk noir detective"},
{"item_id": "movie7", "title": "leo", "features": "action gangster one person army action thriller crime"},
{"item_id": "movie8", "title": "Avatar", "features": "sci-fi action adventure space aliens nature environment"},
{"item_id": "movie9", "title": "The Shawshank Redemption", "features": "drama prison friendship hope redemption escape"},
{"item_id": "movie10", "title": "The Lord of the Rings", "features": "fantasy adventure epic quest magic elves dwarves"},
{"item_id": "movie11", "title": "Chennai 600028 ", "features": "drama comedy romance sport"},
{"item_id": "movie12", "title": "Forrest Gump", "features": "drama comedy historical love friendship simplicity"},
{"item_id": "movie13", "title": "The Silence of the Lambs", "features": "thriller crime psychological horror serial-killer fbi"},
{"item_id": "movie14", "title": "Star Wars", "features": "sci-fi space opera adventure heroes villains force"},
{"item_id": "movie15", "title": "Jurassic Park", "features": "sci-fi adventure dinosaurs genetic-engineering theme-park"},
{"item_id": "movie16", "title": "Titanic", "features": "romance drama disaster historical ship tragedy"},
{"item_id": "movie17", "title": "The Greatest Of All Time", "features": "action thriller one person army action "},
{"item_id": "movie18", "title": "The Avengers", "features": "superhero action adventure team marvel aliens"},
{"item_id": "movie19", "title": "The Lion King", "features": "animation family drama adventure coming-of-age animals"},
{"item_id": "movie20", "title": "The Departed", "features": "crime thriller drama undercover police mafia identity"},
{"item_id": "movie21", "title": "The Conjuring", "features": "horror mystery suspense mystery thriller supernatural horror "},
{"item_id": "movie22", "title": "The Social Network", "features": "drama biography technology facebook startup betrayal"},
{"item_id": "movie23", "title": "Back to the Future", "features": "sci-fi comedy adventure time-travel 1980s 1950s"},
{"item_id": "movie24", "title": "Amaran", "features": "action epic tragedy action biography drama war army"},
{"item_id": "movie25", "title": "The Revenant", "features": "adventure drama survival wilderness revenge bear"},
{"item_id": "movie26", "title": "Hit The First Case", "features": " police procedural thriller crime drama mystery"},
{"item_id": "movie27", "title": "Maattrraan", "features": "action sci-fi thriller conspiracy thriller"},
{"item_id": "movie28", "title": "Eternal Sunshine of the Spotless Mind", "features": "romance drama sci-fi memory relationships technology"},
{"item_id": "movie29", "title": "Darshana", "features": "upcoming movie scf-fi Horror Mystery "},
{"item_id": "movie30", "title": "La La Land", "features": "musical romance drama jazz hollywood dreams"},
{"item_id": "movie31", "title": "Kaavalan", "features": "slapstick action comedy drama romance"},
{"item_id": "movie32", "title": "chaarulatha", "features": "horror thriller twin sister love"},
{"item_id": "movie33", "title": "Abina sri", "features": "kindfull mystery action love"},
{"item_id": "movie34", "title": "Maharaja", "features": "crime drama one-person army action action thriller "},
{"item_id": "movie35", "title": "Manjummel Boys", "features": "adventure drama thriller friendship"},
{"item_id": "movie36", "title": "Premalu", "features": "comedy romance fun intresting action"},
{"item_id": "movie37", "title": "Vikram", "features": "gun fu one-person army action crime thriller action"},
{"item_id": "movie38", "title": "K.G.F", "features": "action epic gangster period drama crime thriller love "},
{"item_id": "movie39", "title": "Stalin", "features": "hacker kindfull gangster"},
{"item_id": "movie40", "title": "Comali", "features": "comedy memory loss love action drama"},
{"item_id": "movie41", "title": "Miruthan", "features": "zombie horror action adventure sci-fi horror crime"},
{"item_id": "movie42", "title": "Tik Tik Tik", "features": "action adventure sci-fi space travel love"},
{"item_id": "movie43", "title": "Ayalaan", "features": "action adventure sci-fi alien"},
{"item_id": "movie44", "title": "Joe", "features": "drama love action adventure comedy"},
{"item_id": "movie45", "title": "Por Thozhil", "features": "action crime thriller love investigation"},
{"item_id": "movie46", "title": "chithha", "features": "drama thriller love comedy action"},
{"item_id": "movie47", "title": "viduthalai", "features": "action thriller crime drama "},
{"item_id": "movie48", "title": "Teddy", "features": "action love fantasy adventure thriller"},
{"item_id": "movie49", "title": "Sarpatta parambarai", "features": "boxing action drama sport love"},
{"item_id": "movie50", "title": "Pariyerum Perumal", "features": "drama love action adve"},
{"item_id": "movie51", "title": "Soorarai Pottru", "features": "drama hardwork action love thriller "},
{"item_id": "movie52", "title": "Asuran", "features": "drama action period drama one-person army action"},
{"item_id": "movie53", "title": "Raatchasan", "features": "psychological thriller serial killer suspense mystery crime thriller"},
{"item_id": "movie54", "title": "Jai Bhim", "features": "legal drama crime drama"},
{"item_id": "movie55", "title": "Kaithi", "features": "cop drama drug crime action crime thriller "},
{"item_id": "movie56", "title": "12th Fail", "features": " docudrama biography drama "},
{"item_id": "movie57", "title": "3 idiots", "features": "buddy comedy coming-of-age drama quirky comedy"},
{"item_id": "movie58", "title": "Vikram Vedha", "features": "action crime drama thriller"},
{"item_id": "movie59", "title": "Bahubali", "features": "action epic one-person army action drama"},
{"item_id": "movie60", "title": "Friends", "features": "action comedy drama romance"}
]
social_connections = [
{"user_id": "user1", "friend_id": "user2", "weight": 0.8},
{"user_id": "user1", "friend_id": "user4", "weight": 0.6},
{"user_id": "user1", "friend_id": "user10", "weight": 0.7},
{"user_id": "user1", "friend_id": "user15", "weight": 0.4},
{"user_id": "user2", "friend_id": "user3", "weight": 0.9},
{"user_id": "user2", "friend_id": "user5", "weight": 0.5},
{"user_id": "user2", "friend_id": "user7", "weight": 0.6},
{"user_id": "user2", "friend_id": "user12", "weight": 0.7},
{"user_id": "user3", "friend_id": "user5", "weight": 0.7},
{"user_id": "user3", "friend_id": "user8", "weight": 0.8},
{"user_id": "user3", "friend_id": "user13", "weight": 0.6},
{"user_id": "user3", "friend_id": "user16", "weight": 0.5},
{"user_id": "user4", "friend_id": "user5", "weight": 0.6},
{"user_id": "user4", "friend_id": "user9", "weight": 0.7},
{"user_id": "user4", "friend_id": "user14", "weight": 0.5},
{"user_id": "user4", "friend_id": "user17", "weight": 0.4},
{"user_id": "user5", "friend_id": "user6", "weight": 0.8},
{"user_id": "user5", "friend_id": "user10", "weight": 0.7},
{"user_id": "user5", "friend_id": "user15", "weight": 0.6},
{"user_id": "user5", "friend_id": "user18", "weight": 0.5},
{"user_id": "user6", "friend_id": "user7", "weight": 0.7},
{"user_id": "user6", "friend_id": "user11", "weight": 0.6},
{"user_id": "user6", "friend_id": "user16", "weight": 0.5},
{"user_id": "user6", "friend_id": "user19", "weight": 0.4},
{"user_id": "user7", "friend_id": "user8", "weight": 0.8},
{"user_id": "user7", "friend_id": "user12", "weight": 0.7},
{"user_id": "user7", "friend_id": "user17", "weight": 0.6},
{"user_id": "user7", "friend_id": "user20", "weight": 0.5},
{"user_id": "user8", "friend_id": "user9", "weight": 0.7},
{"user_id": "user8", "friend_id": "user13", "weight": 0.6},
{"user_id": "user8", "friend_id": "user18", "weight": 0.5},
{"user_id": "user9", "friend_id": "user10", "weight": 0.8},
{"user_id": "user9", "friend_id": "user14", "weight": 0.7},
{"user_id": "user9", "friend_id": "user19", "weight": 0.6},
{"user_id": "user10", "friend_id": "user15", "weight": 0.7},
{"user_id": "user10", "friend_id": "user20", "weight": 0.6},
{"user_id": "user11", "friend_id": "user12", "weight": 0.8},
{"user_id": "user11", "friend_id": "user16", "weight": 0.7},
{"user_id": "user12", "friend_id": "user13", "weight": 0.7},
{"user_id": "user12", "friend_id": "user17", "weight": 0.6},
{"user_id": "user13", "friend_id": "user14", "weight": 0.8},
{"user_id": "user13", "friend_id": "user18", "weight": 0.7},
{"user_id": "user14", "friend_id": "user15", "weight": 0.7},
{"user_id": "user14", "friend_id": "user19", "weight": 0.6},
{"user_id": "user15", "friend_id": "user16", "weight": 0.8},
{"user_id": "user15", "friend_id": "user20", "weight": 0.7},
{"user_id": "user16", "friend_id": "user17", "weight": 0.7},
{"user_id": "user17", "friend_id": "user18", "weight": 0.8},
{"user_id": "user18", "friend_id": "user19", "weight": 0.7},
{"user_id": "user19", "friend_id": "user20", "weight": 0.8},
]
# Load data into the recommender
recommender.load_ratings_data(ratings_data)
recommender.load_item_features(item_features)
recommender.load_social_graph(social_connections)
@app.route('/')
def home():
return render_template('index.html') # Create an HTML template for the frontend
@app.route('/recommend', methods=['POST'])
def recommend():
"""
API endpoint to get recommendations for a user.
"""
data = request.json
user_id = data.get('user_id')
top_n = data.get('top_n', 5) # Default to 5 recommendations
if not user_id:
return jsonify({"error": "user_id is required"}), 400
recommendations = recommender.get_recommendations(user_id, top_n=top_n)
return jsonify(recommendations)
@app.route('/explain', methods=['POST'])
def explain():
"""
API endpoint to explain a recommendation for a user.
"""
data = request.json
user_id = data.get('user_id')
item_id = data.get('item_id')
if not user_id or not item_id:
return jsonify({"error": "user_id and item_id are required"}), 400
explanation = recommender.explain_recommendation(user_id, item_id)
return jsonify(explanation)
@app.route('/visualize', methods=['POST'])
def visualize():
"""
API endpoint to visualize the social graph for a user.
"""
data = request.json
user_id = data.get('user_id')
if not user_id:
return jsonify({"error": "user_id is required"}), 400
# Save the graph visualization to a file
plt.switch_backend('Agg') # Use a non-interactive backend for Matplotlib
recommender.visualize_graph(highlight_user=user_id)
graph_filename = f"graph_{user_id}_{int(time.time())}.png" # Add a timestamp
graph_path = f"static/{graph_filename}"
plt.savefig(graph_path) # Save the graph as an image
plt.close()
return jsonify({"graph_url": f"/static/{graph_filename}"})
if __name__ == "__main__":
app.run(debug=True)