-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
141 lines (132 loc) · 5.04 KB
/
app.py
File metadata and controls
141 lines (132 loc) · 5.04 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
import streamlit as st
import pickle
import pandas as pd
import requests
import time
import base64
def fetch_movie_data(movie_id):
url = f"https://api.themoviedb.org/3/movie/{movie_id}?language=en-US"
headers = {
"accept": "application/json",
"Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI3YTJjNGQwYzM4Yzc0MjdmZjQwMDk2MjQ5NTIyYWY4MyIsIm5iZiI6MTc1MDAwMjQxNi4zNTksInN1YiI6IjY4NGVlYWYwN2ExZGI5YzIxYTI5NGVjMCIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.KuXwbnvA4qcb4jVErRBTHg0M9opeo2KccC74Ae39UOM"
}
try:
response = requests.get(url, headers=headers, timeout=5)
response.raise_for_status()
data = response.json()
poster_path = data.get('poster_path')
if poster_path:
poster_url = "https://image.tmdb.org/t/p/w500/" + poster_path
else:
poster_url = "assets/more_info.jpg" # Local fallback image
return {
"movie_id": data.get("id", movie_id),
"title": data.get("title", "N/A"),
"overview": data.get("overview", "No overview available."),
"release_date": data.get("release_date", "N/A"),
"vote_average": data.get("vote_average", "N/A"),
"poster_url": poster_url,
"genres": ", ".join([g["name"] for g in data.get("genres", [])]),
"runtime": data.get("runtime", "N/A")
}
except Exception as e:
return {
"movie_id": movie_id,
"title": "N/A",
"overview": "No overview available.",
"release_date": "N/A",
"vote_average": "N/A",
"poster_url": "assets/more_info.jpg", # Local fallback image
"genres": "N/A",
"runtime": "N/A"
}
movies_dict = pickle.load(open('models/movies_dict.pkl', 'rb'))
movies = pd.DataFrame(movies_dict)
similarity = pickle.load(open('models/similarity.pkl', 'rb'))
def recommend(movie):
movie_index = movies[movies['title'] == movie].index[0]
distances = similarity[movie_index]
movie_list = sorted(list(enumerate(distances)), reverse=True, key=lambda x: x[1])[1:6]
recommended_movies_data = []
for i in movie_list:
movie_id = movies.iloc[i[0]].movie_id
local_title = movies.iloc[i[0]].title
movie_data = fetch_movie_data(movie_id)
movie_data['title'] = local_title # Always use local title
recommended_movies_data.append(movie_data)
time.sleep(0.5) # To avoid rate limiting
return recommended_movies_data
def get_base64_image(image_path):
with open(image_path, "rb") as img_file:
return base64.b64encode(img_file.read()).decode()
st.markdown(
"""
<style>
.main-title {
font-size: 3em !important;
color: #FF4B4B;
font-weight: bold;
text-align: center;
margin-bottom: 0.5em;
}
.movie-card {
background-color: #f9f9f9;
border-radius: 12px;
padding: 1em;
box-shadow: 0 2px 8px rgba(0,0,0,0.07);
margin-bottom: 1em;
height: 100%;
}
.movie-title {
font-size: 1.2em;
font-weight: bold;
color: #333;
margin-bottom: 0.3em;
}
.movie-meta {
color: #888;
font-size: 0.9em;
margin-bottom: 0.5em;
}
.movie-overview {
font-size: 1em;
color: #444;
margin-bottom: 0.5em;
}
</style>
""",
unsafe_allow_html=True
)
st.markdown('<div class="main-title">🍿 Popcorn Picks</div>', unsafe_allow_html=True)
st.markdown("##### Select a movie you like and get recommendations with details:")
selected_movie_name = st.selectbox(
"Select Movie",
(movies['title'].values),
)
if st.button('Recommend'):
recommended_movies = recommend(selected_movie_name)
local_img_b64 = get_base64_image("assets/more_info.jpg")
for movie in recommended_movies:
tmdb_url = f"https://www.themoviedb.org/movie/{movie['movie_id']}"
if movie['poster_url'] == "assets/more_info.jpg":
img_src = f"data:image/jpeg;base64,{local_img_b64}"
else:
img_src = movie['poster_url']
st.markdown(
f"""
<a href="{tmdb_url}" target="_blank" style="text-decoration: none; color: inherit;">
<div class="movie-card" style="transition: box-shadow 0.2s; cursor: pointer;">
<div style="display: flex; gap: 2em; align-items: flex-start;">
<img src="{img_src}" width="180" style="border-radius:8px; box-shadow:0 2px 8px rgba(0,0,0,0.10);">
<div style="flex:1;">
<div class="movie-title">{movie['title']}</div>
<div class="movie-meta">⭐ {movie['vote_average']} | {movie['release_date']}<br>
{movie['genres']} | {movie['runtime']} min</div>
<div class="movie-overview">{movie['overview']}</div>
</div>
</div>
</div>
</a>
""",
unsafe_allow_html=True
)