Update app4.py
Browse files
app4.py
CHANGED
|
@@ -1,176 +1,207 @@
|
|
| 1 |
-
import pickle
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import requests
|
| 4 |
-
from datetime import datetime
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
st.
|
| 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 |
-
st.
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
if
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
if st.
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
st.markdown("
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
# ุฅุนุฏุงุฏ ุงูุตูุญุฉ
|
| 7 |
+
st.set_page_config(page_title="๐ฌ Movie Recommender", page_icon="๐๏ธ", layout="wide")
|
| 8 |
+
|
| 9 |
+
# -------------------- ุตูุญุฉ ุฅุฏุฎุงู ุงุณู
ุงูู
ุณุชุฎุฏู
--------------------
|
| 10 |
+
if 'name' not in st.session_state:
|
| 11 |
+
st.session_state.name = None
|
| 12 |
+
|
| 13 |
+
# ุงูุชุญูู ุฅุฐุง ูุงู ุงูู
ุณุชุฎุฏู
ูุฏ ุฃุฏุฎู ุงุณู
ู ุจุงููุนู
|
| 14 |
+
if st.session_state.name is None:
|
| 15 |
+
# ุตูุญุฉ ุฅุฏุฎุงู ุงูุงุณู
|
| 16 |
+
st.title("๐ฌ Welcome to the Movie Recommender!")
|
| 17 |
+
st.markdown("Please enter your name to continue.")
|
| 18 |
+
|
| 19 |
+
name = st.text_input("Enter your name", "")
|
| 20 |
+
|
| 21 |
+
if st.button("Submit"):
|
| 22 |
+
if name:
|
| 23 |
+
st.session_state.name = name # ุชุฎุฒูู ุงูุงุณู
ูู session_state
|
| 24 |
+
st.success(f"Hello, {name}! Welcome to the Movie Recommender ๐ฌ")
|
| 25 |
+
# ุฅุนุงุฏุฉ ุชุญู
ูู ุงูุตูุญุฉ ุจุงุณุชุฎุฏุงู
st.empty()
|
| 26 |
+
st.empty() # ูู
ูู ุฅุนุงุฏุฉ ุชุญู
ูู ุงูู
ุญุชูู ููุง ุฃู ู
ู ุฎูุงู ุงุณุชุจุฏุงู ูุฐุง ุจู rerun ุจุณูุท
|
| 27 |
+
else:
|
| 28 |
+
st.error("Please enter a valid name.") # ุฅุฐุง ูู
ูุชู
ุฅุฏุฎุงู ุงุณู
|
| 29 |
+
|
| 30 |
+
else:
|
| 31 |
+
# -------------------- ุชุญุณูู ุชูุณูู ุงูุดุฑูุท ุงูุฌุงูุจู ุจุงุณุชุฎุฏุงู
CSS --------------------
|
| 32 |
+
st.markdown("""
|
| 33 |
+
<style>
|
| 34 |
+
.sidebar .sidebar-content {
|
| 35 |
+
background-color: #2c3e50; /* ุฎูููุฉ ุฏููุงุก */
|
| 36 |
+
color: white;
|
| 37 |
+
padding: 15px;
|
| 38 |
+
border-radius: 10px;
|
| 39 |
+
}
|
| 40 |
+
.sidebar .sidebar-content h1, .sidebar .sidebar-content h2 {
|
| 41 |
+
font-size: 20px;
|
| 42 |
+
font-weight: bold;
|
| 43 |
+
color: #ecf0f1; /* ููู ุงููุต */
|
| 44 |
+
}
|
| 45 |
+
.sidebar .sidebar-content .stButton button {
|
| 46 |
+
background-color: #3498db; /* ุฒุฑ ููู ุฃุฒุฑู */
|
| 47 |
+
color: white;
|
| 48 |
+
font-weight: bold;
|
| 49 |
+
border-radius: 5px;
|
| 50 |
+
width: 100%;
|
| 51 |
+
padding: 10px;
|
| 52 |
+
}
|
| 53 |
+
.sidebar .sidebar-content .stButton button:hover {
|
| 54 |
+
background-color: #2980b9;
|
| 55 |
+
}
|
| 56 |
+
</style>
|
| 57 |
+
""", unsafe_allow_html=True)
|
| 58 |
+
|
| 59 |
+
# -------------------- ุชุญู
ูู ุงูุจูุงูุงุช --------------------
|
| 60 |
+
try:
|
| 61 |
+
movies = pickle.load(open('movie_list.pkl', 'rb'))
|
| 62 |
+
similarity = pickle.load(open('similarity.pkl', 'rb'))
|
| 63 |
+
except FileNotFoundError:
|
| 64 |
+
st.error("โ Required data files (movie_list.pkl and similarity.pkl) not found.")
|
| 65 |
+
st.stop()
|
| 66 |
+
|
| 67 |
+
# -------------------- ุฏูุงู ุงูู
ุณุงุนุฏุฉ --------------------
|
| 68 |
+
def fetch_poster(movie_id):
|
| 69 |
+
try:
|
| 70 |
+
url = f"https://api.themoviedb.org/3/movie/{movie_id}?api_key=8265bd1679663a7ea12ac168da84d2e8&language=en-US"
|
| 71 |
+
response = requests.get(url)
|
| 72 |
+
data = response.json()
|
| 73 |
+
poster_path = data.get('poster_path')
|
| 74 |
+
return f"https://image.tmdb.org/t/p/w500/{poster_path}" if poster_path else "https://via.placeholder.com/500x750?text=No+Image"
|
| 75 |
+
except:
|
| 76 |
+
return "https://via.placeholder.com/500x750?text=Error"
|
| 77 |
+
|
| 78 |
+
def fetch_movie_details(movie_id):
|
| 79 |
+
try:
|
| 80 |
+
url = f"https://api.themoviedb.org/3/movie/{movie_id}?api_key=8265bd1679663a7ea12ac168da84d2e8&language=en-US"
|
| 81 |
+
response = requests.get(url)
|
| 82 |
+
data = response.json()
|
| 83 |
+
overview = data.get('overview', 'No overview available.')
|
| 84 |
+
rating = data.get('vote_average', 'No rating')
|
| 85 |
+
release_date = data.get('release_date', 'No release date')
|
| 86 |
+
genres = ', '.join([genre['name'] for genre in data.get('genres', [])])
|
| 87 |
+
return overview, rating, release_date, genres
|
| 88 |
+
except:
|
| 89 |
+
return "No details available.", "No rating", "No release date", "No genres"
|
| 90 |
+
|
| 91 |
+
def recommend(movie):
|
| 92 |
+
if movie not in movies['title'].values:
|
| 93 |
+
return [], [], []
|
| 94 |
+
index = movies[movies['title'] == movie].index[0]
|
| 95 |
+
distances = sorted(list(enumerate(similarity[index])), reverse=True, key=lambda x: x[1])
|
| 96 |
+
recommended_names = []
|
| 97 |
+
recommended_posters = []
|
| 98 |
+
recommended_ids = []
|
| 99 |
+
for i in distances[1:6]:
|
| 100 |
+
movie_id = movies.iloc[i[0]].movie_id
|
| 101 |
+
recommended_names.append(movies.iloc[i[0]].title)
|
| 102 |
+
recommended_posters.append(fetch_poster(movie_id))
|
| 103 |
+
recommended_ids.append(movie_id)
|
| 104 |
+
return recommended_names, recommended_posters, recommended_ids
|
| 105 |
+
|
| 106 |
+
def fetch_popular_movies():
|
| 107 |
+
url = "https://api.themoviedb.org/3/movie/popular?api_key=8265bd1679663a7ea12ac168da84d2e8&language=en-US&page=1"
|
| 108 |
+
try:
|
| 109 |
+
response = requests.get(url)
|
| 110 |
+
data = response.json()
|
| 111 |
+
popular_movies = []
|
| 112 |
+
for movie in data['results'][:60]:
|
| 113 |
+
title = movie['title']
|
| 114 |
+
poster_path = movie.get('poster_path')
|
| 115 |
+
poster_url = f"https://image.tmdb.org/t/p/w500/{poster_path}" if poster_path else "https://via.placeholder.com/500x750?text=No+Image"
|
| 116 |
+
movie_id = movie['id']
|
| 117 |
+
popular_movies.append((title, poster_url, movie_id))
|
| 118 |
+
return popular_movies
|
| 119 |
+
except:
|
| 120 |
+
return []
|
| 121 |
+
|
| 122 |
+
# -------------------- ุงูุชููู ูู ุงูุดุฑูุท ุงูุฌุงูุจู --------------------
|
| 123 |
+
st.sidebar.markdown("""
|
| 124 |
+
<div style="text-align:center; color:white; font-size:30px; font-weight:bold;">
|
| 125 |
+
๐๏ธ Movie Recommender
|
| 126 |
+
</div>
|
| 127 |
+
<hr style="border-color: #ecf0f1;">
|
| 128 |
+
""", unsafe_allow_html=True)
|
| 129 |
+
|
| 130 |
+
st.session_state.selected_section = st.sidebar.radio(
|
| 131 |
+
"Navigate",
|
| 132 |
+
["Popular Movies", "Recommend Movies", "Movie Details"]
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# -------------------- ุงูุตูุญุฉ ุงูุฑุฆูุณูุฉ --------------------
|
| 136 |
+
st.markdown(f"### Hello, {st.session_state.get('name', 'Guest')} ๐", unsafe_allow_html=True)
|
| 137 |
+
|
| 138 |
+
if st.session_state.selected_section == "Popular Movies":
|
| 139 |
+
with st.expander("๐ฅ Popular Movies", expanded=True):
|
| 140 |
+
popular = fetch_popular_movies()
|
| 141 |
+
if popular:
|
| 142 |
+
cols = st.columns(5)
|
| 143 |
+
for i, (title, poster_url, movie_id) in enumerate(popular):
|
| 144 |
+
with cols[i % 5]:
|
| 145 |
+
st.image(poster_url, use_container_width=True)
|
| 146 |
+
st.markdown(f"<div class='movie-title'>{title}</div>", unsafe_allow_html=True)
|
| 147 |
+
if st.button("๐ Details", key=f"popular_details_{i}"):
|
| 148 |
+
overview, rating, release_date, genres = fetch_movie_details(movie_id)
|
| 149 |
+
st.markdown(f"**๐
Release Date:** {release_date}")
|
| 150 |
+
st.markdown(f"**โญ Rating:** {rating}")
|
| 151 |
+
st.markdown(f"**๐ญ Genres:** {genres}")
|
| 152 |
+
st.markdown(f"**๐ Overview:** {overview}")
|
| 153 |
+
else:
|
| 154 |
+
st.warning("Could not fetch popular movies.")
|
| 155 |
+
|
| 156 |
+
elif st.session_state.selected_section == "Recommend Movies":
|
| 157 |
+
movie_list = movies['title'].values
|
| 158 |
+
selected_movie = st.selectbox("๐ฅ Select a Movie", movie_list)
|
| 159 |
+
rating = st.slider("โญ Your Rating", 1, 10, 1)
|
| 160 |
+
st.write(f"โ
Your rating: {rating} stars")
|
| 161 |
+
|
| 162 |
+
if "show_recommendations" not in st.session_state:
|
| 163 |
+
st.session_state.show_recommendations = False
|
| 164 |
+
|
| 165 |
+
if st.button("๐ฏ Show Recommendations"):
|
| 166 |
+
st.session_state.show_recommendations = not st.session_state.show_recommendations
|
| 167 |
+
|
| 168 |
+
if st.session_state.show_recommendations:
|
| 169 |
+
with st.spinner("โณ Fetching recommendations..."):
|
| 170 |
+
names, posters, ids = recommend(selected_movie)
|
| 171 |
+
if names:
|
| 172 |
+
st.markdown(f"### โ
Recommended Movies Similar to {selected_movie}:")
|
| 173 |
+
cols = st.columns(5)
|
| 174 |
+
for i in range(len(names)):
|
| 175 |
+
with cols[i % 5]:
|
| 176 |
+
st.image(posters[i], use_container_width=True)
|
| 177 |
+
st.markdown(f"<div class='movie-title'>{names[i]}</div>", unsafe_allow_html=True)
|
| 178 |
+
if st.button("๐ Details", key=f"recommend_details_{i}"):
|
| 179 |
+
overview, rating, release_date, genres = fetch_movie_details(ids[i])
|
| 180 |
+
st.markdown(f"**๐
Release Date:** {release_date}")
|
| 181 |
+
st.markdown(f"**โญ Rating:** {rating}")
|
| 182 |
+
st.markdown(f"**๐ญ Genres:** {genres}")
|
| 183 |
+
st.markdown(f"**๐ Overview:** {overview}")
|
| 184 |
+
else:
|
| 185 |
+
st.warning("No recommendations found.")
|
| 186 |
+
|
| 187 |
+
elif st.session_state.selected_section == "Movie Details":
|
| 188 |
+
if "show_details" not in st.session_state:
|
| 189 |
+
st.session_state.show_details = {}
|
| 190 |
+
|
| 191 |
+
movie_list = movies['title'].values
|
| 192 |
+
selected_movie = st.selectbox("๐ฅ Select a Movie to View Details", movie_list)
|
| 193 |
+
index = movies[movies['title'] == selected_movie].index[0]
|
| 194 |
+
movie_id = movies.iloc[index].movie_id
|
| 195 |
+
overview, rating, release_date, genres = fetch_movie_details(movie_id)
|
| 196 |
+
st.image(fetch_poster(movie_id), use_container_width=True)
|
| 197 |
+
st.markdown(f"**๐
Release Date:** {release_date}")
|
| 198 |
+
st.markdown(f"**โญ Rating:** {rating}")
|
| 199 |
+
st.markdown(f"**๐ญ Genres:** {genres}")
|
| 200 |
+
st.markdown(f"**๐ Overview:** {overview}")
|
| 201 |
+
# -------------------- Footer --------------------
|
| 202 |
+
st.markdown("<hr>", unsafe_allow_html=True)
|
| 203 |
+
st.markdown("<div class='footer'>ยฉ 2025 โข by Youssef samy โข Powered by TMDB API</div>", unsafe_allow_html=True)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
|