Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pickle
|
3 |
+
import pandas as pd
|
4 |
+
import requests
|
5 |
+
|
6 |
+
# Function to fetch movie poster from API
|
7 |
+
def fetch_poster(movie_id):
|
8 |
+
response = requests.get(
|
9 |
+
f'https://api.themoviedb.org/3/movie/{movie_id}?api_key=9b955595d7ffef24254513d6a66503fe&language=en-US'
|
10 |
+
)
|
11 |
+
data = response.json()
|
12 |
+
return "http://image.tmdb.org/t/p/w500" + data['poster_path']
|
13 |
+
|
14 |
+
# Function to recommend movies based on selected movie
|
15 |
+
def recommend(movie):
|
16 |
+
movie_index = movies[movies['title'] == movie].index[0]
|
17 |
+
distances = similarity[movie_index]
|
18 |
+
movie_list = sorted(list(enumerate(distances)), reverse=True, key=lambda x: x[1])[1:6]
|
19 |
+
|
20 |
+
recommended_movies = []
|
21 |
+
recommended_posters = []
|
22 |
+
|
23 |
+
for i in movie_list:
|
24 |
+
movie_id = movies.iloc[i[0]].movie_id
|
25 |
+
recommended_movies.append(movies.iloc[i[0]].title)
|
26 |
+
recommended_posters.append(fetch_poster(movie_id))
|
27 |
+
|
28 |
+
return recommended_movies, recommended_posters
|
29 |
+
|
30 |
+
# Load data (movies and similarity matrix)
|
31 |
+
movies_dict = pickle.load(open('movie_dict2.pkl', 'rb'))
|
32 |
+
movies = pd.DataFrame(movies_dict)
|
33 |
+
similarity = pickle.load(open('similarity.pkl', 'rb'))
|
34 |
+
|
35 |
+
# App title
|
36 |
+
st.title('🎬 Movie Recommender System')
|
37 |
+
|
38 |
+
# Movie selection section
|
39 |
+
st.subheader("Select a movie to get recommendations:")
|
40 |
+
selected_movie_name = st.selectbox('Choose a movie:', movies['title'].values)
|
41 |
+
|
42 |
+
# Recommendation button and display
|
43 |
+
if st.button('Recommend'):
|
44 |
+
recommended_names, recommended_posters = recommend(selected_movie_name)
|
45 |
+
|
46 |
+
# Displaying recommendations in a more visually appealing way
|
47 |
+
st.subheader(f"Movies recommended based on '{selected_movie_name}':")
|
48 |
+
cols = st.columns(5) # Dividing the page into 5 columns
|
49 |
+
for idx, col in enumerate(cols):
|
50 |
+
with col:
|
51 |
+
st.text(recommended_names[idx])
|
52 |
+
st.image(recommended_posters[idx])
|
53 |
+
# Adding a clickable link that redirects to Google search for the movie
|
54 |
+
search_url = f"https://www.google.com/search?q={recommended_names[idx].replace(' ', '+')}+movie"
|
55 |
+
st.markdown(f"[Search '{recommended_names[idx]}' on Google]({search_url})", unsafe_allow_html=True)
|