sjw commited on
Commit
02831ce
·
1 Parent(s): ff393f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -32
app.py CHANGED
@@ -52,37 +52,54 @@
52
  # auth_button.click(spotify_auth, inputs=[client_id, client_secret, link], outputs=auth_result)
53
  # demo.launch()
54
 
 
 
 
55
  import gradio as gr
 
56
 
57
- secret_word = "gradio"
58
-
59
- with gr.Blocks() as demo:
60
- used_letters_var = gr.State([])
61
- with gr.Row() as row:
62
- with gr.Column():
63
- input_letter = gr.Textbox(label="Enter letter")
64
- btn = gr.Button("Guess Letter")
65
- with gr.Column():
66
- hangman = gr.Textbox(
67
- label="Hangman",
68
- value="_"*len(secret_word)
69
- )
70
- used_letters_box = gr.Textbox(label="Used Letters")
71
-
72
- def guess_letter(letter, used_letters):
73
- used_letters.append(letter)
74
- answer = "".join([
75
- (letter if letter in used_letters else "_")
76
- for letter in secret_word
77
- ])
78
- return {
79
- used_letters_var: used_letters,
80
- used_letters_box: ", ".join(used_letters),
81
- hangman: answer
82
- }
83
- btn.click(
84
- guess_letter,
85
- [input_letter, used_letters_var],
86
- [used_letters_var, used_letters_box, hangman]
87
- )
88
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  # auth_button.click(spotify_auth, inputs=[client_id, client_secret, link], outputs=auth_result)
53
  # demo.launch()
54
 
55
+ import os
56
+ import spotipy
57
+ from spotipy.oauth2 import SpotifyOAuth
58
  import gradio as gr
59
+ from urllib.parse import urlparse, parse_qs
60
 
61
+ redirect_uri = "https://huggingface.co/sjw"
62
+ scope= ['user-library-read',
63
+ 'user-read-playback-state',
64
+ 'user-modify-playback-state',
65
+ 'playlist-modify-public',
66
+ 'user-top-read']
67
+
68
+ with gr.Blocks() as demo:
69
+ used_tokens_var = gr.State([])
70
+
71
+ with gr.Row():
72
+ client_id = gr.Textbox(label="Client ID")
73
+ client_secret = gr.Textbox(label="Client Secret")
74
+ link_button = gr.Button("Get Link")
75
+ link_result = gr.Markdown()
76
+
77
+ link = gr.Textbox(label="Paste Link")
78
+ auth_button = gr.Button("Authorize")
79
+ auth_result = gr.Textbox()
80
+
81
+ def spotify_auth(client_id, client_secret, used_tokens, code=None):
82
+ auth_manager = SpotifyOAuth(client_id=client_id,
83
+ client_secret=client_secret,
84
+ redirect_uri=redirect_uri,
85
+ scope=scope)
86
+ if code:
87
+ parsed_url = urlparse(code)
88
+ code = parse_qs(parsed_url.query)['code'][0]
89
+
90
+ # EACH USER GETS A UNIQUE CODE
91
+ token = auth_manager.get_access_token(code=code, as_dict=False)
92
+ used_tokens.append(token)
93
+ dictionary = {
94
+ used_tokens_var: used_tokens,
95
+ }
96
+ print(dictionary)
97
+ return token
98
+ else:
99
+ auth_url = auth_manager.get_authorize_url()
100
+ return f"Please authorize the app by clicking [here]({auth_url}) and then paste the code below."
101
+
102
+ link_button.click(spotify_auth, inputs=[client_id, client_secret, used_tokens_var], outputs=link_result)
103
+ auth_button.click(spotify_auth, inputs=[client_id, client_secret, used_tokens_var, link], outputs=auth_result)
104
+
105
+ demo.launch()