Spaces:
Sleeping
Sleeping
fbottarelli commited on
Commit ·
ece69fb
1
Parent(s): 1657c0d
Added dependencies
Browse files- streamlit_standalone.py +135 -0
streamlit_standalone.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import spotipy
|
| 3 |
+
from spotipy.oauth2 import SpotifyClientCredentials
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
# Carica le credenziali di Spotify
|
| 11 |
+
load_dotenv()
|
| 12 |
+
SPOTIPY_CLIENT_ID = os.getenv("SPOTIPY_CLIENT_ID")
|
| 13 |
+
SPOTIPY_CLIENT_SECRET = os.getenv("SPOTIPY_CLIENT_SECRET")
|
| 14 |
+
auth_manager = SpotifyClientCredentials(client_id=SPOTIPY_CLIENT_ID, client_secret=SPOTIPY_CLIENT_SECRET)
|
| 15 |
+
sp = spotipy.Spotify(auth_manager=auth_manager)
|
| 16 |
+
|
| 17 |
+
def get_artist_info(artist_name):
|
| 18 |
+
artist = sp.search(q=artist_name, type='artist')['artists']['items'][0]
|
| 19 |
+
return {
|
| 20 |
+
'name': artist['name'],
|
| 21 |
+
'id': artist['id'],
|
| 22 |
+
'followers': artist['followers']['total'],
|
| 23 |
+
'popularity': artist['popularity'],
|
| 24 |
+
'genres': artist['genres'],
|
| 25 |
+
'image': artist['images'][0]['url'] if artist['images'] else None
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
def get_top_tracks(artist_id):
|
| 29 |
+
results = sp.artist_top_tracks(artist_id)
|
| 30 |
+
tracks = results['tracks'][:10] # Limit to top 10 tracks
|
| 31 |
+
return [{
|
| 32 |
+
'name': track['name'],
|
| 33 |
+
'album': track['album']['name'],
|
| 34 |
+
'artist': track['artists'][0]['name'],
|
| 35 |
+
'release_date': track['album']['release_date'],
|
| 36 |
+
'popularity': track['popularity'],
|
| 37 |
+
'album_cover': track['album']['images'][0]['url'] if track['album']['images'] else None,
|
| 38 |
+
'danceability': sp.audio_features(track['id'])[0]['danceability'] if sp.audio_features(track['id']) else 0,
|
| 39 |
+
'loudness': sp.audio_features(track['id'])[0]['loudness'] if sp.audio_features(track['id']) else 0,
|
| 40 |
+
'energy': sp.audio_features(track['id'])[0]['energy'] if sp.audio_features(track['id']) else 0,
|
| 41 |
+
'valence': sp.audio_features(track['id'])[0]['valence'] if sp.audio_features(track['id']) else 0,
|
| 42 |
+
'tempo': sp.audio_features(track['id'])[0]['tempo'] if sp.audio_features(track['id']) else 0,
|
| 43 |
+
'artist_id': artist_id
|
| 44 |
+
} for track in tracks]
|
| 45 |
+
|
| 46 |
+
def plot_features_comparison(data_tracks, artist1_info, artist2_info):
|
| 47 |
+
df = pd.DataFrame(data_tracks)
|
| 48 |
+
fig, ax = plt.subplots()
|
| 49 |
+
colors = {artist1_info['id']: 'blue', artist2_info['id']: 'red'}
|
| 50 |
+
|
| 51 |
+
for artist_id, group in df.groupby('artist_id'):
|
| 52 |
+
ax.scatter(group['danceability'], group['loudness'], alpha=0.7, label=group['artist'].iloc[0], c=colors[artist_id])
|
| 53 |
+
|
| 54 |
+
plt.title('Confronto Danceability vs. Loudness')
|
| 55 |
+
plt.xlabel('Danceability')
|
| 56 |
+
plt.ylabel('Loudness (dB)')
|
| 57 |
+
plt.legend(title='Artist')
|
| 58 |
+
st.pyplot(fig)
|
| 59 |
+
|
| 60 |
+
def plot_audio_features_boxplot(data_tracks, artist1_name, artist2_name):
|
| 61 |
+
df = pd.DataFrame(data_tracks)
|
| 62 |
+
fig, axs = plt.subplots(1, 4, figsize=(20, 5)) # 1 riga, 4 colonne per 4 features
|
| 63 |
+
features = ['danceability', 'energy', 'valence', 'tempo']
|
| 64 |
+
for i, feature in enumerate(features):
|
| 65 |
+
ax = axs[i]
|
| 66 |
+
artist1_data = df[df['artist'] == artist1_name][feature]
|
| 67 |
+
artist2_data = df[df['artist'] == artist2_name][feature]
|
| 68 |
+
ax.boxplot([artist1_data, artist2_data], labels=[artist1_name, artist2_name])
|
| 69 |
+
ax.set_title(f'Distribution of {feature}')
|
| 70 |
+
ax.set_ylabel(feature)
|
| 71 |
+
|
| 72 |
+
plt.tight_layout()
|
| 73 |
+
st.pyplot(fig)
|
| 74 |
+
|
| 75 |
+
def plot_audio_features_violinplot(data_tracks, artist1_name, artist2_name):
|
| 76 |
+
df = pd.DataFrame(data_tracks)
|
| 77 |
+
fig, axs = plt.subplots(1, 4, figsize=(20, 5)) # 1 riga, 4 colonne per 4 features
|
| 78 |
+
features = ['danceability', 'energy', 'valence', 'tempo']
|
| 79 |
+
for i, feature in enumerate(features):
|
| 80 |
+
ax = axs[i]
|
| 81 |
+
data_to_plot = [df[df['artist'] == artist1_name][feature], df[df['artist'] == artist2_name][feature]]
|
| 82 |
+
ax.violinplot(data_to_plot)
|
| 83 |
+
ax.set_xticks([1, 2])
|
| 84 |
+
ax.set_xticklabels([artist1_name, artist2_name])
|
| 85 |
+
ax.set_title(f'Distribution of {feature}')
|
| 86 |
+
ax.set_ylabel(feature)
|
| 87 |
+
|
| 88 |
+
plt.tight_layout()
|
| 89 |
+
st.pyplot(fig)
|
| 90 |
+
|
| 91 |
+
def show_average_features(data_tracks):
|
| 92 |
+
df = pd.DataFrame(data_tracks)
|
| 93 |
+
numeric_columns = ['loudness', 'danceability', 'energy', 'valence', 'tempo', 'artist']
|
| 94 |
+
df = df[numeric_columns]
|
| 95 |
+
average_features = df.groupby('artist').mean()[['loudness', 'danceability', 'energy', 'valence', 'tempo']]
|
| 96 |
+
st.write("Media delle Caratteristiche Audio per Artista:")
|
| 97 |
+
st.dataframe(average_features)
|
| 98 |
+
|
| 99 |
+
def main():
|
| 100 |
+
st.title("Confronto Artisti su Spotify")
|
| 101 |
+
|
| 102 |
+
artist1_name = st.text_input("Inserisci il nome del primo artista")
|
| 103 |
+
artist2_name = st.text_input("Inserisci il nome del secondo artista")
|
| 104 |
+
|
| 105 |
+
if st.button("Confronta"):
|
| 106 |
+
if artist1_name and artist2_name:
|
| 107 |
+
artist1_info = get_artist_info(artist1_name)
|
| 108 |
+
artist2_info = get_artist_info(artist2_name)
|
| 109 |
+
artist1_tracks = get_top_tracks(artist1_info['id'])
|
| 110 |
+
artist2_tracks = get_top_tracks(artist2_info['id'])
|
| 111 |
+
data_tracks = artist1_tracks + artist2_tracks
|
| 112 |
+
|
| 113 |
+
col1, col2 = st.columns(2)
|
| 114 |
+
with col1:
|
| 115 |
+
st.image(artist1_info['image'], caption=artist1_info['name'])
|
| 116 |
+
st.markdown(f"**Nome:** {artist1_info['name']}")
|
| 117 |
+
st.markdown(f"**Follower:** {artist1_info['followers']:,}")
|
| 118 |
+
st.markdown(f"**Popolarità:** {artist1_info['popularity']}")
|
| 119 |
+
st.markdown(f"**Generi:** {', '.join(artist1_info['genres'])}")
|
| 120 |
+
with col2:
|
| 121 |
+
st.image(artist2_info['image'], caption=artist2_info['name'])
|
| 122 |
+
st.markdown(f"**Nome:** {artist2_info['name']}")
|
| 123 |
+
st.markdown(f"**Follower:** {artist2_info['followers']:,}")
|
| 124 |
+
st.markdown(f"**Popolarità:** {artist2_info['popularity']}")
|
| 125 |
+
st.markdown(f"**Generi:** {', '.join(artist2_info['genres'])}")
|
| 126 |
+
show_average_features(data_tracks)
|
| 127 |
+
plot_features_comparison(data_tracks, artist1_info, artist2_info)
|
| 128 |
+
plot_audio_features_boxplot(data_tracks, artist1_name, artist2_name)
|
| 129 |
+
plot_audio_features_violinplot(data_tracks, artist1_name, artist2_name)
|
| 130 |
+
|
| 131 |
+
else:
|
| 132 |
+
st.error("Per favore inserisci i nomi di entrambi gli artisti.")
|
| 133 |
+
|
| 134 |
+
if __name__ == "__main__":
|
| 135 |
+
main()
|