Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -59,14 +59,22 @@ def get_audio_features(token, track_ids):
|
|
| 59 |
url = 'https://api.spotify.com/v1/audio-features'
|
| 60 |
headers = {'Authorization': f'Bearer {token}'}
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
for i in range(0, len(track_ids), 100):
|
| 63 |
batch_ids = track_ids[i:i+100]
|
| 64 |
params = {'ids': ','.join(batch_ids)}
|
| 65 |
response = make_request_with_retry(url, headers, params)
|
| 66 |
if response:
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
audio_features[feature['id']] = feature
|
|
|
|
|
|
|
|
|
|
| 70 |
return audio_features
|
| 71 |
|
| 72 |
def get_tracks_and_features(token, url):
|
|
@@ -74,14 +82,34 @@ def get_tracks_and_features(token, url):
|
|
| 74 |
track_ids = []
|
| 75 |
|
| 76 |
if "track" in url:
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
elif "playlist" in url:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
if not track_ids:
|
| 87 |
return None, None
|
|
@@ -144,15 +172,35 @@ def interface(project_name, spotify_url, num_similar_songs=10):
|
|
| 144 |
error_message = "Invalid URL format. Please enter a valid Spotify track or playlist URL."
|
| 145 |
# Return empty DataFrame with error message and None for file
|
| 146 |
return gr.Dataframe(value=pd.DataFrame({"Error": [error_message]})), None
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
token_spotify = get_token(client_ids[current_api_index], client_secrets[current_api_index])
|
| 149 |
if not token_spotify:
|
| 150 |
error_message = "Failed to authenticate with Spotify API. Please try again later."
|
| 151 |
return gr.Dataframe(value=pd.DataFrame({"Error": [error_message]})), None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
return gr.Dataframe(value=pd.DataFrame({"Error": [error_message]})), None
|
| 157 |
|
| 158 |
similar_tracks_info = find_similar_tracks(token_spotify, audio_features, num_similar_songs)
|
|
|
|
| 59 |
url = 'https://api.spotify.com/v1/audio-features'
|
| 60 |
headers = {'Authorization': f'Bearer {token}'}
|
| 61 |
|
| 62 |
+
# Print track IDs for debugging
|
| 63 |
+
print(f"Getting audio features for {len(track_ids)} tracks")
|
| 64 |
+
|
| 65 |
for i in range(0, len(track_ids), 100):
|
| 66 |
batch_ids = track_ids[i:i+100]
|
| 67 |
params = {'ids': ','.join(batch_ids)}
|
| 68 |
response = make_request_with_retry(url, headers, params)
|
| 69 |
if response:
|
| 70 |
+
features_list = response.json().get('audio_features', [])
|
| 71 |
+
print(f"Received {len(features_list)} audio features")
|
| 72 |
+
for feature in features_list:
|
| 73 |
+
if feature and 'id' in feature:
|
| 74 |
audio_features[feature['id']] = feature
|
| 75 |
+
|
| 76 |
+
# Print how many valid features we found
|
| 77 |
+
print(f"Found {len(audio_features)} valid audio features")
|
| 78 |
return audio_features
|
| 79 |
|
| 80 |
def get_tracks_and_features(token, url):
|
|
|
|
| 82 |
track_ids = []
|
| 83 |
|
| 84 |
if "track" in url:
|
| 85 |
+
# Handle various URL formats
|
| 86 |
+
parts = url.split("/")
|
| 87 |
+
for part in parts:
|
| 88 |
+
if part and ("?" in part):
|
| 89 |
+
track_id = part.split("?")[0]
|
| 90 |
+
track_ids = [track_id]
|
| 91 |
+
break
|
| 92 |
+
if not track_ids and len(parts) > 0:
|
| 93 |
+
potential_id = parts[-1]
|
| 94 |
+
if potential_id:
|
| 95 |
+
track_ids = [potential_id]
|
| 96 |
elif "playlist" in url:
|
| 97 |
+
# Handle various URL formats
|
| 98 |
+
parts = url.split("/")
|
| 99 |
+
playlist_id = None
|
| 100 |
+
for part in parts:
|
| 101 |
+
if part and ("?" in part):
|
| 102 |
+
playlist_id = part.split("?")[0]
|
| 103 |
+
break
|
| 104 |
+
if not playlist_id and len(parts) > 0:
|
| 105 |
+
playlist_id = parts[-1]
|
| 106 |
+
|
| 107 |
+
if playlist_id:
|
| 108 |
+
tracks_url = f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks'
|
| 109 |
+
response = make_request_with_retry(tracks_url, headers)
|
| 110 |
+
if response and response.json().get('items'):
|
| 111 |
+
track_ids = [item['track']['id'] for item in response.json().get('items', [])
|
| 112 |
+
if item.get('track') and item['track'].get('id')]
|
| 113 |
|
| 114 |
if not track_ids:
|
| 115 |
return None, None
|
|
|
|
| 172 |
error_message = "Invalid URL format. Please enter a valid Spotify track or playlist URL."
|
| 173 |
# Return empty DataFrame with error message and None for file
|
| 174 |
return gr.Dataframe(value=pd.DataFrame({"Error": [error_message]})), None
|
| 175 |
+
|
| 176 |
+
# Log the URL for debugging
|
| 177 |
+
print(f"Processing Spotify URL: {spotify_url}")
|
| 178 |
|
| 179 |
token_spotify = get_token(client_ids[current_api_index], client_secrets[current_api_index])
|
| 180 |
if not token_spotify:
|
| 181 |
error_message = "Failed to authenticate with Spotify API. Please try again later."
|
| 182 |
return gr.Dataframe(value=pd.DataFrame({"Error": [error_message]})), None
|
| 183 |
+
|
| 184 |
+
print(f"Successfully obtained token")
|
| 185 |
+
|
| 186 |
+
try:
|
| 187 |
+
track_ids, audio_features = get_tracks_and_features(token_spotify, spotify_url)
|
| 188 |
+
print(f"Track IDs: {track_ids[:5]}{'...' if len(track_ids) > 5 else ''}")
|
| 189 |
|
| 190 |
+
if not track_ids:
|
| 191 |
+
error_message = "No valid tracks found for the provided URL. Please check the URL and try again."
|
| 192 |
+
return gr.Dataframe(value=pd.DataFrame({"Error": [error_message]})), None
|
| 193 |
+
|
| 194 |
+
if not audio_features:
|
| 195 |
+
error_message = "Could not retrieve audio features for the tracks. Please try again."
|
| 196 |
+
return gr.Dataframe(value=pd.DataFrame({"Error": [error_message]})), None
|
| 197 |
+
|
| 198 |
+
if len(audio_features) == 0:
|
| 199 |
+
error_message = "Received empty audio features. The track might not be available in your region."
|
| 200 |
+
return gr.Dataframe(value=pd.DataFrame({"Error": [error_message]})), None
|
| 201 |
+
except Exception as e:
|
| 202 |
+
error_message = f"An error occurred: {str(e)}"
|
| 203 |
+
print(f"Error: {str(e)}")
|
| 204 |
return gr.Dataframe(value=pd.DataFrame({"Error": [error_message]})), None
|
| 205 |
|
| 206 |
similar_tracks_info = find_similar_tracks(token_spotify, audio_features, num_similar_songs)
|