jisaacso219 commited on
Commit
5709789
Β·
verified Β·
1 Parent(s): 4bbe919

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -4,13 +4,13 @@ import gradio as gr
4
  import spotipy
5
  from spotipy.oauth2 import SpotifyOAuth
6
 
7
- # Set credentials from environment (Hugging Face Secrets)
8
  CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"]
9
  CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"]
10
  REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/"
11
  SCOPE = "user-read-playback-state user-modify-playback-state playlist-read-private"
12
 
13
- # Initialize Spotify auth
14
  sp_oauth = SpotifyOAuth(
15
  client_id=CLIENT_ID,
16
  client_secret=CLIENT_SECRET,
@@ -46,7 +46,7 @@ def try_login(current_url):
46
  playlists = sp.current_user_playlists(limit=50)["items"]
47
  user_playlists = {p["name"]: p["id"] for p in playlists}
48
 
49
- # Get devices
50
  devices = sp.devices()["devices"]
51
  device_map = {d["name"]: d["id"] for d in devices}
52
 
@@ -72,6 +72,7 @@ def shuffle_and_play(playlist_name, device_name):
72
  if not pid or not device_id:
73
  return "❌ Invalid playlist or device selection."
74
 
 
75
  tracks = []
76
  res = sp.playlist_tracks(pid)
77
  tracks.extend(res["items"])
@@ -83,7 +84,8 @@ def shuffle_and_play(playlist_name, device_name):
83
  random.shuffle(uris)
84
 
85
  try:
86
- sp.start_playback(device_id=device_id, uris=uris)
 
87
  return f"▢️ Now playing: **{playlist_name}** on **{device_name}**"
88
  except Exception as e:
89
  return f"❌ Playback failed: {str(e)}"
@@ -95,9 +97,9 @@ with gr.Blocks() as demo:
95
 
96
  url_state = gr.Textbox(visible=False)
97
  status = gr.Markdown()
98
- playlist_dd = gr.Dropdown(label="Select a Playlist", visible=False)
99
- device_dd = gr.Dropdown(label="Select a Device", visible=False)
100
- shuffle_btn = gr.Button("πŸ”€ Shuffle & Play", visible=False)
101
  result = gr.Markdown()
102
 
103
  demo.load(
@@ -112,3 +114,4 @@ with gr.Blocks() as demo:
112
  if __name__ == "__main__":
113
  demo.launch(server_name="0.0.0.0", ssr_mode=False)
114
 
 
 
4
  import spotipy
5
  from spotipy.oauth2 import SpotifyOAuth
6
 
7
+ # Spotify credentials (set in HF Spaces Secrets)
8
  CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"]
9
  CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"]
10
  REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/"
11
  SCOPE = "user-read-playback-state user-modify-playback-state playlist-read-private"
12
 
13
+ # OAuth setup
14
  sp_oauth = SpotifyOAuth(
15
  client_id=CLIENT_ID,
16
  client_secret=CLIENT_SECRET,
 
46
  playlists = sp.current_user_playlists(limit=50)["items"]
47
  user_playlists = {p["name"]: p["id"] for p in playlists}
48
 
49
+ # Get active devices
50
  devices = sp.devices()["devices"]
51
  device_map = {d["name"]: d["id"] for d in devices}
52
 
 
72
  if not pid or not device_id:
73
  return "❌ Invalid playlist or device selection."
74
 
75
+ # Fetch and shuffle tracks
76
  tracks = []
77
  res = sp.playlist_tracks(pid)
78
  tracks.extend(res["items"])
 
84
  random.shuffle(uris)
85
 
86
  try:
87
+ # Limit to first 100 tracks to avoid 413 error
88
+ sp.start_playback(device_id=device_id, uris=uris[:100])
89
  return f"▢️ Now playing: **{playlist_name}** on **{device_name}**"
90
  except Exception as e:
91
  return f"❌ Playback failed: {str(e)}"
 
97
 
98
  url_state = gr.Textbox(visible=False)
99
  status = gr.Markdown()
100
+ playlist_dd = gr.Dropdown(label="Step 2: Select a Playlist", visible=False)
101
+ device_dd = gr.Dropdown(label="Step 3: Select a Device", visible=False)
102
+ shuffle_btn = gr.Button("πŸ”€ Step 4: Shuffle & Play", visible=False)
103
  result = gr.Markdown()
104
 
105
  demo.load(
 
114
  if __name__ == "__main__":
115
  demo.launch(server_name="0.0.0.0", ssr_mode=False)
116
 
117
+