-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
150 lines (130 loc) · 4.81 KB
/
Copy pathapp.py
File metadata and controls
150 lines (130 loc) · 4.81 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
import streamlit as st
import pickle
import pandas as pd
import requests # To make API calls
from thefuzz import process
# --- PAGE CONFIGURATION ---
st.set_page_config(
page_title="Movie Recommender",
page_icon="🎬",
layout="wide",
initial_sidebar_state="expanded",
)
# --- CUSTOM STYLING ---
st.markdown("""
<style>
/* Main background color */
.stApp {
background-color: #1a1a1a;
color: #ffffff;
}
/* Title style */
h1 {
color: #e63946; /* A vibrant red */
}
/* Selectbox styling */
.stSelectbox div[data-baseweb="select"] > div {
background-color: #333333;
color: #ffffff;
}
/* Button styling */
.stButton > button {
background-color: #e63946;
color: #ffffff;
border: none;
border-radius: 8px;
padding: 10px 20px;
}
.stButton > button:hover {
background-color: #f1faee;
color: #1d3557;
}
/* Recommendation card styling */
.movie-card {
background-color: #2b2b2b;
border-radius: 10px;
padding: 10px;
text-align: center;
margin-bottom: 20px;
}
.movie-title {
font-size: 16px;
font-weight: bold;
color: #f1faee;
margin-top: 10px;
}
</style>
""", unsafe_allow_html=True)
# --- DATA LOADING AND FUNCTIONS ---
API_KEY = "876da4e3af6fba7088031847fc5090d8" # IMPORTANT: Replace with your TMDb API key
@st.cache_data
def load_data():
"""Loads the movie data and similarity matrix from pickle files."""
movies_df = pd.DataFrame(pickle.load(open('movies.pkl', 'rb')))
similarity_matrix = pickle.load(open('similarity.pkl', 'rb'))
return movies_df, similarity_matrix
def fetch_poster(movie_id):
"""Fetches the movie poster URL from the TMDb API."""
try:
response = requests.get(f"https://api.themoviedb.org/3/movie/{movie_id}?api_key={API_KEY}&language=en-US")
response.raise_for_status() # Raise an exception for bad status codes
data = response.json()
poster_path = data.get('poster_path')
if poster_path:
return f"https://image.tmdb.org/t/p/w500/{poster_path}"
except requests.exceptions.RequestException as e:
st.error(f"Error fetching poster: {e}")
return "https://placehold.co/500x750/2b2b2b/f1faee?text=No+Poster" # Fallback image
def recommend(movie_title, n=5):
"""
Recommends movies based on fuzzy matching.
Returns a list of recommended movie titles and their posters.
"""
all_titles = new_df['title'].tolist()
best_match = process.extractOne(movie_title, all_titles)
if best_match[1] < 75: # Confidence threshold
return None, None, f"Sorry, couldn't find a close match for '{movie_title}'."
matched_title = best_match[0]
movie_index = new_df[new_df['title'] == matched_title].index[0]
distances = similarity[movie_index]
movies_list = sorted(list(enumerate(distances)), reverse=True, key=lambda x: x[1])[1:n+1]
recommended_movies = []
recommended_posters = []
for i in movies_list:
movie_id = new_df.iloc[i[0]].movie_id
recommended_movies.append(new_df.iloc[i[0]].title)
recommended_posters.append(fetch_poster(movie_id))
return matched_title, recommended_movies, recommended_posters
# Load the data
new_df, similarity = load_data()
# --- STREAMLIT USER INTERFACE ---
st.title('Movie Recommender System')
st.markdown("### Find your next favorite movie!")
# Create a select box for the user to choose a movie
selected_movie_name = st.selectbox(
'Type or select a movie you like from the dropdown:',
new_df['title'].values,
index=None, # No default selection
placeholder="Choose a movie..."
)
# Add a "Recommend" button
if st.button('Get Recommendations'):
if selected_movie_name:
with st.spinner('Finding recommendations for you...'):
found_title, rec_movies, rec_posters = recommend(selected_movie_name, n=5)
if rec_movies:
st.subheader(f"Because you watched '{found_title}', you might like:")
# Create 5 columns for the 5 recommendations
cols = st.columns(5)
for i in range(5):
with cols[i]:
st.markdown(f"""
<div class="movie-card">
<img src="{rec_posters[i]}" style="width:100%; border-radius: 7px;">
<p class="movie-title">{rec_movies[i]}</p>
</div>
""", unsafe_allow_html=True)
else:
st.warning(found_title) # Display the "not found" message
else:
st.warning("Please select a movie first.")