Commit eeb3cf8b authored by M.A. Miqdad Ali Riza's avatar M.A. Miqdad Ali Riza

Update Wishlist_create.py

parent fd503541
from main_imports import MDScreen
from ProjectFiles.applibs import utils
import warnings
from kivy.core.audio import SoundLoader
import pandas as pd
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
# Load the song data
song_ratings = pd.read_csv("song_ratings.csv")
import os
warnings.filterwarnings('ignore')
utils.load_kv("Wish_list.kv")
# Create a user-song matrix
user_song_ratings = song_ratings.pivot_table(index='user_id', columns='song_id', values='rating')
user_song_ratings.fillna(0, inplace=True)
# Calculate the cosine similarity between songs
song_similarity = cosine_similarity(user_song_ratings.T)
# Define a function to generate song recommendations for a user
def generate_song_recommendations(user_id, num_recommendations=10):
if user_id not in user_song_ratings.index:
print(f"User ID {user_id} not found.")
return []
class Wish_list_Screen(MDScreen):
def __init__(self, **kwargs):
super(Wish_list_Screen, self).__init__(**kwargs)
user_ratings = user_song_ratings.loc[user_id].values
song_scores = np.dot(user_ratings, song_similarity) / np.sum(np.abs(song_similarity), axis=1)
recommended_song_indices = np.argsort(-song_scores)[:num_recommendations]
recommended_song_ids = user_song_ratings.columns[recommended_song_indices].tolist()
return recommended_song_ids
def btnA(self):
# Example: Generate song recommendations for user 98549
user_id = 98549
recommended_songs = generate_song_recommendations(user_id, num_recommendations=5)
if recommended_songs:
class RecommendationSystem:
def __init__(self, song_ratings_file):
data = pd.read_csv(song_ratings_file)
with open("mood_label.txt", "r") as file:
readdata = file.read()
self.song_ratings = data[data['types'] == readdata]
# self.song_ratings = pd.read_csv(song_ratings_file)
self.user_song_ratings = self.preprocess_data()
def preprocess_data(self):
# Pivot the data to create a user-song matrix
user_song_ratings = self.song_ratings.pivot_table(index='user_id', columns='song_id', values='rating')
user_song_ratings.fillna(0, inplace=True)
return user_song_ratings
def content_based_recommendations(self, user_id, num_recommendations=10):
user_songs = self.user_song_ratings.loc[user_id]
recommended_songs = user_songs.sort_values(ascending=False).head(num_recommendations)
return recommended_songs.index.tolist()
# Initialize the recommendation system
recommendation_system = RecommendationSystem("song_ratings_new.csv")
user_ids = [1]
for user_id in user_ids:
# Get content-based recommendations for each user
recommended_songs = recommendation_system.content_based_recommendations(user_id, num_recommendations=5)
if recommended_songs:
print(f"Recommended songs for user {user_id}:")
print(recommended_songs)
else:
print("No recommendations available for this user.")
self.l1.text = str(recommended_songs)
else:
print(f"No recommendations available for user {user_id}")
self.ids.play.opacity = 1
self.ids.pause.opacity = 1
self.ids.next.opacity = 1
self.ids.previous.opacity = 1
self.ids.current_audio_label.opacity = 1
audio_files = recommended_songs
static_part = 'wishlistData/'
self.audio_files = [static_part + file + '.mp3' for file in audio_files]
file_path = 'audio_files.txt'
# Open the file in write mode and write the audio files
with open(file_path, 'w') as file:
for item in self.audio_files:
file.write("%s\n" % item)
self.current_audio_index = 0
self.sound = None
if self.current_audio_index < len(self.audio_files):
self.sound = SoundLoader.load(self.audio_files[self.current_audio_index])
def play_audio(self):
if self.sound and self.sound.state == 'stop':
self.sound.play()
self.ids.current_audio_label.text = f"Current Audio: {self.audio_files[self.current_audio_index]}"
def play_next_audio(self):
if self.sound:
self.sound.stop()
self.current_audio_index += 1
if self.current_audio_index >= len(self.audio_files):
self.current_audio_index = 0
self.sound.unload()
self.sound = SoundLoader.load(self.audio_files[self.current_audio_index])
self.sound.play()
self.ids.current_audio_label.text = f"Current Audio: {self.audio_files[self.current_audio_index]}"
def pause_audio(self):
if self.sound and self.sound.state == 'play':
self.sound.stop()
def play_previous_audio(self):
if self.sound:
self.sound.stop()
self.current_audio_index -= 1
if self.current_audio_index < 0:
self.current_audio_index = len(self.audio_files) - 1
self.sound.unload()
self.sound = SoundLoader.load(self.audio_files[self.current_audio_index])
self.sound.play()
self.ids.current_audio_label.text = f"Current Audio: {self.audio_files[self.current_audio_index]}"
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment