File size: 2,520 Bytes
64e5ecd
 
 
 
2f2356b
64e5ecd
2f2356b
 
 
 
 
 
64e5ecd
 
 
 
 
 
 
 
2f2356b
 
 
 
 
 
 
64e5ecd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f2356b
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from surprise import Dataset, Reader, SVD
from surprise.model_selection import train_test_split
import pandas as pd
import numpy as np

# Initialize sample user-item rating data
data = {
    'user_id': [1, 1, 1, 2, 2, 3, 3, 4, 4],
    'item_id': [1, 2, 3, 1, 4, 2, 3, 1, 4],
    'rating': [5, 3, 4, 4, 5, 5, 2, 4, 4]
}
df = pd.DataFrame(data)
reader = Reader(rating_scale=(1, 5))
dataset = Dataset.load_from_df(df[['user_id', 'item_id', 'rating']], reader)
trainset, testset = train_test_split(dataset, test_size=0.25)

algo = SVD()
algo.fit(trainset)

# Initialize session state for storing user ratings
if 'user_ratings' not in st.session_state:
    st.session_state.user_ratings = {1: [], 2: [], 3: [], 4: []}

def update_ratings(item_id, rating):
    st.session_state.user_ratings[item_id].append(rating)

# UI Elements
st.title("🌟 Personalized Entertainment Recommendations")

# Input for user genre preference
user_genre = st.selectbox(
    "Choose your favorite genre", 
    ["Action 🎬", "Drama 🎭", "Comedy πŸ˜‚", "Sci-Fi πŸš€"]
)

# Define genre mapping
genre_mapping = {
    "Action 🎬": 1,
    "Drama 🎭": 2,
    "Comedy πŸ˜‚": 3,
    "Sci-Fi πŸš€": 4
}

# Example mapping of genre to item_id
recommendations = {
    1: ["Die Hard", "Mad Max"], 
    2: ["The Shawshank Redemption", "Forrest Gump"],
    3: ["Superbad", "Step Brothers"], 
    4: ["Inception", "Interstellar"]
}

# Provide recommendation based on collaborative filtering
selected_genre_id = genre_mapping[user_genre]

# Predict ratings for the selected genre items
predicted_ratings = [(id, algo.predict(5, id).est) for id in [1, 2, 3, 4] if id == selected_genre_id]

# Sort recommendations by predicted ratings
predicted_ratings.sort(key=lambda x: x[1], reverse=True)

st.subheader("🎯 Recommendations for you:")
for item_id, rating in predicted_ratings:
    for rec in recommendations[item_id]:
        avg_score = np.mean(st.session_state.user_ratings[item_id]) if st.session_state.user_ratings[item_id] else rating
        st.write(f"{rec} - Average Score: {avg_score:.2f} ⭐")

        # Rating buttons from 1 to 10
        user_rating = st.slider(f"Rate {rec}", 1, 10, 5, key=f"slider_{rec}")
        if st.button(f"Submit Rating for {rec}", key=f"button_{rec}"):
            update_ratings(item_id, user_rating)
            st.write(f"Submitted Rating: {user_rating}")

# Display current session state (for debugging purposes)
st.write("Session State (User Ratings):", st.session_state.user_ratings)