Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,26 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import streamlit as st
|
| 3 |
-
from surprise import Dataset, Reader, SVD
|
| 4 |
-
from surprise.model_selection import train_test_split
|
| 5 |
-
from collections import defaultdict
|
| 6 |
import kagglehub
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Step
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
print("
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Prepare dataset for Surprise
|
| 33 |
-
reader = Reader(rating_scale=(df['score'].min(), df['score'].max()))
|
| 34 |
-
data = Dataset.load_from_df(df[['orig_title', 'orig_lang', 'score']], reader)
|
| 35 |
-
|
| 36 |
-
# Train collaborative filtering model
|
| 37 |
-
trainset, testset = train_test_split(data, test_size=0.2, random_state=42)
|
| 38 |
-
model = SVD(n_factors=50, random_state=42)
|
| 39 |
-
model.fit(trainset)
|
| 40 |
-
|
| 41 |
-
# Function to get movie recommendations
|
| 42 |
-
def get_recommendations(selected_movies, genre):
|
| 43 |
-
if not selected_movies:
|
| 44 |
-
return ["Please select at least one movie."]
|
| 45 |
-
|
| 46 |
-
# Filter dataset by genre
|
| 47 |
-
filtered_movies = df[df['genre'] == genre]
|
| 48 |
-
|
| 49 |
-
# Store average scores of all movies
|
| 50 |
-
movie_scores = defaultdict(float)
|
| 51 |
-
|
| 52 |
-
# Predict ratings for all movies in the filtered dataset
|
| 53 |
-
for movie in filtered_movies['orig_title'].unique():
|
| 54 |
-
est_score = model.predict(uid='user', iid=movie).est
|
| 55 |
-
movie_scores[movie] = est_score
|
| 56 |
-
|
| 57 |
-
# Sort movies by predicted score (descending)
|
| 58 |
-
recommended_movies = sorted(movie_scores.items(), key=lambda x: x[1], reverse=True)
|
| 59 |
-
|
| 60 |
-
# Exclude already selected movies
|
| 61 |
-
recommended_movies = [movie for movie, _ in recommended_movies if movie not in selected_movies]
|
| 62 |
-
|
| 63 |
-
return recommended_movies[:5] # Return top 5 recommendations
|
| 64 |
-
|
| 65 |
-
# Streamlit UI
|
| 66 |
-
st.title("🎬 Movie Recommendation System")
|
| 67 |
-
|
| 68 |
-
# Genre selection
|
| 69 |
-
selected_genre = st.selectbox("Select a Genre", sorted(df['genre'].unique().tolist()))
|
| 70 |
-
|
| 71 |
-
# Get available movies for the selected genre
|
| 72 |
-
movies_in_genre = df[df['genre'] == selected_genre]['orig_title'].unique().tolist()
|
| 73 |
-
|
| 74 |
-
# Movie selection
|
| 75 |
-
selected_movies = st.multiselect("Select Up to 3 Movies", movies_in_genre, max_selections=3)
|
| 76 |
-
|
| 77 |
-
# Recommendation button
|
| 78 |
-
if st.button("Get Recommendations"):
|
| 79 |
-
recommendations = get_recommendations(selected_movies, selected_genre)
|
| 80 |
-
st.subheader("Recommended Movies:")
|
| 81 |
-
for movie in recommendations:
|
| 82 |
-
st.write(f"- {movie}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import kagglehub
|
| 2 |
+
import os
|
| 3 |
+
import random
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Step 1: Download the latest version of the dataset
|
| 7 |
+
path = kagglehub.dataset_download("imreallyjohn/cartoonset10k")
|
| 8 |
+
|
| 9 |
+
print("Path to dataset files:", path)
|
| 10 |
+
|
| 11 |
+
# Step 2: List all image files in the dataset directory
|
| 12 |
+
images_folder = os.path.join(path, "/content/CartoonAvatar/CartoonAvatar") # Adjust the subdirectory if needed
|
| 13 |
+
image_files = [f for f in os.listdir(images_folder) if f.endswith('.png')] # Filter for .png files
|
| 14 |
+
|
| 15 |
+
if image_files:
|
| 16 |
+
# Step 3: Select a random available image
|
| 17 |
+
random_image_filename = random.choice(image_files) # Select a random image
|
| 18 |
+
image_path = os.path.join(images_folder, random_image_filename)
|
| 19 |
+
|
| 20 |
+
# Step 4: Load and display the selected image
|
| 21 |
+
selected_image = Image.open(image_path)
|
| 22 |
+
selected_image.show()
|
| 23 |
+
print(f"Displayed image: {random_image_filename}")
|
| 24 |
+
display(selected_image)
|
| 25 |
+
else:
|
| 26 |
+
print("No available images to display.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|