-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
77 lines (56 loc) · 1.94 KB
/
Copy pathapp.py
File metadata and controls
77 lines (56 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
os.environ["STREAMLIT_SERVER_PORT"] = "7860"
os.environ["STREAMLIT_SERVER_ADDRESS"] = "0.0.0.0"
import streamlit as st
import pickle
import pandas as pd
import requests
import gdown # ✅ NEW
# ✅ Download similarity.pkl from Google Drive if not present
SIMILARITY_FILE_ID = "1cv7PuP80FxlvkRcVrOhRwOxZRGvJCe1d"
SIMILARITY_PATH = "similarity.pkl"
if not os.path.exists(SIMILARITY_PATH):
url = f"https://drive.google.com/uc?id={SIMILARITY_FILE_ID}"
gdown.download(url, SIMILARITY_PATH, quiet=False)
# 🔑 Replace with your OMDb API key
OMDB_API_KEY = "11fe30b0"
def fetch_poster(title):
url = f"http://www.omdbapi.com/?t={title}&apikey={OMDB_API_KEY}"
response = requests.get(url)
data = response.json()
if data.get("Response") == "True":
return data.get("Poster")
else:
return None
def recommend(movie):
movie_index = movies[movies['title'] == movie].index[0]
distances = similarity[movie_index]
movies_list = sorted(
list(enumerate(distances)),
reverse=True,
key=lambda x: x[1]
)[1:6]
recommended_movies = []
for i in movies_list:
recommended_movies.append(movies.iloc[i[0]].title)
return recommended_movies
# Load data
movies_dict = pickle.load(open('movie_dict.pkl', 'rb'))
movies = pd.DataFrame(movies_dict)
# ✅ This now loads the downloaded file
similarity = pickle.load(open(SIMILARITY_PATH, 'rb'))
# Streamlit UI
st.title("Movie Recommender System 🎬")
selected_movie_name = st.selectbox(
"Select your movie",
movies['title'].values
)
if st.button("Recommend"):
recommendations = recommend(selected_movie_name)
cols = st.columns(5)
for col, movie in zip(cols, recommendations):
with col:
poster = fetch_poster(movie)
if poster:
st.image(poster, width=150)
st.markdown(f"**{movie}**")