Spaces:
Sleeping
Sleeping
Upload 11 files
Browse files- .gitattributes +1 -0
- .gitignore +44 -0
- README.md +58 -12
- Untitled.ipynb +0 -0
- app.py +467 -0
- movie_dict.pkl +3 -0
- movies.pkl +3 -0
- packages.txt +1 -0
- requirements.txt +7 -0
- similarity.pkl +3 -0
- tmdb_5000_credits.xls +3 -0
- tmdb_5000_movies.xls +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
tmdb_5000_credits.xls filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
*.so
|
| 6 |
+
.Python
|
| 7 |
+
env/
|
| 8 |
+
build/
|
| 9 |
+
develop-eggs/
|
| 10 |
+
dist/
|
| 11 |
+
downloads/
|
| 12 |
+
eggs/
|
| 13 |
+
.eggs/
|
| 14 |
+
lib/
|
| 15 |
+
lib64/
|
| 16 |
+
parts/
|
| 17 |
+
sdist/
|
| 18 |
+
var/
|
| 19 |
+
*.egg-info/
|
| 20 |
+
.installed.cfg
|
| 21 |
+
*.egg
|
| 22 |
+
|
| 23 |
+
# Streamlit
|
| 24 |
+
.streamlit/
|
| 25 |
+
|
| 26 |
+
# Virtual Environment
|
| 27 |
+
venv/
|
| 28 |
+
ENV/
|
| 29 |
+
|
| 30 |
+
# IDE
|
| 31 |
+
.idea/
|
| 32 |
+
.vscode/
|
| 33 |
+
*.swp
|
| 34 |
+
*.swo
|
| 35 |
+
|
| 36 |
+
# OS specific
|
| 37 |
+
.DS_Store
|
| 38 |
+
Thumbs.db
|
| 39 |
+
|
| 40 |
+
# Large data files
|
| 41 |
+
# Uncomment if you want to exclude large data files from git
|
| 42 |
+
# *.pkl
|
| 43 |
+
# *.csv
|
| 44 |
+
# *.h5
|
README.md
CHANGED
|
@@ -1,12 +1,58 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Movie Recommender System
|
| 2 |
+
|
| 3 |
+
A Streamlit-based movie recommendation application that uses a hybrid approach combining content-based filtering and collaborative filtering.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- Content-based movie recommendations
|
| 8 |
+
- Collaborative filtering based on user preferences
|
| 9 |
+
- Beautiful, responsive UI
|
| 10 |
+
- Movie details including posters, ratings, genres, and overviews
|
| 11 |
+
- Wishlist functionality
|
| 12 |
+
- Search history tracking
|
| 13 |
+
|
| 14 |
+
## Deployment on Hugging Face Spaces
|
| 15 |
+
|
| 16 |
+
This application is designed to be deployed on Hugging Face Spaces.
|
| 17 |
+
|
| 18 |
+
### Setup Instructions
|
| 19 |
+
|
| 20 |
+
1. Create a new Space on Hugging Face
|
| 21 |
+
- Select **Streamlit** as the SDK
|
| 22 |
+
- Set the Python version to 3.9+
|
| 23 |
+
|
| 24 |
+
2. Upload the following files to your Space:
|
| 25 |
+
- `app.py`: Main application code
|
| 26 |
+
- `requirements.txt`: Dependencies
|
| 27 |
+
- `movie_dict.pkl`: Movie data dictionary
|
| 28 |
+
- `similarity.pkl`: Similarity matrix
|
| 29 |
+
|
| 30 |
+
3. Configure the Space:
|
| 31 |
+
- Set a secret environment variable `TMDB_API_KEY` with your TMDB API key
|
| 32 |
+
- Allocate sufficient RAM (at least 4GB recommended due to the size of similarity matrix)
|
| 33 |
+
|
| 34 |
+
4. Build the Space and wait for the deployment to complete
|
| 35 |
+
|
| 36 |
+
## Local Development
|
| 37 |
+
|
| 38 |
+
To run the application locally:
|
| 39 |
+
|
| 40 |
+
```bash
|
| 41 |
+
pip install -r requirements.txt
|
| 42 |
+
streamlit run app.py
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
## Data Sources
|
| 46 |
+
|
| 47 |
+
The application uses The Movie Database (TMDB) API for fetching movie details and posters.
|
| 48 |
+
|
| 49 |
+
## Implementation Details
|
| 50 |
+
|
| 51 |
+
- **Data Preprocessing**: The movie data is preprocessed and similarity scores are calculated based on movie features (genres, keywords, cast, crew, etc.)
|
| 52 |
+
- **Recommendation Algorithm**: Uses cosine similarity for content-based filtering and combines it with collaborative filtering based on user's wishlist
|
| 53 |
+
- **User Interface**: Built with Streamlit and custom CSS for a modern, responsive design
|
| 54 |
+
- **Data Structures**: Uses a linked list for search history and a deque for wishlist management
|
| 55 |
+
|
| 56 |
+
## License
|
| 57 |
+
|
| 58 |
+
This project is open source and available under the MIT License.
|
Untitled.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,467 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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requests
|
| 6 |
+
from collections import deque
|
| 7 |
+
import time
|
| 8 |
+
import os
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
# Set page configuration
|
| 12 |
+
st.set_page_config(
|
| 13 |
+
page_title="Movie Recommender System",
|
| 14 |
+
page_icon="🎬",
|
| 15 |
+
layout="wide",
|
| 16 |
+
initial_sidebar_state="expanded"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Apply custom CSS
|
| 20 |
+
st.markdown("""
|
| 21 |
+
<style>
|
| 22 |
+
.main-header {
|
| 23 |
+
font-size: 36px;
|
| 24 |
+
font-weight: bold;
|
| 25 |
+
color: #FF4B4B;
|
| 26 |
+
text-align: center;
|
| 27 |
+
margin-bottom: 20px;
|
| 28 |
+
padding: 20px;
|
| 29 |
+
background-color: #1E1E1E;
|
| 30 |
+
border-radius: 10px;
|
| 31 |
+
}
|
| 32 |
+
.sub-header {
|
| 33 |
+
font-size: 24px;
|
| 34 |
+
font-weight: bold;
|
| 35 |
+
color: #4B4BFF;
|
| 36 |
+
margin-top: 30px;
|
| 37 |
+
margin-bottom: 10px;
|
| 38 |
+
}
|
| 39 |
+
.movie-card {
|
| 40 |
+
background-color: #2E2E2E;
|
| 41 |
+
border-radius: 10px;
|
| 42 |
+
padding: 15px;
|
| 43 |
+
margin-bottom: 15px;
|
| 44 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
| 45 |
+
}
|
| 46 |
+
.rating-badge {
|
| 47 |
+
background-color: #FFD700;
|
| 48 |
+
color: #000;
|
| 49 |
+
padding: 5px 10px;
|
| 50 |
+
border-radius: 15px;
|
| 51 |
+
font-weight: bold;
|
| 52 |
+
display: inline-block;
|
| 53 |
+
margin-top: 5px;
|
| 54 |
+
}
|
| 55 |
+
.movie-title {
|
| 56 |
+
font-size: 18px;
|
| 57 |
+
font-weight: bold;
|
| 58 |
+
margin-bottom: 10px;
|
| 59 |
+
color: white;
|
| 60 |
+
}
|
| 61 |
+
.movie-info {
|
| 62 |
+
font-size: 14px;
|
| 63 |
+
margin-bottom: 5px;
|
| 64 |
+
color: #CCC;
|
| 65 |
+
}
|
| 66 |
+
.wishlist-btn {
|
| 67 |
+
background-color: #4CAF50;
|
| 68 |
+
color: white;
|
| 69 |
+
border: none;
|
| 70 |
+
padding: 8px 15px;
|
| 71 |
+
text-align: center;
|
| 72 |
+
text-decoration: none;
|
| 73 |
+
display: inline-block;
|
| 74 |
+
border-radius: 5px;
|
| 75 |
+
cursor: pointer;
|
| 76 |
+
}
|
| 77 |
+
.sidebar-content {
|
| 78 |
+
padding: 15px;
|
| 79 |
+
background-color: #262730;
|
| 80 |
+
border-radius: 10px;
|
| 81 |
+
margin-bottom: 20px;
|
| 82 |
+
}
|
| 83 |
+
.stApp {
|
| 84 |
+
max-width: 1200px;
|
| 85 |
+
margin: 0 auto;
|
| 86 |
+
}
|
| 87 |
+
</style>
|
| 88 |
+
""", unsafe_allow_html=True)
|
| 89 |
+
|
| 90 |
+
# Get the directory where the script is located
|
| 91 |
+
SCRIPT_DIR = Path(__file__).parent.absolute()
|
| 92 |
+
|
| 93 |
+
# Define a Node for the Linked List
|
| 94 |
+
class Node:
|
| 95 |
+
def __init__(self, data=None):
|
| 96 |
+
self.data = data
|
| 97 |
+
self.next = None
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# Define the Linked List class for the search history
|
| 101 |
+
class LinkedList:
|
| 102 |
+
def __init__(self):
|
| 103 |
+
self.head = None
|
| 104 |
+
|
| 105 |
+
def append(self, data):
|
| 106 |
+
new_node = Node(data)
|
| 107 |
+
if self.head is None:
|
| 108 |
+
self.head = new_node
|
| 109 |
+
else:
|
| 110 |
+
current = self.head
|
| 111 |
+
while current.next:
|
| 112 |
+
current = current.next
|
| 113 |
+
current.next = new_node
|
| 114 |
+
|
| 115 |
+
def get_all(self):
|
| 116 |
+
history = []
|
| 117 |
+
current = self.head
|
| 118 |
+
while current:
|
| 119 |
+
history.append(current.data)
|
| 120 |
+
current = current.next
|
| 121 |
+
return history
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# Function to fetch movie details from TMDB
|
| 125 |
+
def fetch_movie_details(movie_id):
|
| 126 |
+
# API key should ideally be stored as an environment variable
|
| 127 |
+
# For Hugging Face, set this in your Space settings
|
| 128 |
+
api_key = os.environ.get('TMDB_API_KEY', 'b75fe8f52c05acaed8865a54505ed806')
|
| 129 |
+
|
| 130 |
+
try:
|
| 131 |
+
response = requests.get(
|
| 132 |
+
f'https://api.themoviedb.org/3/movie/{movie_id}?api_key={api_key}&language=en-US')
|
| 133 |
+
data = response.json()
|
| 134 |
+
|
| 135 |
+
poster_path = data.get('poster_path', '')
|
| 136 |
+
poster_url = "https://image.tmdb.org/t/p/w500/" + poster_path if poster_path else "https://via.placeholder.com/500x750?text=No+Image+Available"
|
| 137 |
+
|
| 138 |
+
return {
|
| 139 |
+
'poster_url': poster_url,
|
| 140 |
+
'overview': data.get('overview', 'No overview available'),
|
| 141 |
+
'release_date': data.get('release_date', 'Unknown'),
|
| 142 |
+
'vote_average': data.get('vote_average', 0),
|
| 143 |
+
'genres': [genre['name'] for genre in data.get('genres', [])]
|
| 144 |
+
}
|
| 145 |
+
except Exception as e:
|
| 146 |
+
st.error(f"Error fetching movie details: {e}")
|
| 147 |
+
return {
|
| 148 |
+
'poster_url': "https://via.placeholder.com/500x750?text=Error+Loading+Image",
|
| 149 |
+
'overview': 'Error loading movie details',
|
| 150 |
+
'release_date': 'Unknown',
|
| 151 |
+
'vote_average': 0,
|
| 152 |
+
'genres': []
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
# Function to recommend movies based on hybrid approach
|
| 157 |
+
def recommend(movie, num_recommendations=6):
|
| 158 |
+
try:
|
| 159 |
+
# Get movie index
|
| 160 |
+
movie_index = movies[movies['title'] == movie].index[0]
|
| 161 |
+
|
| 162 |
+
# Get content-based similarity scores
|
| 163 |
+
content_distances = similarity[movie_index]
|
| 164 |
+
|
| 165 |
+
# Get collaborative filtering component (based on user preferences in wishlist if available)
|
| 166 |
+
if len(st.session_state.wishlist) > 0:
|
| 167 |
+
# Find similar movies to wishlist items
|
| 168 |
+
wishlist_indices = [movies[movies['title'] == wish_movie].index[0] for wish_movie in st.session_state.wishlist if wish_movie in movies['title'].values]
|
| 169 |
+
|
| 170 |
+
if wishlist_indices:
|
| 171 |
+
# Calculate average similarity to wishlist items
|
| 172 |
+
wishlist_similarity = np.mean([similarity[idx] for idx in wishlist_indices], axis=0)
|
| 173 |
+
|
| 174 |
+
# Combine content-based and collaborative filtering (weighted average)
|
| 175 |
+
combined_distances = 0.7 * content_distances + 0.3 * wishlist_similarity
|
| 176 |
+
else:
|
| 177 |
+
combined_distances = content_distances
|
| 178 |
+
else:
|
| 179 |
+
combined_distances = content_distances
|
| 180 |
+
|
| 181 |
+
# Get movie recommendations
|
| 182 |
+
movie_indices = sorted(list(enumerate(combined_distances)), reverse=True, key=lambda x: x[1])[1:num_recommendations+1]
|
| 183 |
+
|
| 184 |
+
recommended_movies = []
|
| 185 |
+
for i in movie_indices:
|
| 186 |
+
movie_id = movies.iloc[i[0]].movie_id
|
| 187 |
+
movie_title = movies.iloc[i[0]].title
|
| 188 |
+
movie_details = fetch_movie_details(movie_id)
|
| 189 |
+
|
| 190 |
+
recommended_movies.append({
|
| 191 |
+
'title': movie_title,
|
| 192 |
+
'id': movie_id,
|
| 193 |
+
'poster': movie_details['poster_url'],
|
| 194 |
+
'overview': movie_details['overview'],
|
| 195 |
+
'release_date': movie_details['release_date'],
|
| 196 |
+
'rating': movie_details['vote_average'],
|
| 197 |
+
'genres': movie_details['genres'],
|
| 198 |
+
'similarity_score': round(i[1] * 100, 1)
|
| 199 |
+
})
|
| 200 |
+
|
| 201 |
+
return recommended_movies
|
| 202 |
+
except Exception as e:
|
| 203 |
+
st.error(f"Error in recommendation algorithm: {e}")
|
| 204 |
+
return []
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
# Load the movie data
|
| 208 |
+
@st.cache_data
|
| 209 |
+
def load_data():
|
| 210 |
+
try:
|
| 211 |
+
# Construct paths dynamically to work in different environments
|
| 212 |
+
movie_dict_path = os.path.join(SCRIPT_DIR, 'movie_dict.pkl')
|
| 213 |
+
similarity_path = os.path.join(SCRIPT_DIR, 'similarity.pkl')
|
| 214 |
+
|
| 215 |
+
# Check if files exist
|
| 216 |
+
if not os.path.exists(movie_dict_path):
|
| 217 |
+
st.error(f"File not found: {movie_dict_path}")
|
| 218 |
+
st.stop()
|
| 219 |
+
|
| 220 |
+
if not os.path.exists(similarity_path):
|
| 221 |
+
st.error(f"File not found: {similarity_path}")
|
| 222 |
+
st.stop()
|
| 223 |
+
|
| 224 |
+
# Load the data
|
| 225 |
+
with open(movie_dict_path, 'rb') as f:
|
| 226 |
+
movies_dict = pickle.load(f)
|
| 227 |
+
|
| 228 |
+
movies_df = pd.DataFrame(movies_dict)
|
| 229 |
+
|
| 230 |
+
with open(similarity_path, 'rb') as f:
|
| 231 |
+
similarity_matrix = pickle.load(f)
|
| 232 |
+
|
| 233 |
+
return movies_df, similarity_matrix
|
| 234 |
+
except Exception as e:
|
| 235 |
+
st.error(f"Error loading data: {e}")
|
| 236 |
+
st.stop()
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
# Load data
|
| 240 |
+
try:
|
| 241 |
+
movies, similarity = load_data()
|
| 242 |
+
except Exception as e:
|
| 243 |
+
st.error(f"Error loading data: {e}")
|
| 244 |
+
st.stop()
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
# Initialize the wishlist queue
|
| 248 |
+
if 'wishlist' not in st.session_state:
|
| 249 |
+
st.session_state.wishlist = deque(maxlen=10) # Limit to 10 movies
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
# Initialize the search history linked list
|
| 253 |
+
if 'search_history' not in st.session_state:
|
| 254 |
+
st.session_state.search_history = LinkedList()
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
# Initialize other session states
|
| 258 |
+
if 'show_recommendations' not in st.session_state:
|
| 259 |
+
st.session_state.show_recommendations = False
|
| 260 |
+
if 'current_recommendations' not in st.session_state:
|
| 261 |
+
st.session_state.current_recommendations = []
|
| 262 |
+
if 'tab' not in st.session_state:
|
| 263 |
+
st.session_state.tab = "recommend"
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
# Create sidebar for user options
|
| 267 |
+
with st.sidebar:
|
| 268 |
+
st.markdown('<div class="sidebar-content">', unsafe_allow_html=True)
|
| 269 |
+
|
| 270 |
+
# Use a direct URL for the image in Hugging Face
|
| 271 |
+
st.image("https://img.icons8.com/color/96/000000/film-reel.png", width=80)
|
| 272 |
+
st.markdown("## Movie Explorer")
|
| 273 |
+
|
| 274 |
+
# Navigation
|
| 275 |
+
selected_tab = st.radio("Navigation", ["Recommendations", "Wishlist", "History"])
|
| 276 |
+
|
| 277 |
+
if selected_tab == "Recommendations":
|
| 278 |
+
st.session_state.tab = "recommend"
|
| 279 |
+
elif selected_tab == "Wishlist":
|
| 280 |
+
st.session_state.tab = "wishlist"
|
| 281 |
+
else:
|
| 282 |
+
st.session_state.tab = "history"
|
| 283 |
+
|
| 284 |
+
st.markdown("## About")
|
| 285 |
+
st.info("This movie recommendation system uses a hybrid approach combining content-based filtering and collaborative filtering to provide personalized movie recommendations.")
|
| 286 |
+
|
| 287 |
+
# Add Hugging Face attribution
|
| 288 |
+
st.markdown("## Deployment")
|
| 289 |
+
st.success("Deployed on Hugging Face Spaces")
|
| 290 |
+
|
| 291 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
# Main content
|
| 295 |
+
st.markdown('<h1 class="main-header">🎬 Movie Recommender System</h1>', unsafe_allow_html=True)
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
# Recommendations Tab
|
| 299 |
+
if st.session_state.tab == "recommend":
|
| 300 |
+
st.markdown('<h2 class="sub-header">Find Your Next Favorite Movie</h2>', unsafe_allow_html=True)
|
| 301 |
+
|
| 302 |
+
# Movie selection with autocomplete
|
| 303 |
+
col1, col2 = st.columns([3, 1])
|
| 304 |
+
with col1:
|
| 305 |
+
selected_movie_name = st.selectbox(
|
| 306 |
+
'Select a movie you like:',
|
| 307 |
+
movies['title'].values
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
+
with col2:
|
| 311 |
+
recommendation_button = st.button('Get Recommendations', type="primary")
|
| 312 |
+
|
| 313 |
+
# Display movie details for selected movie
|
| 314 |
+
if selected_movie_name:
|
| 315 |
+
movie_idx = movies[movies['title'] == selected_movie_name].index[0]
|
| 316 |
+
movie_id = movies.iloc[movie_idx].movie_id
|
| 317 |
+
movie_details = fetch_movie_details(movie_id)
|
| 318 |
+
|
| 319 |
+
col1, col2 = st.columns([1, 3])
|
| 320 |
+
with col1:
|
| 321 |
+
st.image(movie_details['poster_url'], width=200)
|
| 322 |
+
|
| 323 |
+
with col2:
|
| 324 |
+
st.markdown(f"### {selected_movie_name}")
|
| 325 |
+
st.markdown(f"**Released:** {movie_details['release_date']}")
|
| 326 |
+
st.markdown(f"**Rating:** {movie_details['vote_average']}/10")
|
| 327 |
+
st.markdown(f"**Genres:** {', '.join(movie_details['genres'])}")
|
| 328 |
+
st.markdown(f"**Overview:** {movie_details['overview']}")
|
| 329 |
+
|
| 330 |
+
# Add to wishlist button
|
| 331 |
+
if st.button('Add to Wishlist', key='add_wishlist'):
|
| 332 |
+
if selected_movie_name not in st.session_state.wishlist:
|
| 333 |
+
st.session_state.wishlist.append(selected_movie_name)
|
| 334 |
+
st.success(f'Added "{selected_movie_name}" to your wishlist!')
|
| 335 |
+
else:
|
| 336 |
+
st.info(f'"{selected_movie_name}" is already in your wishlist!')
|
| 337 |
+
|
| 338 |
+
# Get and display recommendations
|
| 339 |
+
if recommendation_button:
|
| 340 |
+
with st.spinner('Finding the best movies for you...'):
|
| 341 |
+
# Simulate processing time for better UX
|
| 342 |
+
time.sleep(0.5) # Reduced time for better performance on Hugging Face
|
| 343 |
+
|
| 344 |
+
# Add the movie to search history linked list
|
| 345 |
+
st.session_state.search_history.append(selected_movie_name)
|
| 346 |
+
|
| 347 |
+
# Get recommendations
|
| 348 |
+
st.session_state.current_recommendations = recommend(selected_movie_name)
|
| 349 |
+
st.session_state.show_recommendations = True
|
| 350 |
+
|
| 351 |
+
# Display recommendations
|
| 352 |
+
if st.session_state.show_recommendations:
|
| 353 |
+
st.markdown('<h2 class="sub-header">Recommended Movies</h2>', unsafe_allow_html=True)
|
| 354 |
+
|
| 355 |
+
if not st.session_state.current_recommendations:
|
| 356 |
+
st.warning("No recommendations found. Please try another movie.")
|
| 357 |
+
else:
|
| 358 |
+
# Display recommendations in a grid
|
| 359 |
+
cols = st.columns(3) # 3 movies per row
|
| 360 |
+
|
| 361 |
+
for i, movie in enumerate(st.session_state.current_recommendations):
|
| 362 |
+
with cols[i % 3]:
|
| 363 |
+
st.markdown(f"""
|
| 364 |
+
<div class="movie-card">
|
| 365 |
+
<div class="movie-title">{movie['title']}</div>
|
| 366 |
+
<div class="rating-badge">⭐ {movie['rating']}/10</div>
|
| 367 |
+
<div class="movie-info">Similarity: {movie['similarity_score']}%</div>
|
| 368 |
+
</div>
|
| 369 |
+
""", unsafe_allow_html=True)
|
| 370 |
+
|
| 371 |
+
st.image(movie['poster'], width=200)
|
| 372 |
+
|
| 373 |
+
with st.expander("Details"):
|
| 374 |
+
st.write(f"**Release Date:** {movie['release_date']}")
|
| 375 |
+
st.write(f"**Genres:** {', '.join(movie['genres'])}")
|
| 376 |
+
st.write(f"**Overview:** {movie['overview']}")
|
| 377 |
+
|
| 378 |
+
if st.button('Add to Wishlist', key=f'add_wish_{i}'):
|
| 379 |
+
if movie['title'] not in st.session_state.wishlist:
|
| 380 |
+
st.session_state.wishlist.append(movie['title'])
|
| 381 |
+
st.success(f'Added "{movie["title"]}" to your wishlist!')
|
| 382 |
+
else:
|
| 383 |
+
st.info(f'"{movie["title"]}" is already in your wishlist!')
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
# Wishlist Tab
|
| 387 |
+
elif st.session_state.tab == "wishlist":
|
| 388 |
+
st.markdown('<h2 class="sub-header">Your Wishlist</h2>', unsafe_allow_html=True)
|
| 389 |
+
|
| 390 |
+
if len(st.session_state.wishlist) > 0:
|
| 391 |
+
# Display the wishlist with additional options
|
| 392 |
+
for i, movie in enumerate(list(st.session_state.wishlist)):
|
| 393 |
+
col1, col2, col3 = st.columns([1, 3, 1])
|
| 394 |
+
|
| 395 |
+
with col1:
|
| 396 |
+
try:
|
| 397 |
+
movie_idx = movies[movies['title'] == movie].index[0]
|
| 398 |
+
movie_id = movies.iloc[movie_idx].movie_id
|
| 399 |
+
movie_details = fetch_movie_details(movie_id)
|
| 400 |
+
st.image(movie_details['poster_url'], width=150)
|
| 401 |
+
except:
|
| 402 |
+
st.image("https://via.placeholder.com/150x225?text=No+Image", width=150)
|
| 403 |
+
|
| 404 |
+
with col2:
|
| 405 |
+
st.markdown(f"### {movie}")
|
| 406 |
+
|
| 407 |
+
try:
|
| 408 |
+
movie_idx = movies[movies['title'] == movie].index[0]
|
| 409 |
+
movie_id = movies.iloc[movie_idx].movie_id
|
| 410 |
+
movie_details = fetch_movie_details(movie_id)
|
| 411 |
+
|
| 412 |
+
st.markdown(f"**Released:** {movie_details['release_date']}")
|
| 413 |
+
st.markdown(f"**Rating:** {movie_details['vote_average']}/10")
|
| 414 |
+
st.markdown(f"**Genres:** {', '.join(movie_details['genres'])}")
|
| 415 |
+
with st.expander("Overview"):
|
| 416 |
+
st.write(movie_details['overview'])
|
| 417 |
+
except:
|
| 418 |
+
st.write("Details not available")
|
| 419 |
+
|
| 420 |
+
with col3:
|
| 421 |
+
if st.button("Remove", key=f"remove_{i}"):
|
| 422 |
+
st.session_state.wishlist.remove(movie)
|
| 423 |
+
st.experimental_rerun()
|
| 424 |
+
|
| 425 |
+
if st.button("Find Similar", key=f"similar_{i}"):
|
| 426 |
+
st.session_state.tab = "recommend"
|
| 427 |
+
with st.spinner('Finding similar movies...'):
|
| 428 |
+
st.session_state.current_recommendations = recommend(movie)
|
| 429 |
+
st.session_state.show_recommendations = True
|
| 430 |
+
st.experimental_rerun()
|
| 431 |
+
|
| 432 |
+
st.markdown("---")
|
| 433 |
+
|
| 434 |
+
# Clear wishlist button
|
| 435 |
+
if st.button("Clear Wishlist"):
|
| 436 |
+
st.session_state.wishlist.clear()
|
| 437 |
+
st.success("Wishlist cleared!")
|
| 438 |
+
st.experimental_rerun()
|
| 439 |
+
else:
|
| 440 |
+
st.info("Your wishlist is empty. Add movies to your wishlist by clicking 'Add to Wishlist' on movie cards.")
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
# History Tab
|
| 444 |
+
else:
|
| 445 |
+
st.markdown('<h2 class="sub-header">Your Search History</h2>', unsafe_allow_html=True)
|
| 446 |
+
|
| 447 |
+
search_history_list = st.session_state.search_history.get_all()
|
| 448 |
+
|
| 449 |
+
if search_history_list:
|
| 450 |
+
# Display search history
|
| 451 |
+
for i, movie in enumerate(search_history_list):
|
| 452 |
+
col1, col2 = st.columns([4, 1])
|
| 453 |
+
|
| 454 |
+
with col1:
|
| 455 |
+
st.markdown(f"### {i+1}. {movie}")
|
| 456 |
+
|
| 457 |
+
with col2:
|
| 458 |
+
if st.button("Find Again", key=f"find_again_{i}"):
|
| 459 |
+
st.session_state.tab = "recommend"
|
| 460 |
+
with st.spinner('Getting recommendations...'):
|
| 461 |
+
st.session_state.current_recommendations = recommend(movie)
|
| 462 |
+
st.session_state.show_recommendations = True
|
| 463 |
+
st.experimental_rerun()
|
| 464 |
+
|
| 465 |
+
st.markdown("---")
|
| 466 |
+
else:
|
| 467 |
+
st.info("No search history available. Start searching for movie recommendations to build your history.")
|
movie_dict.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3edb834fa65181717a94afccfcc6f05667e3aea8dc52d697cd49e7085721848b
|
| 3 |
+
size 2156446
|
movies.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bdd31f65dad6f5370ba7408bb44867714575a314d1bbcd0e9729ef842f30e0ee
|
| 3 |
+
size 2175040
|
packages.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
build-essential
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.32.0
|
| 2 |
+
pandas==2.0.3
|
| 3 |
+
numpy==1.24.3
|
| 4 |
+
requests==2.31.0
|
| 5 |
+
scikit-learn==1.2.2
|
| 6 |
+
pillow==10.1.0
|
| 7 |
+
protobuf==4.23.4
|
similarity.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9e0798743053348f1075cb166e92bca6568ba504bc2fff99c58aa1feb5e54719
|
| 3 |
+
size 184781251
|
tmdb_5000_credits.xls
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9d0050599ff88d40366c4841204b1489862bca346bfa46c20b05a65d14508435
|
| 3 |
+
size 40044293
|
tmdb_5000_movies.xls
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|