<jupyter_start><jupyter_text>- 

-<jupyter_code>from os import getenv
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.mixture import GaussianMixture #covariance_type='diag'
from scipy.spatial import distance
kg = pd.read_csv('spotify_tracks_metadata.csv')
kg_slim = kg[['danceability','energy','key','loudness','mode','speechiness','acousticness','instrumentalness','liveness','valence','tempo','duration_ms','time_signature']]
kg_slim.head()
pipe = Pipeline(steps=[('scaler', StandardScaler()), ('gauss', GaussianMixture(
        n_components=5,           # could tune by minimizing aic or bic
        covariance_type='diag',   # 'full' is best, 'spherical' is fastest
        n_init=10))])    # Hands-On ML warns that default n_init=1 is too low

pipe.fit(kg_slim)
# Like coordinates in multidimensional song-space, relative to clusters;
# Or like affiliation scores to distinct song distributions;
db_song_coords = pipe.predict_proba(kg_slim)<jupyter_output><empty_output><jupyter_text>the model (pipe) and what it says about the database songs (db_song_coords) could be saved offline  then, via the app, we provide a new_song (or song_name + artist_name);
 the app queries spotify for this new_song's features;
 feeds those audio_features to our pre-trained model; 
 and compares the read-out to its offline analysis of database songs (db_song_coords).<jupyter_code>"""Setting up APIs for this notebook"""
from client.spotify_client import *
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

SPOTIFY_API_KEY = getenv("SPOTIFY_API_KEY")
SPOTIFY_API_KEY_SECRET = getenv("SPOTIFY_API_KEY_SECRET")


# get spotify API 
spotify = SpotifyAPI(SPOTIFY_API_KEY, SPOTIFY_API_KEY_SECRET)

# get spotiPy API (wrapper for spotify API)
auth_manager = SpotifyClientCredentials(SPOTIFY_API_KEY, SPOTIFY_API_KEY_SECRET)
spotiPy = spotipy.Spotify(auth_manager=auth_manager)
# html could accept user input, typed into 2 separate fields; hit a button
user_input_song = 'superstition'
user_input_artist = 'stevie wonder'

# app@route('/') takes user_input_song, user_input_artist...feeds APIs

# spotify API -- from (song_name, artist_name) to track's (Spotify ID);
new_song = spotify.search(
    {'track':user_input_song, 'artist':user_input_artist}, search_type='track')
new_song_spot_id = new_song['tracks']['items'][0]['id']

# spotiPy API -- from track's (Spotify ID) to its (audio_features):
new_song_features = spotiPy.audio_features(tracks=[new_song_spot_id])

# arrange new_song's audio_features to match feature matrix used to train model;
new_song_ready = pd.DataFrame(new_song_features)[['danceability','energy','key','loudness','mode','speechiness','acousticness','instrumentalness','liveness','valence','tempo','duration_ms','time_signature']]

# app unpickles saved model (pipe) and saved model output (db_song_coords);
# new_song_ready meets the model;
new_song_coords = pipe.predict_proba(new_song_ready)

# use scipy.distance.cdist(...'cosine') to get cosine similarity scores,
# comparing new_song to everything stored in database
distances = distance.cdist(db_song_coords, new_song_coords, 'cosine')
distances.shape
pd.DataFrame(distances).hist()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10278745644599303, 'fraction_numerical': 0.0020325203252032522, 'mean_word_length': 6.207112970711297, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '64838', 'license': 'MIT', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1116, 'n_words': 284, 'path': 'GaussianMixture_example.ipynb', 'repo_name': 'engegreg/songs', 'size': '3444'}