add pylast api to get track genre
Browse files- .gitignore +2 -1
- app.py +26 -9
- requirements.txt +3 -1
.gitignore
CHANGED
|
@@ -2,4 +2,5 @@
|
|
| 2 |
.ipynb_checkpoints/
|
| 3 |
catboost_info/
|
| 4 |
my_study.db
|
| 5 |
-
flagged/
|
|
|
|
|
|
| 2 |
.ipynb_checkpoints/
|
| 3 |
catboost_info/
|
| 4 |
my_study.db
|
| 5 |
+
flagged/
|
| 6 |
+
.cache
|
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import pandas as pd
|
|
| 3 |
import joblib
|
| 4 |
import os
|
| 5 |
import spotipy
|
|
|
|
| 6 |
from spotipy.oauth2 import SpotifyClientCredentials
|
| 7 |
from Levenshtein import distance
|
| 8 |
|
|
@@ -10,7 +11,7 @@ final_model = joblib.load('final_model.pkl')
|
|
| 10 |
print(final_model)
|
| 11 |
# Set up authentication with the Spotify API
|
| 12 |
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=os.environ['SPOT_API'], client_secret=os.environ['SPOT_SECRET']))
|
| 13 |
-
|
| 14 |
genre_list = ['acoustic', 'afrobeat', 'alt-rock', 'alternative', 'ambient',
|
| 15 |
'anime', 'black-metal', 'bluegrass', 'blues', 'brazil',
|
| 16 |
'breakbeat', 'british', 'cantopop', 'chicago-house', 'children',
|
|
@@ -32,11 +33,26 @@ genre_list = ['acoustic', 'afrobeat', 'alt-rock', 'alternative', 'ambient',
|
|
| 32 |
'spanish', 'study', 'swedish', 'synth-pop', 'tango', 'techno',
|
| 33 |
'trance', 'trip-hop', 'turkish', 'world-music']
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
track = sp.track(track_id)
|
| 37 |
artist = sp.artist(track['artists'][0]['external_urls']['spotify'])
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def find_most_similar_genre(my_genres, artist_genres):
|
| 42 |
min_distance = float('inf')
|
|
@@ -49,8 +65,8 @@ def find_most_similar_genre(my_genres, artist_genres):
|
|
| 49 |
most_similar_genre = my_genre
|
| 50 |
return most_similar_genre
|
| 51 |
|
| 52 |
-
def match_genres_to_list(track_id):
|
| 53 |
-
track_genres=get_track_genre(track_id)
|
| 54 |
return find_most_similar_genre(genre_list,track_genres)
|
| 55 |
|
| 56 |
def search_songs(query):
|
|
@@ -65,9 +81,10 @@ def get_song_features(song, track_ids):
|
|
| 65 |
index = int(song.split(".")[0])
|
| 66 |
track_id = track_ids[index]
|
| 67 |
track_info = sp.track(track_id)
|
| 68 |
-
|
|
|
|
| 69 |
features = sp.audio_features([track_id])[0]
|
| 70 |
-
genre = match_genres_to_list(track_id)
|
| 71 |
key_map = {0: 'C', 1: 'C#', 2: 'D', 3: 'D#', 4: 'E', 5: 'F', 6: 'F#', 7: 'G', 8: 'G#', 9: 'A', 10: 'A#', 11: 'B'}
|
| 72 |
key = str(key_map[features['key']])
|
| 73 |
mode_map = { 1: "Major", 0: "Minor"}
|
|
@@ -172,7 +189,7 @@ with gr.Blocks(theme=theme) as demo:
|
|
| 172 |
}
|
| 173 |
|
| 174 |
df = pd.DataFrame(data)
|
| 175 |
-
|
| 176 |
# Use your trained model to predict popularity based on the input features
|
| 177 |
if(final_model.predict(df)[0] == 1):
|
| 178 |
return ("<div style='display: flex; align-items: center;'><img src='file=images/pepe-jam.gif' alt='My gif 3' width='200' height='200'>" +
|
|
|
|
| 3 |
import joblib
|
| 4 |
import os
|
| 5 |
import spotipy
|
| 6 |
+
import pylast
|
| 7 |
from spotipy.oauth2 import SpotifyClientCredentials
|
| 8 |
from Levenshtein import distance
|
| 9 |
|
|
|
|
| 11 |
print(final_model)
|
| 12 |
# Set up authentication with the Spotify API
|
| 13 |
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=os.environ['SPOT_API'], client_secret=os.environ['SPOT_SECRET']))
|
| 14 |
+
network = pylast.LastFMNetwork(api_key=os.environ['LAST_API'], api_secret=os.environ['LAST_SECRET'])
|
| 15 |
genre_list = ['acoustic', 'afrobeat', 'alt-rock', 'alternative', 'ambient',
|
| 16 |
'anime', 'black-metal', 'bluegrass', 'blues', 'brazil',
|
| 17 |
'breakbeat', 'british', 'cantopop', 'chicago-house', 'children',
|
|
|
|
| 33 |
'spanish', 'study', 'swedish', 'synth-pop', 'tango', 'techno',
|
| 34 |
'trance', 'trip-hop', 'turkish', 'world-music']
|
| 35 |
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def get_track_genre(track_id,artist_name,track_name):
|
| 41 |
+
genres = []
|
| 42 |
track = sp.track(track_id)
|
| 43 |
artist = sp.artist(track['artists'][0]['external_urls']['spotify'])
|
| 44 |
+
|
| 45 |
+
album_id = track['album']['id']
|
| 46 |
+
|
| 47 |
+
album = sp.album(album_id)
|
| 48 |
+
genres.extend(album['genres'])
|
| 49 |
+
genres.extend(artist['genres'])
|
| 50 |
+
track = network.get_track(artist_name, track_name)
|
| 51 |
+
top_tags = track.get_top_tags(limit =5)
|
| 52 |
+
tags_list = [tag.item.get_name() for tag in top_tags]
|
| 53 |
+
genres.extend(tags_list)
|
| 54 |
+
print(genres)
|
| 55 |
+
return genres
|
| 56 |
|
| 57 |
def find_most_similar_genre(my_genres, artist_genres):
|
| 58 |
min_distance = float('inf')
|
|
|
|
| 65 |
most_similar_genre = my_genre
|
| 66 |
return most_similar_genre
|
| 67 |
|
| 68 |
+
def match_genres_to_list(track_id,artist_name,track_name):
|
| 69 |
+
track_genres=get_track_genre(track_id,artist_name,track_name)
|
| 70 |
return find_most_similar_genre(genre_list,track_genres)
|
| 71 |
|
| 72 |
def search_songs(query):
|
|
|
|
| 81 |
index = int(song.split(".")[0])
|
| 82 |
track_id = track_ids[index]
|
| 83 |
track_info = sp.track(track_id)
|
| 84 |
+
artist_name = track_info['artists'][0]['name']
|
| 85 |
+
track_name = track_info['name']
|
| 86 |
features = sp.audio_features([track_id])[0]
|
| 87 |
+
genre = match_genres_to_list(track_id,artist_name,track_name)
|
| 88 |
key_map = {0: 'C', 1: 'C#', 2: 'D', 3: 'D#', 4: 'E', 5: 'F', 6: 'F#', 7: 'G', 8: 'G#', 9: 'A', 10: 'A#', 11: 'B'}
|
| 89 |
key = str(key_map[features['key']])
|
| 90 |
mode_map = { 1: "Major", 0: "Minor"}
|
|
|
|
| 189 |
}
|
| 190 |
|
| 191 |
df = pd.DataFrame(data)
|
| 192 |
+
print(df)
|
| 193 |
# Use your trained model to predict popularity based on the input features
|
| 194 |
if(final_model.predict(df)[0] == 1):
|
| 195 |
return ("<div style='display: flex; align-items: center;'><img src='file=images/pepe-jam.gif' alt='My gif 3' width='200' height='200'>" +
|
requirements.txt
CHANGED
|
@@ -5,4 +5,6 @@ gradio
|
|
| 5 |
imblearn
|
| 6 |
category_encoders
|
| 7 |
catboost
|
| 8 |
-
scikit-learn
|
|
|
|
|
|
|
|
|
| 5 |
imblearn
|
| 6 |
category_encoders
|
| 7 |
catboost
|
| 8 |
+
scikit-learn
|
| 9 |
+
pylast
|
| 10 |
+
pandas < 2.0.0
|