Movie-Me-Now/app.py

274 lines
11 KiB
Python
Raw Normal View History

2025-04-02 18:49:04 +00:00
from flask import Flask, request, render_template, redirect, url_for, session
import json
import numpy as np
import random
2025-04-02 19:26:37 +00:00
import math
2025-04-02 18:49:04 +00:00
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
app = Flask(__name__)
app.secret_key = 'your_secret_key_here' # Replace with a secure key in production
# Load movies from top_movies.json with UTF-8 encoding
with open('top_movies.json', 'r', encoding='utf-8') as f:
movies = json.load(f)
2025-04-02 19:26:37 +00:00
# Preprocess each movie
2025-04-02 18:49:04 +00:00
for i, movie in enumerate(movies):
2025-04-02 19:26:37 +00:00
movie['id'] = i # Unique ID
# Combine genres and tags into one feature string.
2025-04-02 18:49:04 +00:00
movie['features'] = ' '.join(movie.get('genres', [])) + ' ' + ' '.join(movie.get('tags', []))
2025-04-02 19:26:37 +00:00
# Ensure numeric values for year and runtime:
2025-04-02 18:49:04 +00:00
try:
movie['year_num'] = int(movie.get('year', '0'))
except:
movie['year_num'] = 0
try:
movie['runtime_num'] = float(movie.get('runtime')) if movie.get('runtime') else 0
except:
movie['runtime_num'] = 0
2025-04-02 19:26:37 +00:00
# Ensure vote_count is numeric.
try:
count = movie.get('vote_count', 0)
if isinstance(count, str):
count = count.replace(',', '')
if 'M' in count:
count = float(count.replace('M', '')) * 1e6
else:
count = int(count)
movie['vote_count'] = int(count)
except:
movie['vote_count'] = 0
2025-04-02 18:49:04 +00:00
# Build the TFIDF vectorizer on movie features.
vectorizer = TfidfVectorizer(stop_words='english')
movie_features = [movie['features'] for movie in movies]
movie_vectors = vectorizer.fit_transform(movie_features)
2025-04-02 19:26:37 +00:00
# Precompute overall ranges for numeric features.
2025-04-02 18:49:04 +00:00
years = [m['year_num'] for m in movies if m['year_num'] > 0]
runtimes = [m['runtime_num'] for m in movies if m['runtime_num'] > 0]
2025-04-02 19:26:37 +00:00
max_vote = max([m['vote_count'] for m in movies]) if movies else 1
2025-04-02 18:49:04 +00:00
min_year, max_year = (min(years), max(years)) if years else (0, 1)
min_runtime, max_runtime = (min(runtimes), max(runtimes)) if runtimes else (0, 1)
year_range = max_year - min_year if max_year != min_year else 1
runtime_range = max_runtime - min_runtime if max_runtime != min_runtime else 1
2025-04-02 19:26:37 +00:00
rating_range = 10.0 # Assuming ratings are on a 010 scale
2025-04-02 18:49:04 +00:00
2025-04-02 19:26:37 +00:00
def get_predicted_movies(num=10):
2025-04-02 18:49:04 +00:00
"""
2025-04-02 19:26:37 +00:00
Return up to `num` movies that haven't been shown yet.
Uses the user's past ratings to predict which unseen movies they might like.
If no ratings exist, falls back to random selection.
2025-04-02 18:49:04 +00:00
"""
asked = session.get('asked_movies', [])
available = [m for m in movies if m['id'] not in asked]
if not available:
return []
2025-04-02 19:26:37 +00:00
rated = session.get('rated_movies', {})
# Fallback to random selection if there are no like/dislike ratings.
if not rated or not any(r in ['like', 'dislike'] for r in rated.values()):
random.shuffle(available)
return available[:num]
# Build prediction profiles.
liked_ids = [int(mid) for mid, rating in rated.items() if rating == 'like']
disliked_ids = [int(mid) for mid, rating in rated.items() if rating == 'dislike']
if liked_ids:
liked_profile = np.asarray(movie_vectors[liked_ids].mean(axis=0))
else:
liked_profile = np.zeros((1, movie_vectors.shape[1]))
if disliked_ids:
disliked_profile = np.asarray(movie_vectors[disliked_ids].mean(axis=0))
else:
disliked_profile = np.zeros((1, movie_vectors.shape[1]))
# Compute numeric averages for liked movies.
liked_years = [movies[i]['year_num'] for i in liked_ids if movies[i]['year_num'] > 0]
liked_runtimes = [movies[i]['runtime_num'] for i in liked_ids if movies[i]['runtime_num'] > 0]
liked_ratings = [movies[i].get('imdb_rating', 0) for i in liked_ids if movies[i].get('imdb_rating', 0)]
avg_year = np.mean(liked_years) if liked_years else None
avg_runtime = np.mean(liked_runtimes) if liked_runtimes else None
avg_rating = np.mean(liked_ratings) if liked_ratings else None
predictions = []
# Tunable weights.
w_text = 0.5
w_year = 0.1
w_runtime = 0.1
w_rating = 0.15
w_popularity = 0.15
for movie in available:
i = movie['id']
# TEXT SIMILARITY.
movie_vector = movie_vectors[i].toarray()
like_sim = cosine_similarity(movie_vector, liked_profile)[0][0] if np.linalg.norm(liked_profile) != 0 else 0
dislike_sim = cosine_similarity(movie_vector, disliked_profile)[0][0] if np.linalg.norm(disliked_profile) != 0 else 0
text_score = like_sim - dislike_sim
# YEAR SIMILARITY.
year_score = 0
if avg_year is not None and movie['year_num'] > 0:
diff_year = abs(movie['year_num'] - avg_year)
year_score = 1 - (diff_year / year_range)
# RUNTIME SIMILARITY.
runtime_score = 0
if avg_runtime is not None and movie['runtime_num'] > 0:
diff_runtime = abs(movie['runtime_num'] - avg_runtime)
runtime_score = 1 - (diff_runtime / runtime_range)
# RATING SIMILARITY.
rating_score = 0
movie_rating = movie.get('imdb_rating', 0)
if avg_rating is not None and movie_rating:
diff_rating = abs(movie_rating - avg_rating)
rating_score = 1 - (diff_rating / rating_range)
# POPULARITY SCORE.
popularity_score = 0
if movie['vote_count'] > 0:
popularity_score = math.log(movie['vote_count'] + 1) / math.log(max_vote + 1)
# Final prediction score.
final_score = (w_text * text_score +
w_year * year_score +
w_runtime * runtime_score +
w_rating * rating_score +
w_popularity * popularity_score)
predictions.append((movie, final_score))
predictions.sort(key=lambda x: x[1], reverse=True)
return [pred[0] for pred in predictions[:num]]
2025-04-02 18:49:04 +00:00
def enough_info():
"""
2025-04-02 19:26:37 +00:00
Check if the user has rated at least 3 movies (like/dislike).
2025-04-02 18:49:04 +00:00
"""
rated = session.get('rated_movies', {})
count = sum(1 for rating in rated.values() if rating in ['like', 'dislike'])
return count >= 3
@app.route('/')
def home():
session.setdefault('rated_movies', {}) # {movie_id: rating}
2025-04-02 19:26:37 +00:00
session.setdefault('asked_movies', []) # list of movie IDs already shown
2025-04-02 18:49:04 +00:00
return redirect(url_for('questionnaire'))
@app.route('/questionnaire', methods=['GET', 'POST'])
def questionnaire():
if request.method == 'POST':
current_ids = request.form.getlist("movie_id")
for movie_id in current_ids:
rating = request.form.get(f"rating_{movie_id}")
session['rated_movies'][movie_id] = rating
if int(movie_id) not in session['asked_movies']:
session['asked_movies'].append(int(movie_id))
remaining = [m for m in movies if m['id'] not in session['asked_movies']]
if enough_info() or not remaining:
return redirect(url_for('recommend'))
else:
return redirect(url_for('questionnaire'))
else:
2025-04-02 19:26:37 +00:00
# Use prediction to select movies for the questionnaire.
selected_movies = get_predicted_movies(num=10)
2025-04-02 18:49:04 +00:00
if not selected_movies:
return redirect(url_for('recommend'))
return render_template('questionnaire.html', movies=selected_movies)
def advanced_recommendations():
"""
2025-04-02 19:26:37 +00:00
Compute an advanced hybrid recommendation score on unseen movies.
Only movies not already shown (asked) are considered.
2025-04-02 18:49:04 +00:00
Combines:
2025-04-02 19:26:37 +00:00
1. Text similarity (TFIDF) between liked/disliked profiles.
2. Year similarity.
3. Runtime similarity.
4. Rating similarity.
5. Popularity (log-scaled vote count).
Returns the top 20 recommendations.
2025-04-02 18:49:04 +00:00
"""
rated = session.get('rated_movies', {})
2025-04-02 19:26:37 +00:00
asked = set(session.get('asked_movies', []))
# Only consider movies that haven't been shown to the user.
available = [m for m in movies if m['id'] not in asked]
if not available:
available = movies # Fallback if all movies have been shown.
2025-04-02 18:49:04 +00:00
liked_ids = [int(mid) for mid, rating in rated.items() if rating == 'like']
disliked_ids = [int(mid) for mid, rating in rated.items() if rating == 'dislike']
2025-04-02 19:26:37 +00:00
2025-04-02 18:49:04 +00:00
if liked_ids:
liked_profile = np.asarray(movie_vectors[liked_ids].mean(axis=0))
else:
liked_profile = np.zeros((1, movie_vectors.shape[1]))
if disliked_ids:
disliked_profile = np.asarray(movie_vectors[disliked_ids].mean(axis=0))
else:
disliked_profile = np.zeros((1, movie_vectors.shape[1]))
2025-04-02 19:26:37 +00:00
2025-04-02 18:49:04 +00:00
liked_years = [movies[i]['year_num'] for i in liked_ids if movies[i]['year_num'] > 0]
liked_runtimes = [movies[i]['runtime_num'] for i in liked_ids if movies[i]['runtime_num'] > 0]
2025-04-02 19:26:37 +00:00
liked_ratings = [movies[i].get('imdb_rating', 0) for i in liked_ids if movies[i].get('imdb_rating', 0)]
2025-04-02 18:49:04 +00:00
avg_year = np.mean(liked_years) if liked_years else None
avg_runtime = np.mean(liked_runtimes) if liked_runtimes else None
2025-04-02 19:26:37 +00:00
avg_rating = np.mean(liked_ratings) if liked_ratings else None
2025-04-02 18:49:04 +00:00
recommendations = []
2025-04-02 19:26:37 +00:00
w_text = 0.5
w_year = 0.1
w_runtime = 0.1
w_rating = 0.15
w_popularity = 0.15
for movie in available:
i = movie['id']
2025-04-02 18:49:04 +00:00
movie_vector = movie_vectors[i].toarray()
like_sim = cosine_similarity(movie_vector, liked_profile)[0][0] if np.linalg.norm(liked_profile) != 0 else 0
dislike_sim = cosine_similarity(movie_vector, disliked_profile)[0][0] if np.linalg.norm(disliked_profile) != 0 else 0
text_score = like_sim - dislike_sim
2025-04-02 19:26:37 +00:00
2025-04-02 18:49:04 +00:00
year_score = 0
if avg_year is not None and movie['year_num'] > 0:
diff_year = abs(movie['year_num'] - avg_year)
2025-04-02 19:26:37 +00:00
year_score = 1 - (diff_year / year_range)
2025-04-02 18:49:04 +00:00
runtime_score = 0
if avg_runtime is not None and movie['runtime_num'] > 0:
diff_runtime = abs(movie['runtime_num'] - avg_runtime)
runtime_score = 1 - (diff_runtime / runtime_range)
2025-04-02 19:26:37 +00:00
rating_score = 0
movie_rating = movie.get('imdb_rating', 0)
if avg_rating is not None and movie_rating:
diff_rating = abs(movie_rating - avg_rating)
rating_score = 1 - (diff_rating / rating_range)
popularity_score = 0
if movie['vote_count'] > 0:
popularity_score = math.log(movie['vote_count'] + 1) / math.log(max_vote + 1)
final_score = (w_text * text_score +
w_year * year_score +
w_runtime * runtime_score +
w_rating * rating_score +
w_popularity * popularity_score)
2025-04-02 18:49:04 +00:00
recommendations.append((movie, final_score))
2025-04-02 19:26:37 +00:00
2025-04-02 18:49:04 +00:00
recommendations.sort(key=lambda x: x[1], reverse=True)
2025-04-02 19:26:37 +00:00
return recommendations[:20]
2025-04-02 18:49:04 +00:00
@app.route('/recommend')
def recommend():
recommendations = advanced_recommendations()
return render_template('recommendations.html', recommendations=recommendations)
if __name__ == '__main__':
app.run(debug=True)
2025-04-02 19:26:37 +00:00