Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import spotipy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
-
###########
|
| 6 |
|
| 7 |
from vega_datasets import data
|
| 8 |
|
|
@@ -49,4 +85,6 @@ with gr.Blocks() as demo:
|
|
| 49 |
demo.load(fn=scatter_plot_fn, outputs=plot)
|
| 50 |
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from starlette.middleware.sessions import SessionMiddleware
|
| 4 |
+
from starlette.responses import HTMLResponse, RedirectResponse
|
| 5 |
+
from starlette.requests import Request
|
| 6 |
import gradio as gr
|
| 7 |
+
import uvicorn
|
| 8 |
+
from fastapi.responses import HTMLResponse
|
| 9 |
+
from fastapi.responses import RedirectResponse
|
| 10 |
+
|
| 11 |
import spotipy
|
| 12 |
+
from spotipy import oauth2
|
| 13 |
+
|
| 14 |
+
PORT_NUMBER = 8080
|
| 15 |
+
SPOTIPY_CLIENT_ID = 'c087fa97cebb4f67b6f08ba841ed8378'
|
| 16 |
+
SPOTIPY_CLIENT_SECRET = 'ae27d6916d114ac4bb948bb6c58a72d9'
|
| 17 |
+
SPOTIPY_REDIRECT_URI = 'https://hf-hackathon-2023-01-spotify.hf.space'
|
| 18 |
+
SCOPE = 'user-library-read'
|
| 19 |
+
|
| 20 |
+
sp_oauth = oauth2.SpotifyOAuth(SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, SPOTIPY_REDIRECT_URI, scope=SCOPE)
|
| 21 |
+
|
| 22 |
+
app = FastAPI()
|
| 23 |
+
app.add_middleware(SessionMiddleware, secret_key="w.o.w")
|
| 24 |
+
|
| 25 |
+
@app.get('/', response_class=HTMLResponse)
|
| 26 |
+
async def homepage(request: Request):
|
| 27 |
+
token = request.session.get('token')
|
| 28 |
+
if token:
|
| 29 |
+
return RedirectResponse("/gradio")
|
| 30 |
+
|
| 31 |
+
url = str(request.url)
|
| 32 |
+
code = sp_oauth.parse_response_code(url)
|
| 33 |
+
if code != url:
|
| 34 |
+
token_info = sp_oauth.get_access_token(code)
|
| 35 |
+
request.session['token'] = token_info['access_token']
|
| 36 |
+
return RedirectResponse("/gradio")
|
| 37 |
+
|
| 38 |
+
auth_url = sp_oauth.get_authorize_url()
|
| 39 |
+
return "<a href='" + auth_url + "'>Login to Spotify</a>"
|
| 40 |
|
| 41 |
|
|
|
|
| 42 |
|
| 43 |
from vega_datasets import data
|
| 44 |
|
|
|
|
| 85 |
demo.load(fn=scatter_plot_fn, outputs=plot)
|
| 86 |
|
| 87 |
|
| 88 |
+
|
| 89 |
+
gradio_app = gr.mount_gradio_app(app, demo, "/gradio")
|
| 90 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|