在制作电影推荐项目时陷入了使用 stramlit 获取海报的困境

问题描述 投票:0回答:0

我使用机器学习概念进行电影推荐项目,余弦相似度就是其中之一,在使用这个之后我找到了我的结果,但我也想要电影海报。我写了一些函数来实现它,但它不起作用。所以请帮助我要找到我的错误,我的代码有错误,请建议我。

下面是我的代码

import difflib
import streamlit as st 
import pickle
import pandas as pd
import requests

def fetch_poster(movie_id):
response=requests.get('https://api.themoviedb.org/3/movie/{}? 
api_key=66862ec077a533abc19c22e85570925e&&language=en-US'.format(movie_id))
data=response.json()
return 'https://image.tmdb.org/t/p/original'+data['poster_path']


mv_movie=pd.read_csv('movieList.csv')
similarity=pickle.load(open('similarity.pkl','rb'))

list_of_all_titles= mv_movie['title'].tolist()
def recommend(movie):    
     find_close_match = difflib.get_close_matches(movie, list_of_all_titles)
     close_match = find_close_match[0]
     index_of_the_movie = mv_movie[mv_movie.title == close_match]['index'].values[0]
     similarity_score = list(enumerate(similarity[index_of_the_movie]))
     sorted_similar_movies = sorted(similarity_score, key = lambda x:x[1], reverse = 
     True) 


     recommend_movie_poster=[]


print('Movies suggested for you : \n')
i = 1
for movie in sorted_similar_movies:
    index = movie[0]
    title_from_index = mv_movie[mv_movie.index==index]['title'].values[0]
    fetch_poster= recommend_movie_poster.append(fetch_poster(index))
    
    if (i<6):
        st.write(i, '.',title_from_index,fetch_poster)
        i+=1
        
        col1, col2, col3,col4,col5 = st.columns(5)



        with col1:
            st.header(title_from_index[0])
            st.image(fetch_poster[0])
            
        with col2:
            st.header(title_from_index[1])
            st.image(fetch_poster[1])
            
        
        with col3:
            st.header(title_from_index[2])
            st.image(fetch_poster[2])
            
            
        with col4:
            st.header(title_from_index[3])
            st.image(fetch_poster[3])
        
        with col5:
            st.header(title_from_index[4])
            st.image(fetch_poster[4])
        

      st.title('Movie Recommender System')

      selected_movie_name = st.selectbox(
      'Select Your Favourite Movie',
       list_of_all_titles)

      if st.button('Recommend'):
      recommend(selected_movie_name)
python pandas streamlit cosine-similarity difflib
© www.soinside.com 2019 - 2024. All rights reserved.