Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,108 +1,33 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import requests
|
| 3 |
-
import pandas as pd
|
| 4 |
-
|
| 5 |
-
# —————— PARCHE Pydantic para FastAPI ——————
|
| 6 |
-
import pydantic
|
| 7 |
-
pydantic.Schema = pydantic.Field
|
| 8 |
-
|
| 9 |
-
# —————— PARCHE GRADIO_CLIENT BOOLEANS ——————
|
| 10 |
-
import gradio_client.utils as client_utils
|
| 11 |
-
_orig_get_type = client_utils.get_type
|
| 12 |
-
|
| 13 |
-
def patched_get_type(schema):
|
| 14 |
-
if not isinstance(schema, dict):
|
| 15 |
-
return "Any"
|
| 16 |
-
return _orig_get_type(schema)
|
| 17 |
-
client_utils.get_type = patched_get_type
|
| 18 |
-
# ————————————————————————————————
|
| 19 |
-
|
| 20 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# Configuración Spotify (Client Credentials)
|
| 23 |
-
CLIENT_ID = os.getenv("SPOTIFY_CLIENT_IDS", "").split(',')[0]
|
| 24 |
-
CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRETS", "").split(',')[0]
|
| 25 |
-
|
| 26 |
-
# Función para obtener token
|
| 27 |
-
def get_token():
|
| 28 |
-
resp = requests.post(
|
| 29 |
-
"https://accounts.spotify.com/api/token",
|
| 30 |
-
data={"grant_type": "client_credentials"},
|
| 31 |
-
auth=(CLIENT_ID, CLIENT_SECRET)
|
| 32 |
-
)
|
| 33 |
-
resp.raise_for_status()
|
| 34 |
-
return resp.json()["access_token"]
|
| 35 |
-
|
| 36 |
-
# Extrae el ID de playlist de la URL
|
| 37 |
-
def extract_pid(url):
|
| 38 |
-
return url.strip().rstrip('/').split('/')[-1].split('?')[0]
|
| 39 |
-
|
| 40 |
-
# Caché de géneros por artista
|
| 41 |
-
_artist_cache = {}
|
| 42 |
-
|
| 43 |
-
def get_artist_genres(aid, headers):
|
| 44 |
-
if aid in _artist_cache:
|
| 45 |
-
return _artist_cache[aid]
|
| 46 |
-
r = requests.get(f"https://api.spotify.com/v1/artists/{aid}", headers=headers)
|
| 47 |
-
genres = r.json().get('genres', []) if r.status_code == 200 else []
|
| 48 |
-
_artist_cache[aid] = genres
|
| 49 |
-
return genres
|
| 50 |
-
|
| 51 |
-
# Trae tabla y devuelve DataFrame
|
| 52 |
-
def fetch_playlist_table(url):
|
| 53 |
-
if not CLIENT_ID or not CLIENT_SECRET:
|
| 54 |
-
return pd.DataFrame([{'Error': 'Faltan credenciales'}])
|
| 55 |
-
token = get_token()
|
| 56 |
-
pid = extract_pid(url)
|
| 57 |
-
headers = {"Authorization": f"Bearer {token}"}
|
| 58 |
-
meta = requests.get(f"https://api.spotify.com/v1/playlists/{pid}", headers=headers)
|
| 59 |
-
if meta.status_code != 200:
|
| 60 |
-
return pd.DataFrame([{'Error': f"Playlist error {meta.status_code}: {meta.json()}"}])
|
| 61 |
-
total = meta.json().get('tracks', {}).get('total', 0)
|
| 62 |
-
records = []
|
| 63 |
-
limit = 100
|
| 64 |
-
offset = 0
|
| 65 |
-
while offset < total:
|
| 66 |
-
params = {'limit': limit, 'offset': offset}
|
| 67 |
-
r = requests.get(
|
| 68 |
-
f"https://api.spotify.com/v1/playlists/{pid}/tracks",
|
| 69 |
-
headers=headers,
|
| 70 |
-
params=params
|
| 71 |
-
)
|
| 72 |
-
r.raise_for_status()
|
| 73 |
-
for item in r.json().get('items', []):
|
| 74 |
-
tr = item.get('track', {})
|
| 75 |
-
if not tr:
|
| 76 |
-
continue
|
| 77 |
-
aid = tr['artists'][0]['id'] if tr.get('artists') else None
|
| 78 |
-
genres = get_artist_genres(aid, headers) if aid else []
|
| 79 |
-
records.append({
|
| 80 |
-
'Artist': tr['artists'][0]['name'] if tr.get('artists') else '',
|
| 81 |
-
'Title': tr.get('name', ''),
|
| 82 |
-
'ISRC': tr.get('external_ids', {}).get('isrc', ''),
|
| 83 |
-
'URL': tr.get('external_urls', {}).get('spotify', ''),
|
| 84 |
-
'URI': tr.get('uri', ''),
|
| 85 |
-
'Genres': ', '.join(genres)
|
| 86 |
-
})
|
| 87 |
-
offset += limit
|
| 88 |
-
return pd.DataFrame(records)
|
| 89 |
-
|
| 90 |
-
# Función principal: retorna sólo DataFrame
|
| 91 |
-
|
| 92 |
-
def main(url):
|
| 93 |
-
return fetch_playlist_table(url)
|
| 94 |
-
|
| 95 |
-
# Configuración de la interfaz Gradio
|
| 96 |
iface = gr.Interface(
|
| 97 |
-
fn=
|
| 98 |
-
inputs=gr.Textbox(label="
|
| 99 |
-
outputs=
|
| 100 |
-
|
| 101 |
-
label="
|
| 102 |
-
|
| 103 |
-
title="
|
| 104 |
-
description="
|
| 105 |
)
|
| 106 |
|
| 107 |
if __name__ == '__main__':
|
| 108 |
-
iface.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
|
| 5 |
+
# Simple example to test in-memory CSV download using gr.Download
|
| 6 |
+
# Requires Gradio ≥4.44.1
|
| 7 |
+
|
| 8 |
+
def generate_table_and_csv(text):
|
| 9 |
+
# Create a DataFrame based on input text
|
| 10 |
+
df = pd.DataFrame({
|
| 11 |
+
"Input": [text],
|
| 12 |
+
"Length": [len(text)]
|
| 13 |
+
})
|
| 14 |
+
# Convert DataFrame to CSV in memory
|
| 15 |
+
buffer = BytesIO()
|
| 16 |
+
df.to_csv(buffer, index=False)
|
| 17 |
+
buffer.seek(0)
|
| 18 |
+
# Return DataFrame and a tuple (filename, bytes) for gr.Download
|
| 19 |
+
return df, ("output.csv", buffer.getvalue())
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
iface = gr.Interface(
|
| 22 |
+
fn=generate_table_and_csv,
|
| 23 |
+
inputs=gr.Textbox(label="Enter some text"),
|
| 24 |
+
outputs=[
|
| 25 |
+
gr.Dataframe(label="Generated Table"),
|
| 26 |
+
gr.Download(label="Download CSV")
|
| 27 |
+
],
|
| 28 |
+
title="Download Test",
|
| 29 |
+
description="Type text to generate a table and download it as CSV (in memory)."
|
| 30 |
)
|
| 31 |
|
| 32 |
if __name__ == '__main__':
|
| 33 |
+
iface.launch()
|