|
import json |
|
from fastapi import FastAPI |
|
from starlette.middleware.sessions import SessionMiddleware |
|
from starlette.responses import HTMLResponse, RedirectResponse |
|
from starlette.requests import Request |
|
import gradio as gr |
|
import uvicorn |
|
from fastapi.responses import HTMLResponse |
|
from fastapi.responses import RedirectResponse |
|
|
|
import spotipy |
|
from spotipy import oauth2 |
|
|
|
PORT_NUMBER = 8080 |
|
SPOTIPY_CLIENT_ID = 'c087fa97cebb4f67b6f08ba841ed8378' |
|
SPOTIPY_CLIENT_SECRET = 'ae27d6916d114ac4bb948bb6c58a72d9' |
|
SPOTIPY_REDIRECT_URI = 'https://hf-hackathon-2023-01-spotify.hf.space' |
|
SCOPE = 'user-library-read' |
|
|
|
sp_oauth = oauth2.SpotifyOAuth(SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, SPOTIPY_REDIRECT_URI, scope=SCOPE) |
|
|
|
app = FastAPI() |
|
app.add_middleware(SessionMiddleware, secret_key="w.o.w") |
|
|
|
@app.get('/', response_class=HTMLResponse) |
|
async def homepage(request: Request): |
|
token = request.session.get('token') |
|
if token: |
|
return RedirectResponse("/gradio") |
|
|
|
url = str(request.url) |
|
code = sp_oauth.parse_response_code(url) |
|
if code != url: |
|
token_info = sp_oauth.get_access_token(code) |
|
request.session['token'] = token_info['access_token'] |
|
return RedirectResponse("/gradio") |
|
|
|
auth_url = sp_oauth.get_authorize_url() |
|
return "<a href='" + auth_url + "'>Login to Spotify</a>" |
|
|
|
|
|
|
|
from vega_datasets import data |
|
|
|
iris = data.iris() |
|
|
|
|
|
def scatter_plot_fn(dataset, request: gr.Request): |
|
token = request.request.session.get('token') |
|
if token: |
|
sp = spotipy.Spotify(token) |
|
results = sp.current_user() |
|
print(f"Welcome to Gradio, {name}!\n{results}") |
|
|
|
return gr.ScatterPlot( |
|
value=iris |
|
) |
|
|
|
|
|
|
|
def get_started(): |
|
|
|
|
|
return |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown(" ## Spotify Analyzer 🥳🎉") |
|
gr.Markdown("This app analyzes how cool your music taste is. We dare you to take this challenge!") |
|
with gr.Row(): |
|
get_started_btn = gr.Button("Get Started") |
|
with gr.Row(): |
|
with gr.Column(): |
|
with gr.Row(): |
|
with gr.Column(): |
|
plot = gr.ScatterPlot(show_label=False).style(container=True) |
|
with gr.Column(): |
|
plot = gr.ScatterPlot(show_label=False).style(container=True) |
|
with gr.Row(): |
|
with gr.Column(): |
|
plot = gr.ScatterPlot(show_label=False).style(container=True) |
|
with gr.Column(): |
|
plot = gr.ScatterPlot(show_label=False).style(container=True) |
|
with gr.Row(): |
|
gr.Markdown(" ### We have recommendations for you!") |
|
with gr.Row(): |
|
gr.Dataframe( |
|
headers=["Song", "Album", "Artist"], |
|
datatype=["str", "str", "str"], |
|
label="Reccomended Songs", |
|
) |
|
demo.load(fn=scatter_plot_fn, outputs=plot) |
|
|
|
|
|
|
|
gradio_app = gr.mount_gradio_app(app, demo, "/gradio") |
|
uvicorn.run(app, host="0.0.0.0", port=7860) |
|
|