Upload 2 files
Browse files- app.py +68 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
import pickle
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
movies_dict = pickle.load(open('pickle_files\movies_dict.pkl', 'rb'))
|
| 8 |
+
movies = pd.DataFrame(movies_dict)
|
| 9 |
+
movies_list = movies['title'].tolist()
|
| 10 |
+
|
| 11 |
+
movies_cos_sim = pickle.load(open('pickle_files\movies_cos_sim.pkl', 'rb'))
|
| 12 |
+
|
| 13 |
+
print(movies)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def get_recommendations(movie):
|
| 17 |
+
if movie in movies['title'].tolist():
|
| 18 |
+
index = movies[movies['title']==movie].index[0]
|
| 19 |
+
ascending_indices = movies_cos_sim[index].argsort()
|
| 20 |
+
descending_indices = ascending_indices[::-1]
|
| 21 |
+
return movies.iloc[descending_indices[1:21]]['title'].tolist()
|
| 22 |
+
else:
|
| 23 |
+
return 0
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
TMDB_API_KEY = 'd86b9111d56c0b1da25f8f37a94c9a2b'
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def fetch_movie_poster(movie_name):
|
| 32 |
+
search_url = f"https://api.themoviedb.org/3/search/movie?api_key={TMDB_API_KEY}&query={movie_name}"
|
| 33 |
+
response = requests.get(search_url).json()
|
| 34 |
+
|
| 35 |
+
if response['results']:
|
| 36 |
+
poster_path = response['results'][0]['poster_path']
|
| 37 |
+
full_poster_url = f"https://image.tmdb.org/t/p/w500{poster_path}"
|
| 38 |
+
return full_poster_url
|
| 39 |
+
else:
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
st.title("Movie Recommendation System")
|
| 45 |
+
|
| 46 |
+
movie_name = st.selectbox("Enter a movie name:", movies_list)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
if st.button("Get Recommendations"):
|
| 51 |
+
if movie_name:
|
| 52 |
+
recommendations = get_recommendations(movie_name)
|
| 53 |
+
if recommendations:
|
| 54 |
+
st.write("Here are some movies you might like:")
|
| 55 |
+
cols = st.columns(5)
|
| 56 |
+
for idx, movie in enumerate(recommendations):
|
| 57 |
+
col = cols[idx % 5]
|
| 58 |
+
with col:
|
| 59 |
+
st.write(movie)
|
| 60 |
+
poster_url = fetch_movie_poster(movie)
|
| 61 |
+
if poster_url:
|
| 62 |
+
st.image(poster_url, width=250)
|
| 63 |
+
else:
|
| 64 |
+
st.write("Poster not found.")
|
| 65 |
+
else:
|
| 66 |
+
st.write("No recommendations found. Please check the movie name and try again.")
|
| 67 |
+
else:
|
| 68 |
+
st.write("Please enter a movie name.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pickle
|
| 3 |
+
requests
|
| 4 |
+
pandas
|