# Standard Library Imports import os import random import re import time from urllib.parse import urlparse, parse_qs # Third-Party Imports import gradio as gr import lyricsgenius import requests import spotipy from bs4 import BeautifulSoup from dotenv import load_dotenv from fuzzywuzzy import fuzz from pydantic import BaseModel, Field from requests.exceptions import Timeout from sentence_transformers import SentenceTransformer from sklearn.metrics.pairwise import cosine_similarity from spotipy.exceptions import SpotifyException # Local Application/Library Specific Imports import openai from langchain.agents import OpenAIFunctionsAgent, AgentExecutor, tool from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.chat_models import ChatOpenAI from langchain.memory import ConversationBufferMemory from langchain.prompts import MessagesPlaceholder from langchain.schema import SystemMessage, HumanMessage from messages import SYSTEM_MESSAGE, GENRE_LIST, HTML, COMMANDS_MSG, ERROR_MSG from dotenv import load_dotenv load_dotenv() # ------------------------------ # Section: Global Vars # ------------------------------ GENIUS_TOKEN = os.getenv("GENIUS_ACCESS_TOKEN") OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") DEBUG_MODE = True def debug_print(*args, **kwargs): if DEBUG_MODE: print(*args, **kwargs) THEME = gr.themes.Default( primary_hue=gr.themes.colors.stone, font=[gr.themes.GoogleFont("Days One"), "monospace", "sans-serif"], spacing_size=gr.themes.sizes.spacing_sm, radius_size=gr.themes.sizes.radius_sm, text_size=gr.themes.sizes.text_lg ).set( body_background_fill="#FFFFFF", #button_primary_background_fill="white", #button_primary_text_color="black", #button_primary_background_fill_hover="#DC143C", #button_primary_text_color_hover="white" ) # Days One | RocknRoll One # TODO: switch to personal website REDIRECT_URI = "https://jonaswaller.com" # Spotify functions SCOPE = [ 'user-library-read', 'user-read-playback-state', 'user-modify-playback-state', 'playlist-modify-public', 'user-top-read' ] MOOD_SETTINGS = { "happy": {"max_instrumentalness": 0.001, "min_valence": 0.6}, "sad": {"max_danceability": 0.65, "max_valence": 0.4}, "energetic": {"min_tempo": 120, "min_danceability": 0.75}, "calm": {"max_energy": 0.65, "max_tempo": 130} } # genre + mood function NUM_ARTISTS = 20 # artists to retrieve from user's top artists TIME_RANGE = "medium_term" # short, medium, long NUM_TRACKS = 10 # tracks to add to playback MAX_ARTISTS = 4 # sp.recommendations() seeds: 4/5 artists, 1/5 genre # artist + mood function NUM_ALBUMS = 20 # maximum number of albums to retrieve from an artist MAX_TRACKS = 10 # tracks to randomly select from an artist # matching playlists + moods MODEL = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2') # smaller BERT os.environ["TOKENIZERS_PARALLELISM"] = "false" # warning MOOD_LIST = ["happy", "sad", "energetic", "calm"] MOOD_EMBEDDINGS = MODEL.encode(MOOD_LIST) GENRE_EMBEDDINGS = MODEL.encode(GENRE_LIST) # agent tools RETURN_DIRECT = True #LLM_MODEL = "gpt-3.5-turbo-0613" LLM_MODEL = "gpt-4" # adjectives for playlist names THEMES = ["Epic", "Hypnotic", "Dreamy", "Legendary", "Majestic", "Enchanting", "Ethereal", "Super Lit", "Harmonious", "Heroic"] with gr.Blocks(theme=THEME) as app: # ------------------------------ # Section: Spotify Authentication # ------------------------------ gr.HTML(HTML) # incredibly important for preserving using isolation ACCESS_TOKEN_VAR = gr.State() AGENT_EXECUTOR_VAR = gr.State() with gr.Row(): client_id = gr.Textbox(placeholder="7. Paste Spotify Client ID", container=False, text_align="left") generate_link = gr.Button("Submit ID", variant="primary") display_link = gr.HTML() with gr.Row(): url = gr.Textbox(placeholder="9. Paste entire URL", container=False, text_align="left") authorize_url = gr.Button("Submit URL", variant="primary") auth_result = gr.HTML() def spotify_auth(client_id, url=None, access_tokens=None): """ Authenticate Spotify with the provided client_id and url. """ if url: parsed_url = urlparse(url) fragment = parsed_url.fragment access_token = parse_qs(fragment)['access_token'][0] debug_print(access_token) return access_token, '
Your Spotify is connected!
' else: auth_url = ( f"https://accounts.spotify.com/authorize?response_type=token&client_id={client_id}" f"&scope={'%20'.join(SCOPE)}&redirect_uri={REDIRECT_URI}" ) return f'' \ f'8. Click ' \ f'here and copy the entire URL
' generate_link.click(spotify_auth, inputs=[client_id], outputs=display_link) authorize_url.click(spotify_auth, inputs=[client_id, url, ACCESS_TOKEN_VAR], outputs=[ACCESS_TOKEN_VAR, auth_result]) create_agent_button = gr.Button("Start Session", variant="primary") create_agent_result = gr.HTML() def create_agent(access_token): # included 'client' parameter to fix error llm = ChatOpenAI( model=LLM_MODEL, openai_api_key=OPENAI_API_KEY, streaming=True, callbacks=[StreamingStdOutCallbackHandler()], client=openai.ChatCompletion ) # ------------------------------ # Section: Spotify Functions # ------------------------------ sp = spotipy.Spotify(auth=access_token) devices = sp.devices() device_id = devices['devices'][0]['id'] def find_track_by_name(track_name): """ Finds the Spotify track URI given the track name. """ results = sp.search(q=track_name, type='track') track_uri = results['tracks']['items'][0]['uri'] return track_uri def play_track_by_name(track_name): """ Plays a track given its name. Uses the above function. """ track_uri = find_track_by_name(track_name) track_name = sp.track(track_uri)["name"] artist_name = sp.track(track_uri)['artists'][0]['name'] try: sp.start_playback(device_id=device_id, uris=[track_uri]) return f"♫ Now playing {track_name} by {artist_name} ♫" except SpotifyException as e: return f"An error occurred with Spotify: {e}. \n\n**Remember to wake up Spotify.**" except Exception as e: return f"An unexpected error occurred: {e}." def queue_track_by_name(track_name): """ Queues track given its name. """ track_uri = find_track_by_name(track_name) track_name = sp.track(track_uri)["name"] sp.add_to_queue(uri=track_uri, device_id=device_id) return f"♫ Added {track_name} to your queue ♫" def pause_track(): """ Pauses the current playback. """ sp.pause_playback(device_id=device_id) return "♫ Playback paused ♫" def resume_track(): """ Resumes the current playback. """ sp.start_playback(device_id=device_id) return "♫ Playback started ♫" def skip_track(): """ Skips the current playback. """ sp.next_track(device_id=device_id) return "♫ Skipped to your next track ♫" ### ### ### More Elaborate Functions ### ### ### def play_album_by_name_and_artist(album_name, artist_name): """ Plays an album given its name and the artist. context_uri (provide a context_uri to start playback of an album, artist, or playlist) expects a string. """ results = sp.search(q=f'{album_name} {artist_name}', type='album') album_id = results['albums']['items'][0]['id'] album_info = sp.album(album_id) album_name = album_info['name'] artist_name = album_info['artists'][0]['name'] try: sp.start_playback(device_id=device_id, context_uri=f'spotify:album:{album_id}') return f"♫ Now playing {album_name} by {artist_name} ♫" except spotipy.SpotifyException as e: return f"An error occurred with Spotify: {e}. \n\n**Remember to wake up Spotify.**" except Timeout: return f"An unexpected error occurred: {e}." def play_playlist_by_name(playlist_name): """ Plays an existing playlist in the user's library given its name. """ playlists = sp.current_user_playlists() playlist_dict = {playlist['name']: (playlist['id'], playlist['owner']['display_name']) for playlist in playlists['items']} playlist_names = [key for key in playlist_dict.keys()] # defined inside to capture user-specific playlists playlist_name_embeddings = MODEL.encode(playlist_names) user_playlist_embedding = MODEL.encode([playlist_name]) # compares (embedded) given name to (embedded) playlist library and outputs the closest match similarity_scores = cosine_similarity(user_playlist_embedding, playlist_name_embeddings) most_similar_index = similarity_scores.argmax() playlist_name = playlist_names[most_similar_index] try: playlist_id, creator_name = playlist_dict[playlist_name] sp.start_playback(device_id=device_id, context_uri=f'spotify:playlist:{playlist_id}') return f'♫ Now playing {playlist_name} by {creator_name} ♫' except: return "Unable to find playlist. Please try again." def get_track_info(): """ Harvests information for explain_track() using Genius' API and basic webscraping. """ current_track_item = sp.current_user_playing_track()['item'] track_name = current_track_item['name'] artist_name = current_track_item['artists'][0]['name'] album_name = current_track_item['album']['name'] release_date = current_track_item['album']['release_date'] basic_info = { 'track_name': track_name, 'artist_name': artist_name, 'album_name': album_name, 'release_date': release_date, } # define inside to avoid user conflicts (simultaneously query Genius) genius = lyricsgenius.Genius(GENIUS_TOKEN) # removing feature information from song titles to avoid scewing search track_name = re.split(' \(with | \(feat\. ', track_name)[0] result = genius.search_song(track_name, artist_name) # if no Genius page exists if result is not None and hasattr(result, 'artist'): genius_artist = result.artist.lower().replace(" ", "") spotify_artist = artist_name.lower().replace(" ", "") debug_print(spotify_artist) debug_print(genius_artist) if spotify_artist not in genius_artist: return basic_info, None, None, None else: genius_artist = None return basic_info, None, None, None # if Genius page exists lyrics = result.lyrics url = result.url response = requests.get(url) # parsing the webpage and locating 'About' section soup = BeautifulSoup(response.text, 'html.parser') # universal 'About' section element across all Genius song lyrics pages about_section = soup.select_one('div[class^="RichText__Container-oz284w-0"]') # if no 'About' section exists if not about_section: return basic_info, None, lyrics, url # if 'About' section exists else: about_section = about_section.get_text(separator='\n') return basic_info, about_section, lyrics, url def explain_track(): """ Displays track information in an organized, informational, and compelling manner. Uses the above function. """ basic_info, about_section, lyrics, url = get_track_info() debug_print(basic_info, about_section, lyrics, url) if lyrics: # if Genius page exists system_message_content = """ Your task is to create an engaging summary for a track using the available details about the track and its lyrics. If there's insufficient or no additional information besides the lyrics, craft the entire summary based solely on the lyrical content." """ human_message_content = f"{about_section}\n\n{lyrics}" messages = [ SystemMessage(content=system_message_content), HumanMessage(content=human_message_content) ] ai_response = llm(messages).content summary = f""" **Name:** {basic_info["track_name"]} **Artist:** {basic_info["artist_name"]} **Album:** {basic_info["album_name"]} **Release:** {basic_info["release_date"]} **About:** {ai_response} Click here for more information on Genius! """ return summary else: # if no Genius page exists url = "https://genius.com/Genius-how-to-add-songs-to-genius-annotated" summary = f""" **Name:** {basic_info["track_name"]} **Artist:** {basic_info["artist_name"]} **Album:** {basic_info["album_name"]} **Release:** {basic_info["release_date"]} **About:** Unfortunately, this track has not been uploaded to Genius.com Be the first to change that! """ return summary ### ### ### Genre + Mood ### ### ### def get_user_mood(user_mood): """ Categorizes the user's mood as either 'happy', 'sad', 'energetic', or 'calm'. Uses same cosine similarity/embedding concepts as with determining playlist names. """ if user_mood.lower() in MOOD_LIST: user_mood = user_mood.lower() return user_mood else: user_mood_embedding = MODEL.encode([user_mood.lower()]) similarity_scores = cosine_similarity(user_mood_embedding, MOOD_EMBEDDINGS) most_similar_index = similarity_scores.argmax() user_mood = MOOD_LIST[most_similar_index] return user_mood def get_genre_by_name(genre_name): """ Matches user's desired genre to closest (most similar) existing genre in the list of genres. recommendations() only accepts genres from this list. """ if genre_name.lower() in GENRE_LIST: genre_name = genre_name.lower() return genre_name else: genre_name_embedding = MODEL.encode([genre_name.lower()]) similarity_scores = cosine_similarity(genre_name_embedding, GENRE_EMBEDDINGS) most_similar_index = similarity_scores.argmax() genre_name = GENRE_LIST[most_similar_index] return genre_name def is_genre_match(genre1, genre2, threshold=75): """ Determines if two genres are semantically similar. token_set_ratio() - for quantifying semantic similarity - and threshold of 75 (out of 100) were were arbitrarily determined through basic testing. """ score = fuzz.token_set_ratio(genre1, genre2) debug_print(score) return score >= threshold def create_track_list_str(track_uris): """ Creates an organized list of track names. Used in final return statements by functions below. """ track_details = sp.tracks(track_uris) track_names_with_artists = [f"{track['name']} by {track['artists'][0]['name']}" for track in track_details['tracks']] track_list_str = "Success! Type -music to view commands
' create_agent_button.click(create_agent, inputs=[ACCESS_TOKEN_VAR], outputs=[AGENT_EXECUTOR_VAR, create_agent_result]) # ------------------------------ # Section: Chat Interface # ------------------------------ chatbot = gr.Chatbot( bubble_full_width=False, label="Apollo", height=460, avatar_images=(None, (os.path.join(os.path.dirname(__file__), "avatar2.png"))) ) msg = gr.Textbox( placeholder="What would you like to hear?", container=False, text_align="left" ) def respond(user_message, chat_history, agent_executor): try: if user_message.strip() == "-music": bot_message = COMMANDS_MSG else: bot_message = agent_executor.run(user_message) chat_history.append((user_message, bot_message)) except Exception as e: bot_message = ERROR_MSG chat_history.append((user_message, bot_message)) time.sleep(1) return "", chat_history msg.submit(respond, inputs=[msg, chatbot, AGENT_EXECUTOR_VAR], outputs=[msg, chatbot]) gr.Examples(["Play chill rap", "I'm feeling great, match my vibe", "Make me a relaxing playlist of SZA-like songs"], inputs=[msg], label="Quick Start 🚀") gr.HTML('''GitHub Repo | I'd love to hear your feedback: stuart.j.waller@vanderbilt.edu
''') app.launch() #app.launch(share=True)