Spaces:
Sleeping
Sleeping
Initial commit
Browse files- app.py +108 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
from collections import Counter, defaultdict
|
4 |
+
import os
|
5 |
+
|
6 |
+
def analyze_spotify_data(files):
|
7 |
+
# files: list of file objects
|
8 |
+
# We'll parse each JSON file and aggregate the data
|
9 |
+
|
10 |
+
all_records = []
|
11 |
+
for f in files:
|
12 |
+
try:
|
13 |
+
data = json.load(open(f))
|
14 |
+
if isinstance(data, list):
|
15 |
+
all_records.extend(data)
|
16 |
+
else:
|
17 |
+
# If the JSON file isn't a list at top-level, skip or handle differently
|
18 |
+
continue
|
19 |
+
except:
|
20 |
+
# If there's an error in loading JSON, skip that file
|
21 |
+
continue
|
22 |
+
|
23 |
+
# If no valid data found
|
24 |
+
if not all_records:
|
25 |
+
return "No valid data found in the uploaded files."
|
26 |
+
|
27 |
+
# Aggregate listening stats
|
28 |
+
artist_counter = Counter()
|
29 |
+
track_counter = Counter()
|
30 |
+
album_counter = Counter() # Note: album info not provided in the sample data, will only work if album is in the data
|
31 |
+
|
32 |
+
# We also want to consider total listening time per artist/track/album
|
33 |
+
artist_time = defaultdict(int)
|
34 |
+
track_time = defaultdict(int)
|
35 |
+
album_time = defaultdict(int)
|
36 |
+
|
37 |
+
# Attempt to detect if albumName is present
|
38 |
+
album_present = all("albumName" in record for record in all_records if isinstance(record, dict))
|
39 |
+
|
40 |
+
for record in all_records:
|
41 |
+
if not isinstance(record, dict):
|
42 |
+
continue
|
43 |
+
artist = record.get("artistName", "Unknown Artist")
|
44 |
+
track = record.get("trackName", "Unknown Track")
|
45 |
+
ms_played = record.get("msPlayed", 0)
|
46 |
+
# Album may not be present; handle gracefully
|
47 |
+
album = record.get("albumName", "Unknown Album") if album_present else None
|
48 |
+
|
49 |
+
artist_counter[artist] += 1
|
50 |
+
track_counter[track] += 1
|
51 |
+
artist_time[artist] += ms_played
|
52 |
+
track_time[track] += ms_played
|
53 |
+
|
54 |
+
if album_present and album is not None:
|
55 |
+
album_counter[album] += 1
|
56 |
+
album_time[album] += ms_played
|
57 |
+
|
58 |
+
# Determine top artists by number of tracks played (frequency) and also by time
|
59 |
+
top_artists_by_count = artist_counter.most_common(10)
|
60 |
+
top_artists_by_time = sorted(artist_time.items(), key=lambda x: x[1], reverse=True)[:10]
|
61 |
+
|
62 |
+
# Determine top tracks by frequency and by time
|
63 |
+
top_tracks_by_count = track_counter.most_common(10)
|
64 |
+
top_tracks_by_time = sorted(track_time.items(), key=lambda x: x[1], reverse=True)[:10]
|
65 |
+
|
66 |
+
# Determine top albums if available
|
67 |
+
if album_present:
|
68 |
+
top_albums_by_count = album_counter.most_common(10)
|
69 |
+
top_albums_by_time = sorted(album_time.items(), key=lambda x: x[1], reverse=True)[:10]
|
70 |
+
else:
|
71 |
+
top_albums_by_count = [("No album data found", 0)]
|
72 |
+
top_albums_by_time = [("No album data found", 0)]
|
73 |
+
|
74 |
+
# Format the results into a readable output
|
75 |
+
def format_list(title, data_list, time_data=False):
|
76 |
+
result = f"**{title}**\n"
|
77 |
+
if not time_data:
|
78 |
+
for i, (name, count) in enumerate(data_list, 1):
|
79 |
+
result += f"{i}. {name} ({count} plays)\n"
|
80 |
+
else:
|
81 |
+
for i, (name, ms) in enumerate(data_list, 1):
|
82 |
+
hours = ms / (1000*60*60)
|
83 |
+
result += f"{i}. {name} ({hours:.2f} hours)\n"
|
84 |
+
result += "\n"
|
85 |
+
return result
|
86 |
+
|
87 |
+
output = ""
|
88 |
+
output += format_list("Top Artists by Play Count", top_artists_by_count, time_data=False)
|
89 |
+
output += format_list("Top Artists by Listening Time", top_artists_by_time, time_data=True)
|
90 |
+
output += format_list("Top Tracks by Play Count", top_tracks_by_count, time_data=False)
|
91 |
+
output += format_list("Top Tracks by Listening Time", top_tracks_by_time, time_data=True)
|
92 |
+
output += format_list("Top Albums by Play Count", top_albums_by_count, time_data=False)
|
93 |
+
output += format_list("Top Albums by Listening Time", top_albums_by_time, time_data=True)
|
94 |
+
|
95 |
+
return output
|
96 |
+
|
97 |
+
with gr.Blocks() as demo:
|
98 |
+
gr.Markdown("# Spotify Listening Data Analyzer")
|
99 |
+
gr.Markdown("Upload your Spotify JSON files (e.g., 'StreamingHistory0.json', 'StreamingHistory1.json', etc.) to get an overview of your top artists, albums, and tracks.")
|
100 |
+
|
101 |
+
file_input = gr.File(file_count="multiple", type="filepath", label="Upload JSON files")
|
102 |
+
analyze_button = gr.Button("Analyze")
|
103 |
+
output_box = gr.Markdown()
|
104 |
+
|
105 |
+
analyze_button.click(fn=analyze_spotify_data, inputs=file_input, outputs=output_box)
|
106 |
+
|
107 |
+
if __name__ == "__main__":
|
108 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
gradio
|