-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
359 lines (301 loc) Β· 12 KB
/
app.py
File metadata and controls
359 lines (301 loc) Β· 12 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import streamlit as st
import pandas as pd
from recommender import MovieRecommender
# ---------------------- PAGE CONFIG ---------------------- #
st.set_page_config(
page_title="CineSense - Movie Recommender",
page_icon="π¬",
layout="wide",
)
# ---------------------- CUSTOM CSS ----------------------- #
st.markdown(
"""
<style>
/* MAIN APP BG */
[data-testid="stAppViewContainer"] {
background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
font-family: 'Inter', sans-serif;
}
/* Prevent global recoloring of toolbar */
[data-testid="stAppViewContainer"] * {
color: #e8eaf6 !important;
}
/* Sidebar styling */
section[data-testid="stSidebar"] {
background: rgba(15, 23, 42, 0.60);
backdrop-filter: blur(20px);
border-right: 2px solid rgba(255, 255, 255, 0.1);
}
section[data-testid="stSidebar"] * {
color: #e0e7ff !important;
}
/* Inputs */
div[data-baseweb="select"] > div {
background-color: rgba(255,255,255,0.07) !important;
border-radius: 6px !important;
color: white !important;
}
.stTextInput input {
background-color: rgba(255,255,255,0.07) !important;
color: white !important;
}
/* Title styling */
.main-title {
font-size: 3.2rem;
font-weight: 900;
margin-bottom: 0;
display: flex;
align-items: center;
gap: 10px;
}
/* Subtitle */
.subtitle {
color: #e2e8f0 !important;
opacity: 0.9;
}
/* Movie card */
.movie-card {
background: rgba(255, 255, 255, 0.07);
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 18px;
padding: 1.3rem;
transition: 0.3s;
backdrop-filter: blur(12px);
box-shadow: 0 10px 30px rgba(0,0,0,0.25);
}
.movie-card:hover {
transform: translateY(-6px) scale(1.02);
box-shadow: 0 20px 40px rgba(0,0,0,0.45);
}
.movie-title {
font-size: 1.3rem;
font-weight: 700;
color: #f8fafc !important;
}
.badge {
display: inline-block;
padding: 5px 11px;
border-radius: 999px;
background: rgba(96,165,250,0.22);
border: 1px solid rgba(96,165,250,0.35);
color: #93c5fd;
font-size: 0.75rem;
margin-right: 5px;
}
/* Footer */
.footer {
width: 100%;
text-align: center;
padding: 12px 0;
color: #c7d2fe;
font-size: 14px;
margin-top: 40px;
}
/* Header Toolbar - Gradient match */
[data-testid="stToolbar"] {
background: linear-gradient(90deg, #0f0c29, #302b63, #24243e) !important;
backdrop-filter: blur(8px);
border-bottom: 1px solid rgba(255,255,255,0.15);
}
[data-testid="stToolbar"] * {
color: #e8eaf6 !important;
visibility: visible !important;
opacity: 1 !important;
}
</style>
""",
unsafe_allow_html=True,
)
# ---------------------- LOAD MODEL ----------------------- #
@st.cache_resource(show_spinner=True)
def load_recommender():
rec = MovieRecommender("movies.csv")
rec.fit()
return rec
recommender = load_recommender()
movies_df = recommender.movies
# ---------------------- NAVIGATION ----------------------- #
st.sidebar.title("π Navigation")
page = st.sidebar.radio("Go to:", ["π Home", "π¬ Recommendations"])
# ---------------------- HEADER --------------------------- #
col1, col2 = st.columns([2.5, 1.3])
with col1:
st.markdown(
"""
<div class="main-title">
π¬ <span style="background: linear-gradient(90deg, #60a5fa, #93c5fd);
-webkit-background-clip: text;
color: transparent;">CineSense</span>
</div>
""",
unsafe_allow_html=True
)
st.markdown(
'<div class="subtitle">Your personalized movie recommendation engine powered by AI & content-based filtering.</div>',
unsafe_allow_html=True
)
with col2:
st.metric("Movies Loaded", f"{len(movies_df):,}")
# ---------------------- HOME PAGE ------------------------ #
if page == "π Home":
st.markdown("""
<div style='margin-top: 20px; padding: 40px;
background: rgba(255,255,255,0.05);
border-radius: 22px;
backdrop-filter: blur(12px);
border: 1px solid rgba(255,255,255,0.08);'>
<h1 style='font-size: 3rem; font-weight: 900;
background: linear-gradient(90deg, #38bdf8, #c084fc);
-webkit-background-clip: text; color: transparent;'>
Welcome to CineSense
</h1>
<p style='color:#cbd5e1; font-size: 1.25rem; margin-top: -10px;'>
Your intelligent AI-powered movie recommendation assistant.
</p>
<h2 style='color:#a5b4fc; margin-top: 30px;'>π Features</h2>
<ul style='color:#dbeafe; font-size: 1.1rem; line-height: 1.8;'>
<li><b>π― Similar Movie Recommendations</b> β Find movies similar to any title.</li>
<li><b>β€οΈ Recommendations Based on Favourites</b> β AI learns your taste profile.</li>
<li><b>π Genre-Based Filtering</b> β Explore movies by selected genres.</li>
<li><b>π€ Smart AI Engine</b> β TF-IDF + Cosine Similarity for content understanding.</li>
<li><b>β‘ Fast Processing</b> β Search across 10,000+ movies instantly.</li>
</ul>
<h2 style='color:#a5b4fc; margin-top: 30px;'>β¨ How It Works</h2>
<ol style='color:#dbeafe; font-size: 1.1rem; line-height: 1.8;'>
<li>Select <b>Recommendations</b> from the navigation menu.</li>
<li>Choose your method: Similar Movie, Favourites, or Genres.</li>
<li>Get personalized, AI-powered movie suggestions!</li>
</ol>
<p style='color:#94a3b8; margin-top: 25px; font-size: 1rem;'>
Start your cinematic journey using the sidebar ππΏ
</p>
</div>
""", unsafe_allow_html=True)
st.markdown("""
<div style="
margin-top: 50px;
padding: 16px;
text-align: center;
font-size: 0.95rem;
color: #d1d5e1;
background: rgba(255,255,255,0.04);
border-top: 1px solid rgba(255,255,255,0.12);
backdrop-filter: blur(6px);
border-radius: 12px;
">
Built with β€οΈ by Priya<br> Built for movie lovers, by a movie lover.<br>Β© 2025 <b>CineSense</b>
</div>
""", unsafe_allow_html=True)
st.stop()
# ---------------------- RECOMMENDATIONS PAGE ------------- #
if page == "π¬ Recommendations":
# Sidebar Filters
st.sidebar.title("π Recommendation Controls")
mode = st.sidebar.radio(
"How do you want recommendations?",
["Similar to a movie", "Based on favourites", "Just by genres"],
)
all_titles = movies_df["title"].astype(str).sort_values().tolist()
if mode == "Similar to a movie":
selected_title = st.sidebar.selectbox("Choose a movie you like", all_titles)
elif mode == "Based on favourites":
selected_titles = st.sidebar.multiselect(
"Pick a few of your favourite movies",
all_titles
)
genres_available = recommender.get_genres()
selected_genres = st.sidebar.multiselect(
"Filter by genres (optional)",
genres_available,
)
top_n = st.sidebar.slider("Number of recommendations", 5, 20, 10)
# Main area
st.subheader("Recommendations")
# Helper: Genre filter
def apply_genre_filter(df):
if not selected_genres or df is None:
return df
def match(movie_genres):
mg = {g.strip().lower() for g in str(movie_genres).split(",")}
return all(g.lower() in mg for g in selected_genres)
return df[df["genre"].apply(match)]
# ---------- Recommendation Modes ----------
if mode == "Similar to a movie":
st.subheader(f"Because you liked: **{selected_title}**")
results, err = recommender.recommend_similar_by_title(selected_title, top_n=top_n*2)
if err:
st.error(err)
else:
results = apply_genre_filter(results).head(top_n)
cols = st.columns(2)
for i, (_, row) in enumerate(results.iterrows()):
with cols[i % 2]:
st.markdown(
f"""
<div class="movie-card">
<div class="movie-title">{row['title']}</div>
<div class="movie-meta">{str(row['release_date'])[:4]} β’ β {row['vote_average']}</div>
<div class="movie-meta"><b>Genres:</b> {row['genre']}</div>
<div style="margin:0.4rem 0;">
<span class="badge">Similarity: {row['similarity']:.2f}</span>
<span class="badge">Lang: {row['original_language']}</span>
</div>
<div class="movie-overview">{row['overview'][:280]}...</div>
</div>
""",
unsafe_allow_html=True,
)
elif mode == "Based on favourites":
st.subheader("Because of your favourite movies:")
if not selected_titles:
st.info("Choose at least one movie from favourites.")
else:
results, err = recommender.recommend_from_favourites(selected_titles, top_n=top_n*2)
if err:
st.error(err)
else:
results = apply_genre_filter(results).head(top_n)
cols = st.columns(2)
for i, (_, row) in enumerate(results.iterrows()):
with cols[i % 2]:
st.markdown(
f"""
<div class="movie-card">
<div class="movie-title">{row['title']}</div>
<div class="movie-meta">{str(row['release_date'])[:4]} β’ β {row['vote_average']}</div>
<div class="movie-meta"><b>Genres:</b> {row['genre']}</div>
<div style="margin:0.4rem 0;">
<span class="badge">Similarity: {row['similarity']:.2f}</span>
<span class="badge">Lang: {row['original_language']}</span>
</div>
<div class="movie-overview">{row['overview'][:280]}...</div>
</div>
""",
unsafe_allow_html=True,
)
elif mode == "Just by genres":
st.subheader("Genre-based Recommendations")
results = recommender.filter_by_genres(selected_genres).head(top_n)
cols = st.columns(2)
for i, (_, row) in enumerate(results.iterrows()):
with cols[i % 2]:
st.markdown(
f"""
<div class="movie-card">
<div class="movie-title">{row['title']}</div>
<div class="movie-meta">{str(row['release_date'])[:4]} β’ β {row['vote_average']}</div>
<div class="movie-meta"><b>Genres:</b> {row['genre']}</div>
<div class="movie-overview">{row['overview'][:280]}...</div>
</div>
""",
unsafe_allow_html=True,
)
# ---------------------- FOOTER --------------------------- #
st.markdown(
"""
<div class="footer">
Built with TF-IDF, cosine similarity & Streamlit β’ Crafted by Priya π
</div>
""",
unsafe_allow_html=True
)