Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,88 +1,88 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
import pickle
|
4 |
-
import requests
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
# Load data
|
9 |
-
movie_list = pickle.load(open('movie_list.pkl', 'rb'))
|
10 |
-
df = pickle.load(open('movies.pkl', 'rb'))
|
11 |
-
similarity = pickle.load(open('similarity.pkl', 'rb'))
|
12 |
-
|
13 |
-
movie_list = pd.DataFrame(movie_list)
|
14 |
-
|
15 |
-
# Title of the app
|
16 |
-
st.title("🎥 Movie Recommender System")
|
17 |
-
|
18 |
-
# TMDb API key
|
19 |
-
# TMDb API key
|
20 |
-
TMDB_API_KEY = "4142bd9f69dc53d39b4f3661066099ef"
|
21 |
-
|
22 |
-
|
23 |
-
# Fetch poster path
|
24 |
-
def get_poster_path(movie_id):
|
25 |
-
response = requests.get(
|
26 |
-
f"https://api.themoviedb.org/3/movie/{movie_id}?api_key={TMDB_API_KEY}&language=en-US"
|
27 |
-
)
|
28 |
-
data = response.json()
|
29 |
-
return f"https://image.tmdb.org/t/p/w500/{data.get('poster_path', '')}"
|
30 |
-
|
31 |
-
# Fetch trailer link
|
32 |
-
def get_trailer(movie_id):
|
33 |
-
response = requests.get(
|
34 |
-
f"https://api.themoviedb.org/3/movie/{movie_id}/videos?api_key={TMDB_API_KEY}&language=en-US"
|
35 |
-
)
|
36 |
-
data = response.json()
|
37 |
-
for video in data.get("results", []):
|
38 |
-
if video["type"] == "Trailer":
|
39 |
-
return f"https://www.youtube.com/watch?v={video['key']}"
|
40 |
-
return None
|
41 |
-
|
42 |
-
# Recommendation logic
|
43 |
-
def recommend(movie):
|
44 |
-
movie_index = df[df['title'] == movie].index[0]
|
45 |
-
distances = similarity[movie_index]
|
46 |
-
movies_list = sorted(list(enumerate(distances)), reverse=True, key=lambda x: x[1])[1:6]
|
47 |
-
|
48 |
-
recommend_movies = []
|
49 |
-
recommend_movie_posters = []
|
50 |
-
recommend_movie_trailers = []
|
51 |
-
|
52 |
-
for i in movies_list:
|
53 |
-
movie_id = df.iloc[i[0]]['id']
|
54 |
-
recommend_movies.append(df.iloc[i[0]]['title'])
|
55 |
-
recommend_movie_posters.append(get_poster_path(movie_id))
|
56 |
-
recommend_movie_trailers.append(get_trailer(movie_id))
|
57 |
-
|
58 |
-
return recommend_movies, recommend_movie_posters, recommend_movie_trailers
|
59 |
-
|
60 |
-
# Search for a movie
|
61 |
-
selected_movie = st.selectbox("🔎 Search for a movie you watched:",movie_list['title'].values)
|
62 |
-
if selected_movie not in movie_list['title'].values:
|
63 |
-
st.warning("Movie not found. Please select a valid movie.")
|
64 |
-
else:
|
65 |
-
st.success(f"You selected: {selected_movie}")
|
66 |
-
|
67 |
-
# Recommend button
|
68 |
-
if st.button("Recommend"):
|
69 |
-
if selected_movie in movie_list['title'].values:
|
70 |
-
names, posters, trailers = recommend(selected_movie)
|
71 |
-
|
72 |
-
# Create a dynamic grid for recommendations
|
73 |
-
st.markdown("## Recommended Movies:")
|
74 |
-
cols = st.columns(5) # Create 5 columns
|
75 |
-
for idx, (name, poster, trailer) in enumerate(zip(names, posters, trailers)):
|
76 |
-
with cols[idx % 5]: # Use modulus to cycle columns
|
77 |
-
st.image(poster, use_container_width=True)
|
78 |
-
|
79 |
-
st.caption(name)
|
80 |
-
if trailer:
|
81 |
-
st.markdown(f"[🎥 Watch Trailer]({trailer})")
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
# Footer
|
87 |
-
st.markdown("---")
|
88 |
-
st.markdown("Powered by [TMDb API](https://www.themoviedb.org/) | Developed with ❤️ by Shaik")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import pickle
|
4 |
+
import requests
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
# Load data
|
9 |
+
movie_list = pickle.load(open('movie_list.pkl', 'rb'))
|
10 |
+
df = pickle.load(open('movies.pkl', 'rb'))
|
11 |
+
similarity = pickle.load(open('similarity.pkl', 'rb'))
|
12 |
+
|
13 |
+
movie_list = pd.DataFrame(movie_list)
|
14 |
+
|
15 |
+
# Title of the app
|
16 |
+
st.title("🎥 Movie Recommender System")
|
17 |
+
|
18 |
+
# TMDb API key
|
19 |
+
# TMDb API key
|
20 |
+
TMDB_API_KEY = "4142bd9f69dc53d39b4f3661066099ef"
|
21 |
+
|
22 |
+
|
23 |
+
# Fetch poster path
|
24 |
+
def get_poster_path(movie_id):
|
25 |
+
response = requests.get(
|
26 |
+
f"https://api.themoviedb.org/3/movie/{movie_id}?api_key={TMDB_API_KEY}&language=en-US"
|
27 |
+
)
|
28 |
+
data = response.json()
|
29 |
+
return f"https://image.tmdb.org/t/p/w500/{data.get('poster_path', '')}"
|
30 |
+
|
31 |
+
# Fetch trailer link
|
32 |
+
def get_trailer(movie_id):
|
33 |
+
response = requests.get(
|
34 |
+
f"https://api.themoviedb.org/3/movie/{movie_id}/videos?api_key={TMDB_API_KEY}&language=en-US"
|
35 |
+
)
|
36 |
+
data = response.json()
|
37 |
+
for video in data.get("results", []):
|
38 |
+
if video["type"] == "Trailer":
|
39 |
+
return f"https://www.youtube.com/watch?v={video['key']}"
|
40 |
+
return None
|
41 |
+
|
42 |
+
# Recommendation logic
|
43 |
+
def recommend(movie):
|
44 |
+
movie_index = df[df['title'] == movie].index[0]
|
45 |
+
distances = similarity[movie_index]
|
46 |
+
movies_list = sorted(list(enumerate(distances)), reverse=True, key=lambda x: x[1])[1:6]
|
47 |
+
|
48 |
+
recommend_movies = []
|
49 |
+
recommend_movie_posters = []
|
50 |
+
recommend_movie_trailers = []
|
51 |
+
|
52 |
+
for i in movies_list:
|
53 |
+
movie_id = df.iloc[i[0]]['id']
|
54 |
+
recommend_movies.append(df.iloc[i[0]]['title'])
|
55 |
+
recommend_movie_posters.append(get_poster_path(movie_id))
|
56 |
+
recommend_movie_trailers.append(get_trailer(movie_id))
|
57 |
+
|
58 |
+
return recommend_movies, recommend_movie_posters, recommend_movie_trailers
|
59 |
+
|
60 |
+
# Search for a movie
|
61 |
+
selected_movie = st.selectbox("🔎 Search for a movie you watched:",movie_list['title'].values)
|
62 |
+
if selected_movie not in movie_list['title'].values:
|
63 |
+
st.warning("Movie not found. Please select a valid movie.")
|
64 |
+
else:
|
65 |
+
st.success(f"You selected: {selected_movie}")
|
66 |
+
|
67 |
+
# Recommend button
|
68 |
+
if st.button("Recommend"):
|
69 |
+
if selected_movie in movie_list['title'].values:
|
70 |
+
names, posters, trailers = recommend(selected_movie)
|
71 |
+
|
72 |
+
# Create a dynamic grid for recommendations
|
73 |
+
st.markdown("## Recommended Movies:")
|
74 |
+
cols = st.columns(5) # Create 5 columns
|
75 |
+
for idx, (name, poster, trailer) in enumerate(zip(names, posters, trailers)):
|
76 |
+
with cols[idx % 5]: # Use modulus to cycle columns
|
77 |
+
st.image(poster, use_container_width=True)
|
78 |
+
|
79 |
+
st.caption(name)
|
80 |
+
if trailer:
|
81 |
+
st.markdown(f"[🎥 Watch Trailer]({trailer})")
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
# Footer
|
87 |
+
st.markdown("---")
|
88 |
+
st.markdown("Powered by [TMDb API](https://www.themoviedb.org/) | Developed with ❤️ by [Shaik](https://www.linkedin.com/in/shaik-hidaythulla/)")
|