Spaces:
Build error
Build error
Download app.py from mudit68/Recommendation_system: direct link, hf CLI and curl.
- Browser
- Download file 5.32 kB
-
https://huggingface.co/spaces/mudit68/Recommendation_system/resolve/main/app.py
- Command line
-
hf download hf://spaces/mudit68/Recommendation_system/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mudit68/Recommendation_system/resolve/main/app.py
5.32 kB
| import streamlit as st | |
| import pickle | |
| import requests | |
| import os | |
| import time | |
| api_key = os.getenv("API_KEY") | |
| movies_list = pickle.load(open('movies.pkl','rb')) | |
| similarity = pickle.load(open('similarity_matrix.pkl','rb')) | |
| all_movies = movies_list['title'].values | |
| def fetch_poster(id): | |
| response = requests.get(f"https://api.themoviedb.org/3/movie/{id}?api_key={api_key}&language=en-US") | |
| data = response.json() | |
| if data['poster_path']: | |
| return "http://image.tmdb.org/t/p/w500/"+data['poster_path'] | |
| return "https://www.kingsporttn.gov/wp-content/uploads/Image-Not-Available.jpg" | |
| def getMovieData(id): | |
| response = requests.get(f"https://api.themoviedb.org/3/movie/{id}?api_key={api_key}&language=en-US") | |
| data = response.json() | |
| return data | |
| def getCastData(id): | |
| response = requests.get(f"https://api.themoviedb.org/3/movie/{id}/credits?api_key={api_key}") | |
| cast = response.json() | |
| return cast | |
| def minutes_to_hours(minutes): | |
| hours = minutes // 60 | |
| minutes = minutes % 60 | |
| return hours,minutes | |
| def recommend(movie): | |
| recommended_movie = [] | |
| recommended_movie_poster = [] | |
| index = movies_list[movies_list['title']==movie].index[0] | |
| searchedMovieID = movies_list.loc[index].id | |
| data = getMovieData(searchedMovieID) | |
| cast = getCastData(searchedMovieID) | |
| searched_poster_path = fetch_poster(searchedMovieID) | |
| movieList = sorted(list(enumerate(similarity[index])),reverse=True,key=lambda x:x[1])[1:6] | |
| for i in movieList: | |
| movie_id = movies_list.loc[i[0]].id | |
| poster_path = fetch_poster(movie_id) | |
| recommended_movie.append(movies_list.loc[i[0]].title) | |
| recommended_movie_poster.append(poster_path) | |
| return recommended_movie,recommended_movie_poster,searched_poster_path,data,cast | |
| st.title('Movie Database') | |
| movieName = st.selectbox('Enter the movie',all_movies) | |
| if st.button('Search 🔍'): | |
| recommendations,posters,currMoviePoster,data,cast = recommend(movieName) | |
| placeholder = st.empty() | |
| placeholder.progress(0, "Fetching the data...") | |
| time.sleep(1) | |
| placeholder.progress(25, "Wait for it...") | |
| time.sleep(1) | |
| placeholder.progress(50, "Hold a sec...") | |
| time.sleep(1) | |
| placeholder.progress(75, "Recommendations finding...") | |
| time.sleep(1) | |
| placeholder.progress(100, "Almost Done...") | |
| time.sleep(1) | |
| with st.container(border=True,height=400): | |
| c1, c2 = st.columns(2) | |
| with c1: | |
| st.markdown(f'''**:red[Movie: {data['title']}]**''') | |
| st.image(currMoviePoster) | |
| with c2: | |
| with st.container(border=True,height=530): | |
| st.subheader("Details", divider="red") | |
| st.write(f"**Tag Line: {data['tagline']}**") | |
| st.write(data['overview']) | |
| with st.container(border=True,height=150): | |
| st.write(f"**Released on**: {data['release_date']}") | |
| h,m = minutes_to_hours( data['runtime']) | |
| st.write(f"**Total Runtime**: {h} hours & {m} min") | |
| formatted_number = "{:.2f}".format(data['vote_average']) | |
| st.write(f"**Total Revenue**: ${data['revenue']}") | |
| st.write(f"**Rating**: {formatted_number}⭐") | |
| st.subheader("Cast", divider="red") | |
| casts = cast['cast'][0:6] | |
| c1, c2 = st.columns(2) | |
| with c1: | |
| st.image("http://image.tmdb.org/t/p/w500/"+casts[0]['profile_path']) | |
| st.write(f"**{casts[0]['name']}** | {casts[0]['character']}") | |
| with c2: | |
| st.image("http://image.tmdb.org/t/p/w500/"+casts[1]['profile_path']) | |
| st.write(f"**{casts[1]['name']}** | {casts[1]['character']}") | |
| c1, c2 = st.columns(2) | |
| with c1: | |
| st.image("http://image.tmdb.org/t/p/w500/"+casts[2]['profile_path']) | |
| st.write(f"**{casts[2]['name']}** | {casts[2]['character']}") | |
| with c2: | |
| st.image("http://image.tmdb.org/t/p/w500/"+casts[3]['profile_path']) | |
| st.write(f"**{casts[3]['name']}** | {casts[3]['character']}") | |
| c1, c2 = st.columns(2) | |
| with c1: | |
| st.image("http://image.tmdb.org/t/p/w500/"+casts[4]['profile_path']) | |
| st.write(f"**{casts[4]['name']}** | {casts[4]['character']}") | |
| with c2: | |
| st.image("http://image.tmdb.org/t/p/w500/"+casts[5]['profile_path']) | |
| st.write(f"**{casts[5]['name']}** | {casts[5]['character']}") | |
| with st.container(border=True,height=350): | |
| st.subheader("You Might like...", divider="green") | |
| col1, col2, col3, col4, col5 = st.columns(5) | |
| with col1: | |
| st.image(posters[0]) | |
| st.write(recommendations[0]) | |
| with col2: | |
| st.image(posters[1]) | |
| st.write(recommendations[1]) | |
| with col3: | |
| st.image(posters[2]) | |
| st.write(recommendations[2]) | |
| with col4: | |
| st.image(posters[3]) | |
| st.write(recommendations[3]) | |
| with col5: | |
| st.image(posters[4]) | |
| st.write(recommendations[4]) | |
| placeholder.empty() | |