File size: 1,638 Bytes
9658bdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import gradio as gr

# Create a toy user-movie ratings matrix (replace this with your real data)
# Rows represent users, columns represent movies
# Use NaN for missing ratings
ratings_matrix = np.array([
    [5, 3, 0, 0, 2],
    [4, 0, 0, 0, 1],
    [1, 1, 0, 0, 0],
    [1, 0, 0, 0, 0],
    [0, 1, 5, 4, 0],
])

# Collaborative filtering model function
def collaborative_filtering(user_id):
    # Get the ratings of the user
    user_ratings = ratings_matrix[user_id]

    # Calculate the similarity between the user and all other users
    similarities = []
    for other_user_ratings in ratings_matrix:
        similarity = np.dot(user_ratings, other_user_ratings) / (
            np.linalg.norm(user_ratings) * np.linalg.norm(other_user_ratings)
        )
        similarities.append(similarity)

    # Find the most similar user (excluding the user itself)
    most_similar_user_id = np.argmax(similarities)
    
    # Get the movie ratings of the most similar user
    movie_ratings = ratings_matrix[most_similar_user_id]

    # Predict the ratings for the user based on the most similar user's ratings
    predicted_ratings = np.where(user_ratings == 0, movie_ratings, 0)

    # Get the indices of the top recommended movies
    top_movie_indices = np.argsort(predicted_ratings)[::-1][:5]

    return top_movie_indices.tolist()

# Create a Gradio interface
iface = gr.Interface(
    fn=collaborative_filtering,
    inputs="number",
    outputs="text",
    title="Movie Recommendation System",
    description="Enter a user ID (0-4) to get movie recommendations.",
)

# Launch the Gradio interface
iface.launch()