import os import threading import time import json import requests import pandas as pd import numpy as np from sklearn.feature_extraction.text import CountVectorizer from sklearn.metrics.pairwise import cosine_similarity from flask import Flask, request, jsonify from flask_cors import CORS import gradio as gr from typing import List, Dict, Any, Optional import html import re from dotenv import load_dotenv # Import load_dotenv from pyngrok import ngrok # --- Ngrok usage example (if needed) --- NGROK_AUTH_TOKEN = os.getenv("Ngrok") if NGROK_AUTH_TOKEN: try: ngrok.set_auth_token(NGROK_AUTH_TOKEN) # Example: ngrok.connect(7860) except ImportError: print("pyngrok not installed; skipping ngrok setup.") # --- MovieRecommendationSystem Class (from Cell C) --- class MovieRecommendationSystem: def __init__(self): self.movies_df = None self.similarity_matrix = None self.vectorizer = CountVectorizer(stop_words='english') # Load API key from environment variable self.API_KEY = os.getenv("OMDB_API_KEY") if not self.API_KEY: print("🚨 WARNING: OMDB_API_KEY not found in environment variables.") self.BASE_URL = "http://www.omdbapi.com/" self.HEADERS = {} def fetch_movie_by_title(self, title): """Fetch a single movie by title from OMDb API.""" if not self.API_KEY: print("OMDb API key is not set. Cannot fetch movie.") return None params = { "apikey": self.API_KEY, "t": title, "plot": "full" } try: response = requests.get(self.BASE_URL, headers=self.HEADERS, params=params, timeout=10) # Added timeout if response.status_code == 200: data = response.json() if data.get("Response") == "True": return data print(f"Error fetching movie '{title}': {response.status_code} or movie not found") return None except requests.exceptions.Timeout: print(f"Timeout fetching movie '{title}'.") return None except Exception as e: print(f"Error fetching movie '{title}': {e}") return None def fetch_movies(self, titles=None, limit=400): """Fetch a list of movies, either from provided titles or a default list.""" if titles is None: titles = [ "Inception", "The Dark Knight", "Interstellar", "The Matrix", "Fight Club", "Pulp Fiction", "Forrest Gump", "The Shawshank Redemption", "Gladiator", "Titanic", "Avatar", "The Avengers", "Jurassic Park", "Star Wars", "The Lord of the Rings", "Harry Potter", "Pirates of the Caribbean", "The Godfather", "Back to the Future", "Indiana Jones", "The Lion King", "Toy Story", "Finding Nemo", "Up", "WALL-E", "The Incredibles", "Coco", "Spider-Man", "Iron Man", "Captain America", "Thor", "Black Panther", "Deadpool", "Logan", "X-Men", "Batman Begins", "The Dark Knight Rises", "Man of Steel", "Wonder Woman", "Aquaman", "Parasite", "Joker", "Once Upon a Time in Hollywood", "Avengers: Endgame", "Toy Story 4", "Spider-Man: Into the Spider-Verse", "Green Book", "Bohemian Rhapsody", "A Star Is Born", "The Irishman", "1917", "Ford v Ferrari", "Little Women", "Marriage Story", "Knives Out", "Us", "Midsommar", "Parasite", "Joker", "Once Upon a Time in Hollywood", "Avengers: Endgame", "Toy Story 4", "Spider-Man: Into the Spider-Verse", "Green Book", "Bohemian Rhapsody", "A Star Is Born", "The Irishman", "1917", "Ford v Ferrari", "Little Women", "Marriage Story", "Knives Out", "Us", "Midsommar", "Get Out", "Dunkirk", "La La Land", "Moonlight", "Arrival", "Hacksaw Ridge", "Hell or High Water", "Manchester by the Sea", "Hidden Figures", "Lion", "Fences", "Deadpool", "Logan", "Arrival", "Hell or High Water", "Manchester by the Sea", "Hidden Figures", "Lion", "Fences", "Zootopia", "Moana", "Sing Street", "The Nice Guys", "Captain America: Civil War", "Doctor Strange", "Fantastic Beasts and Where to Find Them", "Rogue One: A Star Wars Story", "Arrival", "Hacksaw Ridge", "Hell or High Water", "Manchester by the Sea", "Hidden Figures", "Lion", "Fences", "Zootopia", "Moana", "Sing Street", "The Nice Guys", "Captain America: Civil War", "Doctor Strange", "Fantastic Beasts and Where to Find Them", "Rogue One: A Star Wars Story", "Deadpool", "Logan", "Arrival", "Hell or High Water", "Manchester by the Sea", "Hidden Figures", "Lion", "Fences", "Zootopia", "Moana", "Sing Street", "The Nice Guys", "Captain America: Civil War", "Doctor Strange", "Fantastic Beasts and Where to Find Them", "Rogue One: A Star Wars Story", "The Martian", "Mad Max: Fury Road", "Inside Out", "Spotlight", "The Revenant", "Room", "Brooklyn", "Carol", "Sicario", "Straight Outta Compton", "The Big Short", "Bridge of Spies", "Ex Machina", "The Hateful Eight", "Anomalisa", "Son of Saul", "The Lobster", "Amy", "Cartel Land", "Winter on Fire: Ukraine's Fight for Freedom", "What Happened, Miss Simone?", "Listen to Me Marlon", "The Look of Silence", "Shaun the Sheep Movie", "When Marnie Was There", "Boy and the World", "Mustang", "Embrace of the Serpent", "Theeb", "A War", "A Bigger Splash", "Florence Foster Jenkins", "Hail, Caesar!", "Julieta", "Love & Friendship", "Maggie's Plan", "Miles Ahead", "Our Little Sister", "The Lobster", "Amy", "Cartel Land", "Winter on Fire: Ukraine's Fight for Freedom", "What Happened, Miss Simone?", "Listen to Me Marlon", "The Look of Silence", "Shaun the Sheep Movie", "When Marnie Was There", "Boy and the World", "Mustang", "Embrace of the Serpent", "Theeb", "A War", "A Bigger Splash", "Florence Foster Jenkins", "Hail, Caesar!", "Julieta", "Love & Friendship", "Maggie's Plan", "Miles Ahead", "Our Little Sister", "Paterson", "Sing Street", "The Nice Guys", "Captain America: Civil War", "Doctor Strange", "Fantastic Beasts and Where to Find Them", "Rogue One: A Star Wars Story", "The Martian", "Mad Max: Fury Road", "Inside Out", "Spotlight", "The Revenant", "Room", "Brooklyn", "Carol", "Sicario", "Straight Outta Compton", "The Big Short", "Bridge of Spies", "Ex Machina", "The Hateful Eight", "Anomalisa", "Son of Saul", "The Lobster", "Amy", "Cartel Land", "Winter on Fire: Ukraine's Fight for Freedom", "What Happened, Miss Simone?", "Listen to Me Marlon", "The Look of Silence", "Shaun the Sheep Movie", "When Marnie Was There", "Boy and the World", "Mustang", "Embrace of the Serpent", "Theeb", "A War", "A Bigger Splash", "Florence Foster Jenkins", "Hail, Caesar!", "Julieta", "Love & Friendship", "Maggie's Plan", "Miles Ahead", "Our Little Sister", "Paterson", "Sing Street", "The Nice Guys", "Captain America: Civil War", "Doctor Strange", "Fantastic Beasts and Where to Find Them", "Rogue One: A Star Wars Story", "The Martian", "Mad Max: Fury Road", "Inside Out", "Spotlight", "The Revenant", "Room", "Brooklyn", "Carol", "Sicario", "Straight Outta Compton", "The Big Short", "Bridge of Spies", "Ex Machina", "The Hateful Eight", "Anomalisa", "Son of Saul", "The Lobster", "Amy", "Cartel Land", "Winter on Fire: Ukraine's Fight for Freedom", "What Happened, Miss Simone?", "Listen to Me Marlon", "The Look of Silence", "Shaun the Sheep Movie", "When Marnie Was There", "Boy and the World", "Mustang", "Embrace of the Serpent", "Theeb", "A War", "A Bigger Splash", "Florence Foster Jenkins", "Hail, Caesar!", "Julieta", "Love & Friendship", "Maggie's Plan", "Miles Ahead", "Our Little Sister", "Paterson" ][:limit] movies = [] titles_to_fetch = titles[:limit] if limit is not None else titles for title in titles_to_fetch: movie_data = self.fetch_movie_by_title(title) if movie_data: movies.append(movie_data) return movies def prepare_movie_data(self): """Prepare movie data from OMDb API or fallback if API fetch fails.""" movies = self.fetch_movies() if not movies: print("🚨 API returned no movies. Loading fallback dataset.") fallback_movies = [ {'id': 'tt0372784', 'title': 'Batman Begins', 'overview': 'A young Bruce Wayne becomes Batman to fight crime in Gotham.', 'genres': 'Action, Adventure, Crime', 'cast': 'Christian Bale, Michael Caine', 'poster_path': 'https://m.media-amazon.com/images/M/MV5BMjE3NDcyNDExNF5BMl5BanBnXkFtZTcwMDYwNDk0OA@@._V1_SX300.jpg', 'vote_average': 8.2, 'release_date': '2005', 'combined_features': 'Action Adventure Crime Christian Bale Michael Caine A young Bruce Wayne becomes Batman to fight crime in Gotham.'}, {'id': 'tt0468569', 'title': 'The Dark Knight', 'overview': 'Batman faces the Joker, a criminal mastermind.', 'genres': 'Action, Crime, Drama, Thriller', 'cast': 'Christian Bale, Heath Ledger', 'poster_path': 'https://m.media-amazon.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg', 'vote_average': 9.0, 'release_date': '2008', 'combined_features': 'Action Crime Drama Thriller Christian Bale Heath Ledger Batman faces the Joker, a criminal mastermind.'}, {'id': 'tt1345836', 'title': 'The Dark Knight Rises', 'overview': 'Batman returns to save Gotham from Bane.', 'genres': 'Action, Crime, Thriller', 'cast': 'Christian Bale, Tom Hardy', 'poster_path': 'https://m.media-amazon.com/images/M/MV5BMTk4ODQzNDY3Ml5BMl5BanBnXkFtZTcwODA0NTM4Nw@@._V1_SX300.jpg', 'vote_average': 8.4, 'release_date': '2012', 'combined_features': 'Action Crime Thriller Christian Bale Tom Hardy Batman returns to save Gotham from Bane.'}, {'id': 'tt0144084', 'title': 'American Psycho', 'overview': 'A Wall Street banker leads a double life as a serial killer.', 'genres': 'Crime, Drama, Horror', 'cast': 'Christian Bale, Willem Dafoe', 'poster_path': 'https://m.media-amazon.com/images/M/MV5BZTM2ZGJmNzktNzc3My00ZWMzLTg0MjItZjBlMWJiNDE0NjZiXkEyXkFqcGc@._V1_SX300.jpg', 'vote_average': 7.6, 'release_date': '2000', 'combined_features': 'Crime Drama Horror Christian Bale Willem Dafoe A Wall Street banker leads a double life as a serial killer.'}, {'id': 'tt0246578', 'title': 'Donnie Darko', 'overview': 'A troubled teenager is plagued by visions of a man in a rabbit costume.', 'genres': 'Drama, Sci-Fi, Thriller', 'cast': 'Jake Gyllenhaal, Maggie Gyllenhaal', 'poster_path': 'https://m.media-amazon.com/images/M/MV5BZjZlZDlkYTktMmU1My00ZDBiLWE0TAQtNjkzZDFiYTY0ZmMyXkEyXkFqcGc@._V1_SX300.jpg', 'vote_average': 8.0, 'release_date': '2001', 'combined_features': 'Drama Sci-Fi Thriller Jake Gyllenhaal Maggie Gyllenhaal A troubled teenager is plagued by visions of a man in a rabbit costume.'} ] self.movies_df = pd.DataFrame(fallback_movies) else: print(f"✅ Successfully fetched {len(movies)} movies from OMDb API.") movie_data = [] for movie in movies: movie_info = { 'id': movie.get('imdbID', movie.get('Title', 'unknown')), 'title': movie.get('Title', ''), 'overview': movie.get('Plot', ''), 'genres': movie.get('Genre', ''), 'cast': movie.get('Actors', ''), 'poster_path': movie.get('Poster', ''), 'vote_average': float(movie.get('imdbRating', '0')) if movie.get('imdbRating') not in ['N/A', None] else 0, 'release_date': movie.get('Year', ''), 'combined_features': f"{movie.get('Genre', '')} {movie.get('Actors', '')} {movie.get('Plot', '')}" } movie_data.append(movie_info) self.movies_df = pd.DataFrame(movie_data) self.build_similarity_matrix() return self.movies_df def build_similarity_matrix(self): """Build similarity matrix for recommendations based on combined features.""" if self.movies_df is not None and not self.movies_df.empty: max_features = 5000 self.vectorizer = CountVectorizer(stop_words='english', max_features=max_features) corpus = self.movies_df['combined_features'].fillna('').tolist() vectorized_features = self.vectorizer.fit_transform(corpus) self.similarity_matrix = cosine_similarity(vectorized_features) print(f"✅ Similarity matrix built with shape: {self.similarity_matrix.shape}") else: print("🚨 Cannot build similarity matrix: movies_df is empty.") def get_recommendations(self, selected_movie_ids, num_recommendations=5): """Get movie recommendations based on selected movie IDs.""" if self.similarity_matrix is None or self.movies_df.empty: print("Debug: Similarity matrix or movies_df is empty.") return [] selected_indices = self.movies_df[self.movies_df['id'].isin(selected_movie_ids)].index.tolist() if not selected_indices: print("Debug: No selected movies found in DataFrame for recommendations.") return [] avg_similarity_scores = np.mean(self.similarity_matrix[selected_indices], axis=0) movie_indices = np.argsort(avg_similarity_scores)[::-1] recommendations = [] for idx in movie_indices: movie = self.movies_df.iloc[idx] # Ensure the recommended movie is not one of the selected movies if movie['id'] not in selected_movie_ids: recommendations.append(movie.to_dict()) if len(recommendations) >= num_recommendations: break return recommendations # Initialize the recommender globally recommender = MovieRecommendationSystem() # --- Flask Application (from Cell D, modified) --- app = Flask(__name__) CORS(app) # Enable CORS @app.route('/') def index(): """Health check endpoint""" return jsonify({ "message": "Netflix Clone API is running!", "status": "success", "endpoints": ["/api/movies", "/api/recommend"] }) @app.route('/api/movies') def get_movies(): """Get all movies for display""" try: if recommender.movies_df is None or recommender.movies_df.empty: print("Preparing movie data...") recommender.prepare_movie_data() print(f"Loaded {len(recommender.movies_df)} movies") movies = recommender.movies_df.to_dict('records') return jsonify(movies) except Exception as e: print(f"Error in get_movies: {e}") return jsonify({'error': 'Failed to fetch movies'}), 500 @app.route('/api/recommend', methods=['POST']) def recommend_movies(): """Get recommendations based on selected movies""" try: data = request.json selected_movie_ids = data.get('selected_movies', []) if len(selected_movie_ids) < 5: return jsonify({'error': 'Please select at least 5 movies'}), 400 print(f"Getting recommendations for movies: {selected_movie_ids}") recommendations = recommender.get_recommendations(selected_movie_ids) return jsonify(recommendations) except Exception as e: print(f"Error in recommend_movies: {e}") return jsonify({'error': 'Failed to get recommendations'}), 500 @app.route('/api/health') def health_check(): """Health check endpoint""" return jsonify({ "status": "healthy", "movies_loaded": len(recommender.movies_df) if recommender.movies_df is not None else 0, "similarity_matrix_built": recommender.similarity_matrix is not None }) # Function to start Flask server (from Cell E, modified) def start_flask_server(): """Start Flask server in background""" try: print("🚀 Starting Flask backend server...") # Run Flask app on port 5000, accessible locally within the Space app.run(host='127.0.0.1', port=5000, debug=False) except Exception as e: print(f"❌ Error starting Flask server: {e}") # --- Gradio Application (from Cell 7P4A_qIhjvbT, modified) --- # API_BASE_URL now points to the Flask app running locally within the Space API_BASE_URL = "http://127.0.0.1:5000" MAX_SELECTIONS = 10 MIN_RECOMMENDATIONS = 5 class CinemaCloneApp: def __init__(self): self.selected_movies = [] self.all_movies = [] self.recommendations = [] def sanitize_input(self, text: str) -> str: """Sanitize user input to prevent XSS attacks""" if not isinstance(text, str): return "" text = re.sub(r'<[^>]*>', '', text) text = html.escape(text) return text.strip() def validate_movie_data(self, movie: Dict[str, Any]) -> bool: """Validate movie data structure""" required_fields = ['id', 'title'] return all(field in movie and movie[field] for field in required_fields) def fetch_movies_from_backend(self) -> List[Dict[str, Any]]: """Fetch movies from the Flask backend with comprehensive error handling""" try: response = requests.get( f"{API_BASE_URL}/api/movies", timeout=60, # Increased timeout headers={'Accept': 'application/json'} ) if response.status_code == 200: content_type = response.headers.get('content-type', '') if 'application/json' not in content_type: # Attempt to read text response for debugging non-JSON errors print(f"Warning: Received non-JSON response (status {response.status_code}). Content: {response.text[:500]}...") # Print first 500 chars raise ValueError(f"Backend returned non-JSON response (Status: {response.status_code})") movies = response.json() if isinstance(movies, list) and len(movies) > 0: validated_movies = [] for movie in movies: if self.validate_movie_data(movie): movie['title'] = self.sanitize_input(movie.get('title', '')) movie['overview'] = self.sanitize_input(movie.get('overview', '')) movie['genres'] = self.sanitize_input(movie.get('genres', '')) movie['cast'] = self.sanitize_input(movie.get('cast', '')) # Sanitize cast as well validated_movies.append(movie) self.all_movies = validated_movies print(f"Successfully fetched and validated {len(validated_movies)} movies from backend.") return validated_movies elif isinstance(movies, list) and len(movies) == 0: print("Backend returned an empty movie list.") return self.get_fallback_movies() else: print(f"Backend returned unexpected data format: {movies}") raise ValueError("Invalid movie data structure from backend") else: try: error_response = response.json() print(f"Backend error response (Status {response.status_code}): {error_response}") except json.JSONDecodeError: print(f"Backend non-JSON error response (Status {response.status_code}) from recommendations endpoint: {response.text[:500]}...") raise requests.RequestException(f"Backend request failed with status: {response.status_code}") except requests.exceptions.Timeout: print(f"Timeout fetching movies from backend at {API_BASE_URL}/api/movies") return self.get_fallback_movies() except requests.exceptions.ConnectionError as ce: print(f"Connection error fetching movies from backend at {API_BASE_URL}/api/movies: {ce}") print("Ensure the Flask backend is running and accessible at http://127.0.0.1:5000.") return self.get_fallback_movies() except Exception as e: print(f"An unexpected error occurred fetching movies from backend: {e}") return self.get_fallback_movies() def get_recommendations_from_backend(self, selected_ids: List[str]) -> List[Dict[str, Any]]: """Get recommendations from Flask backend with security validation""" try: if not selected_ids or not isinstance(selected_ids, list): raise ValueError("Invalid selected movie IDs") sanitized_ids = [self.sanitize_input(str(id_)) for id_ in selected_ids if id_] response = requests.post( f"{API_BASE_URL}/api/recommend", json={"selected_movies": sanitized_ids}, headers={'Content-Type': 'application/json', 'Accept': 'application/json'}, timeout=30 ) if response.status_code == 200: content_type = response.headers.get('content-type', '') if 'application/json' not in content_type: print(f"Warning: Received non-JSON response (status {response.status_code}) from recommendations endpoint. Content: {response.text[:500]}...") raise ValueError(f"Backend returned non-JSON response (Status: {response.status_code}) from recommendations endpoint") recommendations = response.json() if isinstance(recommendations, list): validated_recs = [] for rec in recommendations: if self.validate_movie_data(rec): rec['title'] = self.sanitize_input(rec.get('title', '')) rec['overview'] = self.sanitize_input(rec.get('overview', '')) rec['genres'] = self.sanitize_input(rec.get('genres', '')) rec['cast'] = self.sanitize_input(rec.get('cast', '')) # Sanitize cast as well validated_recs.append(rec) print(f"Successfully received and validated {len(validated_recs)} recommendations.") return validated_recs else: print(f"Backend returned unexpected data format for recommendations: {recommendations}") raise ValueError("Invalid recommendations data structure from backend") else: try: error_response = response.json() print(f"Backend error response (Status {response.status_code}) from recommendations endpoint: {error_response}") except json.JSONDecodeError: print(f"Backend non-JSON error response (Status {response.status_code}) from recommendations endpoint: {response.text[:500]}...") raise requests.RequestException(f"Backend recommendation request failed with status: {response.status_code}") except requests.exceptions.Timeout: print(f"Timeout getting recommendations from backend at {API_BASE_URL}/api/recommend") return [] except requests.exceptions.ConnectionError as ce: print(f"Connection error getting recommendations from backend at {API_BASE_URL}/api/recommend: {ce}") print("Ensure the Flask backend is running and accessible at http://127.0.0.1:5000.") return [] except Exception as e: print(f"An unexpected error occurred getting recommendations: {e}") return [] def create_movie_card_html(self, movie: Dict[str, Any], is_selected: bool = False, is_recommendation: bool = False) -> str: """Create HTML for a movie card with React-inspired styling and animations""" # Ensure all fields are present with default empty strings to avoid KeyError title = html.escape(movie.get('title', 'Unknown')) overview = html.escape(movie.get('overview', '')[:200] + "..." if len(movie.get('overview', '')) > 200 else movie.get('overview', '')) genres = html.escape(movie.get('genres', '')) cast = html.escape(movie.get('cast', '')[:150] + "..." if len(movie.get('cast', '')) > 150 else movie.get('cast', '')) rating = float(movie.get('vote_average', 0)) year = html.escape(str(movie.get('release_date', ''))) movie_id = html.escape(str(movie.get('id', ''))) # Ensure ID is also sanitized and present poster_url = movie.get('poster_path', '') if not poster_url or not poster_url.startswith(('http://', 'https://')): poster_url = 'https://via.placeholder.com/300x450/1a1a1a/fff?text=No+Image' selected_class = "selected" if is_selected else "" rec_class = "recommendation" if is_recommendation else "" selection_indicator = "✓" if is_selected else "+" genre_list = genres.split(', ') if genres else [] genre_tags_html = "" for genre in genre_list[:3]: genre_tags_html += f'{html.escape(genre.strip())}' # Sanitize genre tags # Use data-movie-id for JavaScript interaction return f"""
{title}
{selection_indicator}

{title}

{rating:.1f}
{year}
{genre_tags_html}
Cast: {cast}
{overview}
""" def create_movies_grid_html(self, movies: List[Dict[str, Any]], is_recommendation: bool = False) -> str: """Create HTML grid of movie cards with React-inspired layout""" if not movies: return f"""
🎬

No {'recommendations' if is_recommendation else 'movies'} available

{'Select more movies to get better recommendations' if is_recommendation else 'Click Load Movies to start exploring'}

""" cards_html = "" for movie in movies[:500]: # Limit for performance, can be adjusted # Find the full movie object from self.all_movies to check selection status full_movie_data = next((item for item in self.all_movies if item.get('id') == movie.get('id')), None) is_selected = full_movie_data and full_movie_data.get('id') in self.selected_movies cards_html += self.create_movie_card_html(movie, is_selected, is_recommendation) grid_class = "recommendations-grid" if is_recommendation else "movies-grid" return f"""
{cards_html}
""" # Initialize the app instance globally gradio_app_instance = CinemaCloneApp() # Enhanced CSS with React-inspired styling and animations cinema_css = """ """ def load_movies(): """Load movies from backend with enhanced UI feedback""" try: movies = gradio_app_instance.fetch_movies_from_backend() movies_html = gradio_app_instance.create_movies_grid_html(movies, is_recommendation=False) status = f"
✨ Successfully loaded {len(movies)} amazing movies!
" movie_choices = ["🎬 Select a movie"] + [movie['title'] for movie in movies if movie.get('title')] # Clear selected movies on load gradio_app_instance.selected_movies = [] selection_counter_html = f"
Selected: {len(gradio_app_instance.selected_movies)}/{MAX_SELECTIONS}
" return movies_html, status_display, gr.update(visible=False), gr.update(choices=movie_choices, value="🎬 Select a movie"), "", selection_counter_html except Exception as e: error_msg = f"
❌ Oops! Failed to load movies: {str(e)}
" return "
Failed to load movies. Please try again.
", error_msg, gr.update(visible=False), gr.update(choices=["🎬 Select a movie"], value="🎬 Select a movie"), "", "
Selected: 0/10
" def toggle_movie_selection(movie_title: str): """Toggle movie selection with enhanced feedback""" movie_title = gradio_app_instance.sanitize_input(movie_title) if not movie_title or movie_title == "🎬 Select a movie": return gr.update(), "
Please select a movie first! 🎬
", gr.update(visible=False), f"
Selected: {len(gradio_app_instance.selected_movies)}/{MAX_SELECTIONS}
" selected_movie = None for movie in gradio_app_instance.all_movies: if movie.get('title') == movie_title: selected_movie = movie break if not selected_movie: return gr.update(), "
❌ Movie not found in our collection
", gr.update(visible=False), f"
Selected: {len(gradio_app_instance.selected_movies)}/{MAX_SELECTIONS}
" movie_id = selected_movie['id'] if movie_id in gradio_app_instance.selected_movies: gradio_app_instance.selected_movies.remove(movie_id) action = "removed from" emoji = "➖" else: if len(gradio_app_instance.selected_movies) >= MAX_SELECTIONS: return gr.update(), f"
🚫 Maximum {MAX_SELECTIONS} movies can be selected
", gr.update(visible=False), f"
Selected: {len(gradio_app_instance.selected_movies)}/{MAX_SELECTIONS}
" gradio_app_instance.selected_movies.append(movie_id) action = "added to" emoji = "➕" # Re-render the movies grid to update selection indicators movies_html = gradio_app_instance.create_movies_grid_html(gradio_app_instance.all_movies, is_recommendation=False) status = f"
{emoji} '{movie_title}' {action} your collection
" selection_counter_html = f"
Selected: {len(gradio_app_instance.selected_movies)}/{MAX_SELECTIONS}
" show_rec_btn = len(gradio_app_instance.selected_movies) >= MIN_RECOMMENDATIONS return movies_html, status_display, gr.update(visible=show_rec_btn), selection_counter_html def get_recommendations(): """Get movie recommendations with beautiful presentation""" if len(gradio_app_instance.selected_movies) < MIN_RECOMMENDATIONS: return gr.update(), f"
🎯 Please select at least {MIN_RECOMMENDATIONS} movies to get personalized recommendations
", gr.update(visible=False) try: recommendations = gradio_app_instance.get_recommendations_from_backend(gradio_app_instance.selected_movies) if not recommendations: return gr.update(), "
🤔 No recommendations found. Try selecting different movies!
", gr.update(visible=False) rec_html = f"""
🎯 Curated Just For You
{gradio_app_instance.create_movies_grid_html(recommendations, is_recommendation=True)}
""" status = f"
🌟 Found {len(recommendations)} perfect recommendations based on your {len(gradio_app_instance.selected_movies)} selections!
" return rec_html, status_display, gr.update(visible=True) except Exception as e: error_msg = f"
❌ Error getting recommendations: {str(e)}
" return gr.update(), error_msg, gr.update(visible=False) def clear_selections(): """Clear all selections with confirmation""" gradio_app_instance.selected_movies.clear() # Re-render the movies grid to clear selection indicators movies_html = gradio_app_instance.create_movies_grid_html(gradio_app_instance.all_movies, is_recommendation=False) selection_counter_html = f"
Selected: {len(gradio_app_instance.selected_movies)}/{MAX_SELECTIONS}
" return movies_html, "
🔄 All selections cleared! Start fresh with new choices
", gr.update(visible=False), gr.update(visible=False), gr.update(value="🎬 Select a movie"), "", selection_counter_html def search_movies(query: str): """Search movies by title and update the grid""" query = gradio_app_instance.sanitize_input(query).lower() if not query: # If search query is empty, display all movies return gradio_app_instance.create_movies_grid_html(gradio_app_instance.all_movies, is_recommendation=False) else: # Filter movies based on the query filtered_movies = [ movie for movie in gradio_app_instance.all_movies if query in movie.get('title', '').lower() ] return gradio_app_instance.create_movies_grid_html(filtered_movies, is_recommendation=False) # Create the stunning Gradio interface with gr.Blocks(css=cinema_css, title="CinemaAI - Movie Recommendations", theme=gr.themes.Default()) as demo: gr.HTML("""
🎬 CINEMA AI

Discover Your Next Cinematic Adventure

Select your favorite movies and let our AI curate personalized recommendations just for you

""") with gr.Row(): with gr.Column(scale=3): load_btn = gr.Button("🎬 Load Movie Collection", variant="primary", size="lg") with gr.Column(scale=2): clear_btn = gr.Button("🔄 Clear All Selections", variant="secondary") selection_counter_display = gr.HTML("
Selected: 0/10
") status_display = gr.HTML("
🎭 Click 'Load Movie Collection' to begin your journey
") with gr.Row(): movie_dropdown = gr.Dropdown( choices=["🎬 Select a movie"], label="🎯 Choose Your Favorite Movie", value="🎬 Select a movie", interactive=True ) select_btn = gr.Button("✨ Add/Remove Selection", variant="secondary") search_bar = gr.Textbox(label="🔍 Search for a movie by title", placeholder="e.g., Inception", interactive=True) movies_display = gr.HTML("
🎬

Your Movie Collection Awaits

Load movies to start exploring amazing cinema

") rec_btn = gr.Button("🎯 Get My Personal Recommendations", variant="primary", size="lg", visible=False) recommendations_display = gr.HTML("", visible=False) # Event handlers load_btn.click( fn=load_movies, outputs=[movies_display, status_display, recommendations_display, movie_dropdown, search_bar, selection_counter_display] ) select_btn.click( fn=toggle_movie_selection, inputs=[movie_dropdown], outputs=[movies_display, status_display, rec_btn, selection_counter_display] ) rec_btn.click( fn=get_recommendations, outputs=[recommendations_display, status_display, recommendations_display] ) clear_btn.click( fn=clear_selections, outputs=[movies_display, status_display, rec_btn, recommendations_display, movie_dropdown, search_bar, selection_counter_display] ) search_bar.change( fn=search_movies, inputs=[search_bar], outputs=[movies_display] ) # --- Main execution block --- if __name__ == "__main__": load_dotenv() # Load environment variables from .env file # Start Flask server in a separate thread flask_thread = threading.Thread(target=start_flask_server) flask_thread.daemon = True flask_thread.start() # Give Flask a moment to start time.sleep(5) # Increased sleep time # Launch Gradio interface demo.launch()