Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from surprise import Dataset, Reader, SVD
|
3 |
+
from surprise.model_selection import train_test_split
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
# Sample user-item rating data for collaborative filtering
|
7 |
+
data = {'user_id': [1, 1, 1, 2, 2, 3, 3, 4, 4],
|
8 |
+
'item_id': [1, 2, 3, 1, 4, 2, 3, 1, 4],
|
9 |
+
'rating': [5, 3, 4, 4, 5, 5, 2, 4, 4]}
|
10 |
+
|
11 |
+
df = pd.DataFrame(data)
|
12 |
+
reader = Reader(rating_scale=(1, 5))
|
13 |
+
dataset = Dataset.load_from_df(df[['user_id', 'item_id', 'rating']], reader)
|
14 |
+
trainset, testset = train_test_split(dataset, test_size=0.25)
|
15 |
+
|
16 |
+
algo = SVD()
|
17 |
+
algo.fit(trainset)
|
18 |
+
|
19 |
+
# UI Elements
|
20 |
+
st.title("π Personalized Entertainment Recommendations")
|
21 |
+
|
22 |
+
# Input for user genre preference
|
23 |
+
user_genre = st.selectbox(
|
24 |
+
"Choose your favorite genre",
|
25 |
+
["Action π¬", "Drama π", "Comedy π", "Sci-Fi π"]
|
26 |
+
)
|
27 |
+
|
28 |
+
# Define genre mapping
|
29 |
+
genre_mapping = {
|
30 |
+
"Action π¬": 1,
|
31 |
+
"Drama π": 2,
|
32 |
+
"Comedy π": 3,
|
33 |
+
"Sci-Fi π": 4
|
34 |
+
}
|
35 |
+
|
36 |
+
# Example mapping of genre to item_id
|
37 |
+
recommendations = {
|
38 |
+
1: ["Die Hard", "Mad Max"],
|
39 |
+
2: ["The Shawshank Redemption", "Forrest Gump"],
|
40 |
+
3: ["Superbad", "Step Brothers"],
|
41 |
+
4: ["Inception", "Interstellar"]
|
42 |
+
}
|
43 |
+
|
44 |
+
# Provide recommendation based on collaborative filtering
|
45 |
+
selected_genre_id = genre_mapping[user_genre]
|
46 |
+
|
47 |
+
# Predict ratings for the selected genre items
|
48 |
+
predicted_ratings = [(id, algo.predict(5, id).est) for id in [1, 2, 3, 4] if id == selected_genre_id]
|
49 |
+
|
50 |
+
# Sort recommendations by predicted ratings
|
51 |
+
predicted_ratings.sort(key=lambda x: x[1], reverse=True)
|
52 |
+
|
53 |
+
st.subheader("π― Recommendations for you:")
|
54 |
+
for item_id, rating in predicted_ratings:
|
55 |
+
for rec in recommendations[item_id]:
|
56 |
+
st.write(f"{rec} - Predicted Rating: {rating:.2f} β")
|
57 |
+
|
58 |
+
# Provide visual feedback for actions
|
59 |
+
if predicted_ratings:
|
60 |
+
st.button("π Like", key="like_button")
|
61 |
+
st.button("π Dislike", key="dislike_button")
|
62 |
+
else:
|
63 |
+
st.write("No recommendations available for your selected genre.")
|