huathedev commited on
Commit
2a81672
·
1 Parent(s): 3ad83d0

Delete 01_Recommend_from_Song🎤.py

Browse files
Files changed (1) hide show
  1. 01_Recommend_from_Song🎤.py +0 -147
01_Recommend_from_Song🎤.py DELETED
@@ -1,147 +0,0 @@
1
- import spotipy
2
- import streamlit as st
3
- import numpy as np
4
- from spotipy.oauth2 import SpotifyClientCredentials
5
- from PIL import Image
6
- import requests
7
- import pandas as pd
8
-
9
- st.set_page_config(
10
- page_title="Find Songs Similar to Yours🎤", page_icon="🎤", layout="wide"
11
- )
12
-
13
- # Spotify API
14
- SPOTIPY_CLIENT_ID = st.secrets.spot_creds.spot_client_id
15
- SPOTIPY_CLIENT_SECRET = st.secrets.spot_creds.spot_client_secret
16
-
17
- sp = spotipy.Spotify(
18
- auth_manager=SpotifyClientCredentials(
19
- client_id=SPOTIPY_CLIENT_ID, client_secret=SPOTIPY_CLIENT_SECRET
20
- )
21
- )
22
-
23
- """
24
- # Analyze Song and Get Recommendations🎤
25
-
26
- Input a song title and the app will return recommendations as well as the features of the song.
27
-
28
- Data is obtained using the Python library [Spotipy](https://spotipy.readthedocs.io/en/2.18.0/) that uses [Spotify Web API.](https://developer.spotify.com/documentation/web-api/)
29
-
30
- """
31
- song = st.text_input("Enter a song title", value="Somebody Else")
32
- search = sp.search(q="track:" + song, type="track")
33
-
34
-
35
- class GetSongInfo:
36
- def __init__(self, search):
37
- self.search = search
38
-
39
- def song_id(self):
40
- song_id = search["tracks"]["items"][0]["id"] # -gets song id
41
- return song_id
42
-
43
- def song_album(self):
44
- song_album = search["tracks"]["items"][0]["album"][
45
- "name"
46
- ] # -gets song album name
47
- return song_album
48
-
49
- def song_image(self):
50
- song_image = search["tracks"]["items"][0]["album"]["images"][0][
51
- "url"
52
- ] # -gets song image URL
53
- return song_image
54
-
55
- def song_artist_name(self):
56
- song_artist_name = search["tracks"]["items"][0]["artists"][0][
57
- "name"
58
- ] # -gets artist for song
59
- return song_artist_name
60
-
61
- def song_name(self):
62
- song_name = search["tracks"]["items"][0]["name"] # -gets song name
63
- return song_name
64
-
65
- def song_preview(self):
66
- song_preview = search["tracks"]["items"][0]["preview_url"]
67
- return song_preview
68
-
69
-
70
- songs = GetSongInfo(song)
71
-
72
- ###
73
-
74
-
75
- def url(song):
76
- url_to_song = "https://open.spotify.com/track/" + songs.song_id()
77
- st.write(
78
- f"Link to stream '{songs.song_name()}' by {songs.song_artist_name()} on Spotify: {url_to_song}"
79
- )
80
-
81
-
82
- # Set up two-column layout for Streamlit app
83
- image, stats = st.columns(2)
84
-
85
- with image:
86
- try:
87
- url(song)
88
- r = requests.get(songs.song_image())
89
- open("img/" + songs.song_id() + ".jpg", "w+b").write(r.content)
90
- image_album = Image.open("img/" + songs.song_id() + ".jpg")
91
- st.image(
92
- image_album,
93
- caption=f"{songs.song_artist_name()} - {songs.song_album()}",
94
- use_column_width="auto",
95
- )
96
-
97
- feat = sp.audio_features(tracks=[songs.song_id()])
98
- features = feat[0]
99
- p = pd.Series(features).to_frame()
100
- data_feat = p.loc[
101
- [
102
- "acousticness",
103
- "danceability",
104
- "energy",
105
- "liveness",
106
- "speechiness",
107
- "valence",
108
- ]
109
- ]
110
- bpm = p.loc[["tempo"]]
111
- values = bpm.values[0]
112
- bpms = values.item()
113
- ticks = np.linspace(0, 1, 11)
114
-
115
- plot = data_feat.plot.barh(
116
- xticks=ticks, legend=False, color="limegreen"
117
- ) # Use Pandas plot
118
- plot.set_xlabel("Value")
119
- plot.set_ylabel("Parameters")
120
- plot.set_title(f"Analysing '{songs.song_name()}' by {songs.song_artist_name()}")
121
- plot.invert_yaxis()
122
- st.pyplot(plot.figure)
123
- st.subheader(f"BPM (Beats Per Minute): {bpms}")
124
-
125
- st.warning(
126
- "Note: Audio previews may have very high default volume and will reset after page refresh"
127
- )
128
- st.audio(songs.song_preview(), format="audio/wav")
129
-
130
- except IndexError or NameError:
131
- st.error(
132
- "This error is possibly due to the API being unable to find the song. Maybe try to retype it using the song title followed by artist without any hyphens (e.g. In my Blood Shawn Mendes)"
133
- )
134
-
135
- # Recommendations
136
- with stats:
137
- st.subheader("You might also like")
138
-
139
- reco = sp.recommendations(
140
- seed_artists=None, seed_tracks=[songs.song_id()], seed_genres=[], limit=10
141
- )
142
-
143
- for i in reco["tracks"]:
144
- st.write(f"\"{i['name']}\" - {i['artists'][0]['name']}")
145
- image_reco = requests.get(i["album"]["images"][2]["url"])
146
- open("img/" + i["id"] + ".jpg", "w+b").write(image_reco.content)
147
- st.image(Image.open("img/" + i["id"] + ".jpg"))