Spaces:
Sleeping
Sleeping
from sklearn.metrics.pairwise import cosine_similarity | |
import pandas as pd | |
import numpy as np | |
from vectorization import spotify_data | |
import json | |
import gradio as gr | |
from gradio.components import Textbox | |
from ast import literal_eval | |
spotify_data_processed = pd.read_csv('C:\\Users\\34640\\Desktop\\Saturdays.ai\\spotify_dset\\dataset_modificado.csv') | |
def convert_string_to_array(str_vector): | |
# Si str_vector ya es un array de NumPy, devolverlo directamente | |
if isinstance(str_vector, np.ndarray): | |
return str_vector | |
try: | |
cleaned_str = str_vector.replace('[', '').replace(']', '').replace('\n', ' ').replace('\r', '').strip() | |
vector_elements = [float(item) for item in cleaned_str.split()] | |
return np.array(vector_elements) | |
except ValueError as e: | |
print("Error:", e) | |
return np.zeros((100,)) | |
spotify_data_processed['song_vector'] = spotify_data_processed['song_vector'].apply(convert_string_to_array) | |
# Aplicar la funci贸n a las primeras filas para ver los resultados | |
sample_data = spotify_data_processed['song_vector'].head() | |
converted_vectors = sample_data.apply(convert_string_to_array) | |
print(converted_vectors) | |
def recommend_song(song_name, artist_name, spotify_data_processed, top_n=4): | |
# Filtrar para encontrar la canci贸n espec铆fica | |
specific_song = spotify_data_processed[(spotify_data_processed['song'] == song_name) | |
& (spotify_data_processed['artist'] == artist_name)] | |
# Verificar si la canci贸n existe en el dataset | |
if specific_song.empty: | |
return pd.DataFrame({"Error": ["Canci贸n no encontrada en la base de datos."]}) | |
# Obtener el vector de la canci贸n espec铆fica | |
song_vec = specific_song['song_vector'].iloc[0] | |
# Asegurarte de que song_vec sea un array de NumPy | |
if isinstance(song_vec, str): | |
song_vec = convert_string_to_array(song_vec) | |
all_song_vectors = np.array(spotify_data_processed['song_vector'].tolist()) | |
# Calcular similitudes | |
similarities = cosine_similarity([song_vec], all_song_vectors)[0] | |
# Obtener los 铆ndices de las canciones m谩s similares | |
top_indices = np.argsort(similarities)[::-1][1:top_n+1] | |
# Devolver los nombres y artistas de las canciones m谩s similares | |
recommended_songs = spotify_data_processed.iloc[top_indices][['song', 'artist']] | |
return recommended_songs | |
def recommend_song_interface(song_name, artist_name): | |
recommendations_df = recommend_song(song_name, artist_name, spotify_data_processed) | |
if isinstance(recommendations_df, pd.DataFrame): | |
# Convierte el DataFrame en una lista de listas y luego a un formato de texto plano para la salida | |
recommendations_list = recommendations_df.values.tolist() | |
return ["{} by {}".format(song, artist) for song, artist in recommendations_list] | |
else: | |
# Si no es un DataFrame, devolver el mensaje de error | |
return recommendations_df | |
# Crear la interfaz con Gradio | |
iface = gr.Interface( | |
fn=recommend_song_interface, | |
inputs=[ | |
gr.Textbox(placeholder="Ingrese el t铆tulo de la canci贸n", label="T铆tulo de la Canci贸n"), | |
gr.Textbox(placeholder="Ingrese el nombre del artista", label="Nombre del Artista") | |
], | |
outputs=[gr.Text(label="Recomendaci贸n 1"), | |
gr.Text(label="Recomendaci贸n 2"), | |
gr.Text(label="Recomendaci贸n 3"), | |
gr.Text(label="Recomendaci贸n 4")], | |
title="Recomendador de Canciones", | |
description="Ingrese el t铆tulo de una canci贸n y el nombre del artista para obtener recomendaciones.", | |
theme="dark", # Comenta o elimina si el tema oscuro no est谩 disponible | |
css=""" | |
body {font-family: Arial, sans-serif;} | |
.input_text {background-color: #f0f0f0; border-radius: 5px;} | |
.output_text {border: 2px solid #f0f0f0; border-radius: 5px; padding: 10px;} | |
""" | |
) | |
iface.launch() | |