Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,11 @@ from surprise import Dataset, Reader, SVD
|
|
3 |
from surprise.model_selection import train_test_split
|
4 |
import pandas as pd
|
5 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Initialize sample user-item rating data
|
8 |
data = {
|
@@ -18,13 +23,26 @@ trainset, testset = train_test_split(dataset, test_size=0.25)
|
|
18 |
algo = SVD()
|
19 |
algo.fit(trainset)
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
def update_ratings(movie, rating):
|
27 |
-
|
|
|
28 |
|
29 |
# UI Elements
|
30 |
st.title("π Personalized Entertainment Recommendations")
|
@@ -66,10 +84,11 @@ st.subheader("π― Recommendations for you:")
|
|
66 |
all_movies = []
|
67 |
for item_id, rating in predicted_ratings:
|
68 |
for rec in recommendations[item_id]:
|
69 |
-
current_ratings =
|
70 |
avg_score = np.mean(current_ratings) if current_ratings else rating
|
|
|
71 |
|
72 |
-
st.write(f"{rec} - Average Score: {avg_score:.2f} β")
|
73 |
|
74 |
# Rating slider from 1 to 10
|
75 |
user_rating = st.slider(f"Rate {rec}", 1, 10, 5, key=f"slider_{rec}")
|
@@ -77,15 +96,15 @@ for item_id, rating in predicted_ratings:
|
|
77 |
update_ratings(rec, user_rating)
|
78 |
st.experimental_rerun() # Rerun to immediately reflect the rating submission
|
79 |
|
80 |
-
all_movies.append((rec, avg_score))
|
81 |
|
82 |
# Sort movies by average score in descending order
|
83 |
sorted_movies = sorted(all_movies, key=lambda x: x[1], reverse=True)
|
84 |
|
85 |
# Display scoreboard
|
86 |
st.subheader("π Scoreboard:")
|
87 |
-
for movie, avg_score in sorted_movies:
|
88 |
-
st.write(f"{movie}: {avg_score:.2f} β")
|
89 |
|
90 |
# Display current session state (for debugging purposes)
|
91 |
-
# st.write("Session State (User Ratings):",
|
|
|
3 |
from surprise.model_selection import train_test_split
|
4 |
import pandas as pd
|
5 |
import numpy as np
|
6 |
+
import json
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Path to the JSON file to persist movie ratings
|
10 |
+
RATINGS_FILE = 'movie_ratings.json'
|
11 |
|
12 |
# Initialize sample user-item rating data
|
13 |
data = {
|
|
|
23 |
algo = SVD()
|
24 |
algo.fit(trainset)
|
25 |
|
26 |
+
# Function to load ratings from a JSON file
|
27 |
+
def load_ratings():
|
28 |
+
if os.path.exists(RATINGS_FILE):
|
29 |
+
with open(RATINGS_FILE, 'r') as file:
|
30 |
+
return json.load(file)
|
31 |
+
else:
|
32 |
+
return {movie: [] for movie in ['Die Hard', 'Mad Max', 'The Shawshank Redemption', 'Forrest Gump',
|
33 |
+
'Superbad', 'Step Brothers', 'Inception', 'Interstellar']}
|
34 |
+
|
35 |
+
# Function to save ratings to a JSON file
|
36 |
+
def save_ratings(ratings):
|
37 |
+
with open(RATINGS_FILE, 'w') as file:
|
38 |
+
json.dump(ratings, file)
|
39 |
+
|
40 |
+
# Load ratings from file
|
41 |
+
movie_ratings = load_ratings()
|
42 |
|
43 |
def update_ratings(movie, rating):
|
44 |
+
movie_ratings[movie].append(rating)
|
45 |
+
save_ratings(movie_ratings)
|
46 |
|
47 |
# UI Elements
|
48 |
st.title("π Personalized Entertainment Recommendations")
|
|
|
84 |
all_movies = []
|
85 |
for item_id, rating in predicted_ratings:
|
86 |
for rec in recommendations[item_id]:
|
87 |
+
current_ratings = movie_ratings[rec]
|
88 |
avg_score = np.mean(current_ratings) if current_ratings else rating
|
89 |
+
vote_count = len(current_ratings)
|
90 |
|
91 |
+
st.write(f"{rec} - Average Score: {avg_score:.2f} β (Votes: {vote_count})")
|
92 |
|
93 |
# Rating slider from 1 to 10
|
94 |
user_rating = st.slider(f"Rate {rec}", 1, 10, 5, key=f"slider_{rec}")
|
|
|
96 |
update_ratings(rec, user_rating)
|
97 |
st.experimental_rerun() # Rerun to immediately reflect the rating submission
|
98 |
|
99 |
+
all_movies.append((rec, avg_score, vote_count))
|
100 |
|
101 |
# Sort movies by average score in descending order
|
102 |
sorted_movies = sorted(all_movies, key=lambda x: x[1], reverse=True)
|
103 |
|
104 |
# Display scoreboard
|
105 |
st.subheader("π Scoreboard:")
|
106 |
+
for movie, avg_score, vote_count in sorted_movies:
|
107 |
+
st.write(f"{movie}: {avg_score:.2f} β (Votes: {vote_count})")
|
108 |
|
109 |
# Display current session state (for debugging purposes)
|
110 |
+
# st.write("Session State (User Ratings):", movie_ratings)
|