1
1
import streamlit as st
2
2
import pandas as pd
3
3
import pickle
4
- from streamlit import session_state as session
5
4
import requests
6
- import numpy as np
5
+ import os
7
6
8
7
def fetch_link (movie_name ):
9
- movie_name = '-' .join (movie_name .split ())
10
- movie_link = f"https://www.justwatch.com/in/movie/{ movie_name } "
8
+ movie_name = '-' .join (movie_name .split ())
9
+ movie_link = f"https://www.justwatch.com/in/movie/{ movie_name } "
11
10
response = requests .get (movie_link )
12
11
if response .status_code == 200 :
13
12
return movie_link
14
13
else :
15
- tv_link = f"https://www.justwatch.com/in/tv-show/{ movie_name } "
14
+ tv_link = f"https://www.justwatch.com/in/tv-show/{ movie_name } "
16
15
response = requests .get (tv_link )
17
16
if response .status_code == 200 :
18
17
return tv_link
19
18
20
- return f"https://en.wikipedia.org/wiki/{ '_' .join (movie_name .split ())} "
19
+ return f"https://en.wikipedia.org/wiki/{ '_' .join (movie_name .split ())} "
21
20
22
21
def fetch_trailer (movie_name ):
23
22
return f"https://www.youtube.com/results?search_query={ movie_name } +trailer"
24
23
25
-
26
- def recommend (movies ,input_movie ):
24
+ def recommend (movies , input_movie ):
27
25
movie_index = movies [movies ['title' ] == input_movie ].index [0 ]
28
26
distances = similarity [movie_index ]
29
27
movies_list = sorted (list (enumerate (distances )), reverse = True , key = lambda x : x [1 ])[1 :6 ]
@@ -36,49 +34,60 @@ def recommend(movies,input_movie):
36
34
recommended_movies .append (movie_title )
37
35
recommended_movies_link .append (fetch_link (movie_title ))
38
36
recommended_movies_trailer_link .append (fetch_trailer (movie_title ))
39
- return recommended_movies , recommended_movies_link ,recommended_movies_trailer_link
37
+ return recommended_movies , recommended_movies_link , recommended_movies_trailer_link
38
+
39
+ # Define paths to your files
40
+ similarity_path = r'Web_app/pages/similarity.pkl'
41
+ movies_csv_path = r'Web_app/movies.csv'
40
42
41
- similarity = pickle .load (open ('similarity.pkl' , 'rb' ))
42
- movie_data = pd .read_csv ("movies.csv" ) #you can either generate from scraper.py or can copy paste your own movies data
43
+ # Check if the similarity file exists
44
+ if os .path .exists (similarity_path ):
45
+ with open (similarity_path , 'rb' ) as file :
46
+ similarity = pickle .load (file )
47
+ else :
48
+ st .error (f"File not found: { similarity_path } " )
49
+ similarity = None
43
50
51
+ # Check if the movies CSV file exists
52
+ if os .path .exists (movies_csv_path ):
53
+ movie_data = pd .read_csv (movies_csv_path )
54
+ else :
55
+ st .error (f"File not found: { movies_csv_path } " )
56
+ movie_data = pd .DataFrame (columns = ['title' ]) # Empty DataFrame to avoid errors
44
57
45
58
st .title ("What to binge?" )
46
59
st .subheader ('Movie recommendation :film_projector:' , divider = 'rainbow' )
47
60
st .write ("" )
48
- recent_movie = st .selectbox ("Which movie/tv show you just watched :sunglasses:!!!" ,movie_data ['title' ]) # input movie name
49
- recommend_movies = {"Name" :[],"Link" :[]}
50
- result = pd .DataFrame .from_dict (recommend_movies )
51
-
52
- if st .button ('Recommend' ):
53
- names , links , trailers = recommend (movie_data ,recent_movie )
54
-
55
- data_df = pd .DataFrame (
56
- {
57
- "Name" : names ,
58
- "Link" : links ,
59
- "Trailer" : trailers
60
- }
61
- )
62
-
63
- st .data_editor (
64
- data_df ,
65
- column_config = {
66
- "Name" : st .column_config .TextColumn (
67
- "Top Recommended Movies" ,
68
- help = "The top recommended movies"
69
- ),
70
- "Link" : st .column_config .LinkColumn (
71
- "(watch/description) Links" ,
72
- help = "Links of recommended movies"
73
- ),
74
- "Trailer" : st .column_config .LinkColumn (
75
- "Trailer Links" ,
76
- help = "Youtuber trailer links of recommended movies"
77
- ),
78
- }
79
- )
80
-
81
-
61
+ recent_movie = st .selectbox ("Which movie/tv show you just watched :sunglasses:!!!" , movie_data ['title' ])
82
62
63
+ if st .button ('Recommend' ):
64
+ if similarity is not None and not movie_data .empty :
65
+ names , links , trailers = recommend (movie_data , recent_movie )
66
+
67
+ data_df = pd .DataFrame (
68
+ {
69
+ "Name" : names ,
70
+ "Link" : links ,
71
+ "Trailer" : trailers
72
+ }
73
+ )
83
74
84
-
75
+ st .data_editor (
76
+ data_df ,
77
+ column_config = {
78
+ "Name" : st .column_config .TextColumn (
79
+ "Top Recommended Movies" ,
80
+ help = "The top recommended movies"
81
+ ),
82
+ "Link" : st .column_config .LinkColumn (
83
+ "(watch/description) Links" ,
84
+ help = "Links of recommended movies"
85
+ ),
86
+ "Trailer" : st .column_config .LinkColumn (
87
+ "Trailer Links" ,
88
+ help = "YouTube trailer links of recommended movies"
89
+ ),
90
+ }
91
+ )
92
+ else :
93
+ st .error ("Recommendation system cannot run because required files are missing." )
0 commit comments