Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
2.7 kB
1
Indexable
Never
import streamlit as st
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import openai

# (Your Spotify credentials here)
client_id = '3f58de94bd1745389891a36627a1cc62'
client_secret = 'aff2cacecd68459ea257420ebceb0be2'
oauth_token = 'your_oauth_token'  # Spotify OAuth access token

# Autentication with Spotipy
sp = spotipy.Spotify(auth=oauth_token)

# (Your OpenAI API key here)
openai.api_key = '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'

# Function to get emotions from OpenAI
def get_emotions_from_openai(song_name, artist_name):
    prompt = f"Dada la canción '{song_name}' por '{artist_name}', ¿qué emociones crees que transmite?"
    response = openai.Completion.create(
        model="gpt-3.5-turbo-instruct",  # Use the currently available OpenAI model
        prompt=prompt,
        max_tokens=60,
        temperature=0.7,
        n=1,
        stop=None
    )
    return response.choices[0].text.strip()

# Function to get featured playlists with improved error handling
def get_featured_playlists():
    try:
        results = sp.featured_playlists()
        if results['playlists']['items']:
            return results['playlists']['items']
        else:
            st.error("No se encontraron listas destacadas en este momento.")
            return []  # Return empty list to avoid errors downstream
    except Exception as e:
        st.error(f"Error al obtener listas destacadas: {e}")
        return []  # Return empty list to avoid errors downstream

# Streamlit Interface
st.title('Análisis de Emociones en Listas de Reproducción de Spotify')

# Get featured playlists and handle potential errors
playlists = get_featured_playlists()

# Check if playlists were retrieved successfully before proceeding
if playlists:
    playlist_names = [playlist['name'] for playlist in playlists]
    playlist_ids = [playlist['id'] for playlist in playlists]
    selected_playlist_id = st.selectbox('Selecciona una lista de reproducción:', playlist_ids, format_func=lambda x: playlist_names[playlist_ids.index(x)])

    # Process selected playlist if available
    if selected_playlist_id:
        results = sp.playlist_tracks(selected_playlist_id)
        tracks = results['items']

        if tracks:
            for item in tracks:
                track = item['track']
                name = track['name']
                artist = track['artists'][0]['name']
                st.write(f"{name} - {artist}")

                # Get and display emotions
                emotions = get_emotions_from_openai(name, artist)
                st.write(f"Emociones sugeridas: {emotions}")
        else:
            st.write("No se encontraron canciones en la lista de reproducción seleccionada.")


Leave a Comment