Skip to content

Commit

Permalink
Merge pull request #6 from spotify-song-suggestor/recommend
Browse files Browse the repository at this point in the history
Moved some files for convenience, added model and encoded data. Wrote…
  • Loading branch information
meowgorithms authored May 25, 2021
2 parents 8a6d023 + d29aa7d commit 0d8e686
Show file tree
Hide file tree
Showing 10 changed files with 550 additions and 50 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ gunicorn = "*"
plotly = "*"
dash = "*"
dash-bootstrap-components = "*"
tensorflow = "*"
sklearn = "*"

[dev-packages]

Expand Down
500 changes: 471 additions & 29 deletions Pipfile.lock

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added app/dash-template/assets/encoded_data.joblib
Binary file not shown.
Binary file added app/dash-template/assets/knn.joblib
Binary file not shown.
36 changes: 27 additions & 9 deletions app/dash-template/pages/predictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,40 @@
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

import joblib
from tensorflow.keras.models import load_model
import numpy as np
# Imports from this application
from app import app


encodings = joblib.load(r'app\dash-template\assets\encoded_data.joblib')
knn = joblib.load(r'app\dash-template\assets\knn.joblib')
model = load_model(r'app\dash-template\assets\ae4')


def recommend(index: int, n: int=5):
'''
index: index of song
n: number of recommendations to pull
returns: array of distances, array of indexes for recommended songs. Includes
original song.
'''
return knn.kneighbors([encodings[index]], n_neighbors=5)


def get_songs(indeces: 'list[int]'):
''' TODO
Uses SQLAlchemy queries to return track data from their indeces
'''
return None


# 2 column layout. 1st column width = 4/12
# https://dash-bootstrap-components.opensource.faculty.ai/l/components/layout
column1 = dbc.Col(
[
dcc.Markdown(
"""
## Predictions
Your instructions: How to use your app to get new predictions.
"""
),
],
md=4,
Expand All @@ -31,4 +49,4 @@
]
)

layout = dbc.Row([column1, column2])
layout = dbc.Row([column1, column2])
62 changes: 50 additions & 12 deletions model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python388jvsc74a57bd03dbf5d5b7d5bb3e55d0bec78b06bc8b9b99cd7236bee819b06a33ed77bfc0ab5",
"display_name": "Python 3.8.8 64-bit ('DS-Unit-4-Sprint-3-Deep-Learning-bsxCo5fk': pipenv)"
"name": "python388jvsc74a57bd035c03aa0d851cb6285394a035bf26f38eed74d8588e29c3dd9d14089f75b8b7c",
"display_name": "Python 3.8.8 64-bit ('NLP-MTXprD7X': pipenv)"
},
"metadata": {
"interpreter": {
Expand Down Expand Up @@ -58,16 +58,16 @@
},
{
"cell_type": "code",
"execution_count": 78,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"df_full = pd.read_csv('model_ready_data_no_dupes.csv')"
"df_full = pd.read_csv(r'C:\\Users\\Logan\\Desktop\\model_ready_data_no_dupes.csv')"
]
},
{
"cell_type": "code",
"execution_count": 79,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -76,7 +76,7 @@
},
{
"cell_type": "code",
"execution_count": 80,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -85,7 +85,7 @@
},
{
"cell_type": "code",
"execution_count": 81,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand All @@ -96,7 +96,7 @@
]
},
"metadata": {},
"execution_count": 81
"execution_count": 6
}
],
"source": [
Expand All @@ -105,7 +105,7 @@
},
{
"cell_type": "code",
"execution_count": 85,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -182,7 +182,7 @@
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>name</th>\n <th>duration_ms</th>\n <th>explicit</th>\n <th>artists</th>\n <th>release_date</th>\n <th>danceability</th>\n <th>energy</th>\n <th>key</th>\n <th>loudness</th>\n <th>mode</th>\n <th>speechiness</th>\n <th>acousticness</th>\n <th>instrumentalness</th>\n <th>liveness</th>\n <th>valence</th>\n <th>tempo</th>\n <th>time_signature</th>\n <th>popularity</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>carve</td>\n <td>0.022576</td>\n <td>0</td>\n <td>[\"'uli'\"]</td>\n <td>1922-02-22</td>\n <td>0.645</td>\n <td>0.4450</td>\n <td>0.000000</td>\n <td>0.713748</td>\n <td>1</td>\n <td>0.4510</td>\n <td>0.674</td>\n <td>0.744000</td>\n <td>0.1510</td>\n <td>0.1270</td>\n <td>0.425564</td>\n <td>0.6</td>\n <td>0.06</td>\n </tr>\n <tr>\n <th>1</th>\n <td>capítulo 2.16 - banquero anarquista</td>\n <td>0.017470</td>\n <td>0</td>\n <td>[\"'fernando pessoa'\"]</td>\n <td>1922-06-01</td>\n <td>0.695</td>\n <td>0.2630</td>\n <td>0.000000</td>\n <td>0.579173</td>\n <td>1</td>\n <td>0.9570</td>\n <td>0.797</td>\n <td>0.000000</td>\n <td>0.1480</td>\n <td>0.6550</td>\n <td>0.414029</td>\n <td>0.2</td>\n <td>0.00</td>\n </tr>\n <tr>\n <th>2</th>\n <td>vivo para quererte - remasterizado</td>\n <td>0.032313</td>\n <td>0</td>\n <td>[\"'ignacio corsini'\"]</td>\n <td>1922-03-21</td>\n <td>0.434</td>\n <td>0.1770</td>\n <td>0.090909</td>\n <td>0.593796</td>\n <td>1</td>\n <td>0.0512</td>\n <td>0.994</td>\n <td>0.021800</td>\n <td>0.2120</td>\n <td>0.4570</td>\n <td>0.529335</td>\n <td>1.0</td>\n <td>0.00</td>\n </tr>\n <tr>\n <th>3</th>\n <td>el prisionero - remasterizado</td>\n <td>0.031471</td>\n <td>0</td>\n <td>[\"'ignacio corsini'\"]</td>\n <td>1922-03-21</td>\n <td>0.321</td>\n <td>0.0946</td>\n <td>0.636364</td>\n <td>0.490073</td>\n <td>1</td>\n <td>0.0504</td>\n <td>0.995</td>\n <td>0.918000</td>\n <td>0.1040</td>\n <td>0.3970</td>\n <td>0.689907</td>\n <td>0.6</td>\n <td>0.00</td>\n </tr>\n <tr>\n <th>4</th>\n <td>lady of the evening</td>\n <td>0.029012</td>\n <td>0</td>\n <td>[\"'dick haymes'\"]</td>\n <td>1922</td>\n <td>0.402</td>\n <td>0.1580</td>\n <td>0.272727</td>\n <td>0.659263</td>\n <td>0</td>\n <td>0.0390</td>\n <td>0.989</td>\n <td>0.130000</td>\n <td>0.3110</td>\n <td>0.1960</td>\n <td>0.418945</td>\n <td>0.8</td>\n <td>0.00</td>\n </tr>\n <tr>\n <th>...</th>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n </tr>\n <tr>\n <th>524206</th>\n <td>云与海</td>\n <td>0.045945</td>\n <td>0</td>\n <td>[\"'阿yueyue'\"]</td>\n <td>2020-09-26</td>\n <td>0.560</td>\n <td>0.5180</td>\n <td>0.000000</td>\n <td>0.803491</td>\n <td>0</td>\n <td>0.0292</td>\n <td>0.785</td>\n <td>0.000000</td>\n <td>0.0648</td>\n <td>0.2110</td>\n <td>0.535333</td>\n <td>0.8</td>\n <td>0.50</td>\n </tr>\n <tr>\n <th>524207</th>\n <td>blind</td>\n <td>0.027270</td>\n <td>0</td>\n <td>[\"'role model'\"]</td>\n <td>2020-10-21</td>\n <td>0.765</td>\n <td>0.6630</td>\n <td>0.000000</td>\n <td>0.837876</td>\n <td>1</td>\n <td>0.0652</td>\n <td>0.141</td>\n <td>0.000297</td>\n <td>0.0924</td>\n <td>0.6860</td>\n <td>0.609183</td>\n <td>0.8</td>\n <td>0.72</td>\n </tr>\n <tr>\n <th>524208</th>\n <td>what they'll say about us</td>\n <td>0.033374</td>\n <td>0</td>\n <td>[\"'finneas'\"]</td>\n <td>2020-09-02</td>\n <td>0.535</td>\n <td>0.3140</td>\n <td>0.636364</td>\n <td>0.721626</td>\n <td>0</td>\n <td>0.0408</td>\n <td>0.895</td>\n <td>0.000150</td>\n <td>0.0874</td>\n <td>0.0663</td>\n <td>0.588905</td>\n <td>0.8</td>\n <td>0.70</td>\n </tr>\n <tr>\n <th>524209</th>\n <td>a day at a time</td>\n <td>0.025262</td>\n <td>0</td>\n <td>[\"'gentle bones'\", \"'clara benin'\"]</td>\n <td>2021-03-05</td>\n <td>0.696</td>\n <td>0.6150</td>\n <td>0.909091</td>\n <td>0.822748</td>\n <td>1</td>\n <td>0.0345</td>\n <td>0.206</td>\n <td>0.000003</td>\n <td>0.3050</td>\n <td>0.4380</td>\n <td>0.365406</td>\n <td>0.8</td>\n <td>0.58</td>\n </tr>\n <tr>\n <th>524210</th>\n <td>mar de emociones</td>\n <td>0.038134</td>\n <td>0</td>\n <td>[\"'afrosound'\"]</td>\n <td>2015-07-01</td>\n <td>0.686</td>\n <td>0.7230</td>\n <td>0.545455</td>\n <td>0.809670</td>\n <td>1</td>\n <td>0.0363</td>\n <td>0.105</td>\n <td>0.000000</td>\n <td>0.2640</td>\n <td>0.9750</td>\n <td>0.455408</td>\n <td>0.8</td>\n <td>0.38</td>\n </tr>\n </tbody>\n</table>\n<p>524211 rows × 18 columns</p>\n</div>"
},
"metadata": {},
"execution_count": 85
"execution_count": 7
}
],
"source": [
Expand Down Expand Up @@ -614,7 +614,16 @@
},
{
"cell_type": "code",
"execution_count": 254,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"ae4 = load_model('ae4')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
Expand All @@ -625,7 +634,7 @@
]
},
"metadata": {},
"execution_count": 254
"execution_count": 17
}
],
"source": [
Expand All @@ -634,6 +643,26 @@
"knn4.fit(encoded_data_4)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['encoded_data.joblib']"
]
},
"metadata": {},
"execution_count": 19
}
],
"source": [
"joblib.dump(encoded_data_4, 'encoded_data.joblib')"
]
},
{
"cell_type": "code",
"execution_count": 255,
Expand Down Expand Up @@ -1069,6 +1098,15 @@
"source": [
"df_full[df_full['artists'].str.contains('slipkn') == True].head(50)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"model = load_model('ae4')"
]
}
]
}

0 comments on commit 0d8e686

Please sign in to comment.