Songify / app.py
Sakil's picture
Update app.py
8924b87
import pandas as pd
import numpy as np
from scipy import spatial
from sentence_transformers import SentenceTransformer
import gradio as gr
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
def get_data():
lyrics_data=pd.read_csv('Lyrics.csv')
lyrics_data=lyrics_data.assign(embeddings=(lyrics_data['Lyric'].astype('str')).apply(lambda x: model.encode(x)))
lyrics_data=lyrics_data.drop(columns=['Unnamed: 0'],axis=1)
lyrics_data.dropna(inplace=True)
return lyrics_data
def closest_lyrics(Songpreference):
data=get_data()
inp_vector=model.encode(Songpreference)
s=data['embeddings'].apply(lambda x: 1 - spatial.distance.cosine(x, inp_vector) )
data=data.assign(similarity=s)
#data=data[['Artist','Title ','Album','Date',' Lyric','Year','similarity']]
data=data.sort_values('similarity',ascending=False).head(5)
data=data.drop(columns=['embeddings','similarity','Date','Year'],axis=1)
return data
iface = gr.Interface(
closest_lyrics,inputs=["text"],
outputs=["dataframe"],
examples=[["Romantic"],["Sad"],["workout"],["The Hello Katy"],["Freshness"]],
#css="https://www.w3schools.com/cssref/",
title='Songify',
description="Songify is a software tool designed to suggest personalized music playlists to users based on their preference and the Users can also discover new music and explore different genres through the app's recommendations. The goal of Songify is to help users find the perfect music for any mood or occasion quickly and effortlessly. ")
iface.launch(inline=False)