CoolFace
Apppublic

Pouriamlk/FinalProjectRecommendationSystem

sourceHugging Faceotherupdated 3y agoView on Hugging Face
0likes
app.py119 linesDownload Raw Back to root
1import pandas as pd2import streamlit as st3from sklearn.metrics.pairwise import linear_kernel, cosine_similarity4from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer5import requests6 7movies_raw = pd.read_csv('dataset/movies_raw.csv')8smd = pd.read_csv('dataset/smd.csv')9smd.fillna('', inplace=True)10indices = pd.Series(smd.index, index=smd['title'])11tf = TfidfVectorizer(analyzer='word',ngram_range=(1, 2),min_df=0.0, stop_words='english')12tfidf_matrix = tf.fit_transform(smd['description'])13cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix)14 15def fetch_poster(movie_id):16    response = requests.get('https://api.themoviedb.org/3/movie/{}?api_key=020b311fe0559698373a16008dc6a672&language=en-US'.format(movie_id))17    data = response.json()18    return "https://image.tmdb.org/t/p/w500/" + data['poster_path']19 20vote_counts = movies_raw[movies_raw['vote_count'].notnull()]['vote_count'].astype('int')21vote_averages = movies_raw[movies_raw['vote_average'].notnull()]['vote_average'].astype('int')22 23C = vote_averages.mean()24m = vote_counts.quantile(0.95)25 26def add_line_break(string):27    words = string.split()28    new_string = ""29    for i, word in enumerate(words):30        new_string += word31        if (i + 1) % 4 == 0:32            new_string += "\n"33        else:34            new_string += " "35    return new_string36 37def weighted_rating(x):38    v = x['vote_count']39    R = x['vote_average']40    return (v/(v+m) * R) + (m/(m+v) * C)41 42def improved_recommendations(title):43    idx = indices[title]44    sim_scores = list(enumerate(cosine_sim[idx]))45    sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)46    sim_scores = sim_scores[1:26]47    movie_indices = [i[0] for i in sim_scores]48 49    movies = smd.iloc[movie_indices][['title', 'id', 'vote_average', 'vote_count', 'overview']]50    vote_counts = movies[movies['vote_count'].notnull()]['vote_count'].astype('int')51    vote_averages = movies[movies['vote_average'].notnull()]['vote_average'].astype('int')52 53    C = vote_averages.mean()54    m = vote_counts.quantile(0.60)55 56    qualified = movies[(movies['vote_count'] >= m) & (movies['vote_count'].notnull()) & (movies['vote_average'].notnull())]57    qualified['vote_average'] = qualified['vote_average'].astype('int')58    qualified = qualified.head(5)59 60    recommended_movies_posters = []61    movie_id_list = qualified['id'].tolist()62    for movie_id in movie_id_list:63        recommended_movies_posters.append(fetch_poster(movie_id))64    65    qualified['vote_average'] = qualified['vote_average'].astype(str)66    qualified['overview'] = qualified['overview'].apply(add_line_break)67 68    return qualified['title'].tolist(), recommended_movies_posters, qualified['vote_average'].tolist(), qualified['overview'].tolist()69 70st.title('Movie Recommender System')71 72selected_movie_name = st.selectbox(73    'Select Your favorite Movie!',74    movies_raw['title'].values75)76 77if st.button('Recommend'):78    names, posters, votes, overviews = improved_recommendations(selected_movie_name)79    col1, col2= st.columns(2, gap='large')80    with col1:81        st.text(names[0])82        st.image(posters[0], width=100)83        st.text('score: ' + votes[0])84        st.text(names[1])85        st.image(posters[1], width=100)86        st.text('score: ' + votes[1])87        st.text(names[2])88        st.image(posters[2], width=100)89        st.text('score: ' + votes[2])90        st.text(names[3])91        st.image(posters[3], width=100)92        st.text('score: ' + votes[3])93        st.text(names[4])94        st.image(posters[4], width=100)95        st.text('score: ' + votes[4])96    with col2:97        st.text(names[0] + ': ')98        st.text(overviews[0])99        st.text(" ")100        st.text(" ")101        st.text(" ")102        st.text(names[1] + ': ')103        st.text(overviews[1])104        st.text(" ")105        st.text(" ")106        st.text(" ")107        st.text(names[2] + ': ')108        st.text(overviews[2])109        st.text(" ")110        st.text(" ")111        st.text(" ")112        st.text(names[3] + ': ')113        st.text(overviews[3])114        st.text(" ")115        st.text(" ")116        st.text(" ")117        st.text(names[4] + ': ')118        st.text(overviews[4])119