Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import os | |
| import pandas as pd | |
| import requests | |
| from sentence_transformers import SentenceTransformer | |
| from sklearn.metrics.pairwise import cosine_similarity | |
| st.set_page_config(page_title="AI Movie Recommender", layout="wide") | |
| st.title("🎬 AI Movie Recommendation System") | |
| # --------------------------- | |
| # Load movie dataset | |
| # --------------------------- | |
| data = { | |
| "title": [ | |
| "The Dark Knight", | |
| "Batman Begins", | |
| "Interstellar", | |
| "Inception", | |
| "The Matrix", | |
| "John Wick", | |
| "The Notebook", | |
| "Titanic", | |
| "Avengers: Endgame", | |
| "Iron Man" | |
| ], | |
| "description": [ | |
| "Batman fights the Joker to save Gotham City", | |
| "Bruce Wayne becomes Batman to fight crime", | |
| "Astronauts travel through a wormhole to save humanity", | |
| "A thief enters dreams to steal secrets", | |
| "A hacker discovers reality is a simulation", | |
| "An assassin seeks revenge for his dog", | |
| "A romantic love story between two young people", | |
| "A tragic love story on the Titanic ship", | |
| "Superheroes unite to defeat Thanos", | |
| "A billionaire builds a high-tech armored suit" | |
| ] | |
| } | |
| movies = pd.DataFrame(data) | |
| # --------------------------- | |
| # Load pretrained model | |
| # --------------------------- | |
| model = SentenceTransformer("all-MiniLM-L6-v2") | |
| # --------------------------- | |
| # Generate embeddings | |
| # --------------------------- | |
| embeddings = model.encode(movies["description"]) | |
| # --------------------------- | |
| # Similarity matrix | |
| # --------------------------- | |
| similarity_matrix = cosine_similarity(embeddings) | |
| # --------------------------- | |
| # Fetch movie poster | |
| # --------------------------- | |
| def fetch_poster(movie_title): | |
| api_key = os.getenv("My_TMDB_KEY") | |
| if not api_key: | |
| return None | |
| url = f"https://api.themoviedb.org/3/search/movie?api_key={api_key}&query={movie_title}" | |
| try: | |
| response = requests.get(url, timeout=5) | |
| if response.status_code != 200: | |
| return None | |
| data = response.json() | |
| # Safe validation | |
| results = data.get("results", []) | |
| if len(results) == 0: | |
| return None | |
| poster_path = results[0].get("poster_path") | |
| if not poster_path: | |
| return None | |
| return f"https://image.tmdb.org/t/p/w500/{poster_path}" | |
| except Exception as e: | |
| return None | |
| # --------------------------- | |
| # Recommendation function | |
| # --------------------------- | |
| def recommend(movie_title): | |
| idx = movies[movies["title"] == movie_title].index[0] | |
| similarity_scores = list(enumerate(similarity_matrix[idx])) | |
| similarity_scores = sorted(similarity_scores, key=lambda x: x[1], reverse=True) | |
| similarity_scores = similarity_scores[1:6] | |
| movie_indices = [i[0] for i in similarity_scores] | |
| return movies.iloc[movie_indices]["title"].values | |
| # --------------------------- | |
| # Search UI | |
| # --------------------------- | |
| selected_movie = st.selectbox( | |
| "Search a movie", | |
| movies["title"].values | |
| ) | |
| if st.button("Recommend"): | |
| recommended_movies = recommend(selected_movie) | |
| cols = st.columns(5) | |
| for i, movie in enumerate(recommended_movies): | |
| poster = fetch_poster(movie) | |
| with cols[i]: | |
| st.text(movie) | |
| if poster: | |
| st.image(poster) |