Skip to content

Commit

Permalink
Merge pull request #29 from Lambda-School-Labs/RDS-postgres
Browse files Browse the repository at this point in the history
Rds postgres
  • Loading branch information
zwarshavsky authored Feb 20, 2020
2 parents 82a6444 + a7cfe0c commit dc50a20
Show file tree
Hide file tree
Showing 37 changed files with 1,100 additions and 73 deletions.
9 changes: 9 additions & 0 deletions RDS-postgres/SOUNDDRIP/.ebextensions/SounddripDev-env.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
option_settings:
"aws:elasticbeanstalk:container:python":
WSGIPath: application.py
files:
"/etc/httpd/conf.d/wsgi_custom.conf":
mode: "000644"
owner: root
group: root
content: WSGIApplicationGroup %{GLOBAL}
1 change: 1 addition & 0 deletions RDS-postgres/SOUNDDRIP/.ebignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sounddrip
33 changes: 33 additions & 0 deletions RDS-postgres/SOUNDDRIP/application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# import spotipy
# import spotipy.util as util
from flask import (Flask, render_template, request,
make_response,
jsonify)

from models.predict import predictfunc, get_id, get_features, instantiate_sp


# Create Flask app. Should use "application" as variable name for AWS
application = Flask(__name__)



# Main Default page
@application.route("/request", methods=['GET', 'POST'])
def prediction():
''''request flask route takes token passed in from FE POST and outputs the 20 most similar songs'''
content = request.get_json(silent=True)
token = content["token"]
sp = instantiate_sp(token)
id = get_id(sp)
features = get_features(id, sp)
return jsonify(predictfunc(features), print('yay'))


@application.route("/")
def root():
return """Hello, I am working right now. send your request to {/request}"""


if __name__ == "__main__":
application.run()
Binary file added RDS-postgres/SOUNDDRIP/data/song_id_list2.joblib
Binary file not shown.
Binary file added RDS-postgres/SOUNDDRIP/data/song_id_list2.pkl
Binary file not shown.
2 changes: 2 additions & 0 deletions RDS-postgres/SOUNDDRIP/misc/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Allow: /
Binary file added RDS-postgres/SOUNDDRIP/models/model2.joblib
Binary file not shown.
37 changes: 37 additions & 0 deletions RDS-postgres/SOUNDDRIP/models/original_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Ihis file contains def for spotify api"""
"""Data engnering, cosine_similarity, and all_similarities"""

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import pandas as pd
import numpy as np

"""This def Grants access to spotify API"""
def cred_init(cli_id, cli_secret):
client_credentials_manager = SpotifyClientCredentials(
client_id=cli_id,
client_secret=cli_secret)
client_credentials_manager = client_credentials_manager
sp = spotipy.Spotify(client_credentials_manager=
client_credentials_manager)
return sp

"""This def organizes pandas dataframe from a csv"""
def original_data_engnering(csv1, csv2):
df = pd.read_csv(csv1)
df_other = pd.read_csv(csv2)
df = df.drop('popularity', 1)
df = df.drop('duration_ms', 1)
df = df.dropna()
df_other = df_other.drop('popularity', 1)
df_other = df_other.drop('duration_ms', 1)
df_other = df_other.dropna()
df = pd.concat([df,df_other]).
drop_duplicates().
reset_index(drop=True)
dfy = df[['artist_name','track_id', 'track_name']]
df2 = df.drop('artist_name', 1)
df2 = df2.drop('track_id', 1)
df2 = df2.drop('track_name', 1)
array = df2.values
return dfy, array
74 changes: 74 additions & 0 deletions RDS-postgres/SOUNDDRIP/models/predict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import spotipy
import spotipy.util as util
from sklearn.neighbors import NearestNeighbors
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from flask import request
import pandas as pd
from pandas.io.json import json_normalize
from flask import jsonify
from joblib import load
import pickle


def instantiate_sp(token):
sp = spotipy.Spotify(auth=token)
return sp


def get_id(sp):
results = sp.current_user_saved_tracks()
song_id = results['items'][0]['track']['id']
return song_id


def get_features(song_id,sp):
results_dict = sp.audio_features(song_id)[0]
audio_features = {
"audio_features": {
key: results_dict[key] for key in results_dict.keys() & {
'danceability',
'energy',
'key',
'loudness',
'mode',
'speechiness',
'acousticness',
'instrumentalness',
'liveness',
'valence',
'tempo',
'time_signature'}}}

return audio_features


def predictfunc(content):
similar_songs = []
print('Loading dataframe...')
dataframe = pd.DataFrame.from_dict(
json_normalize(content['audio_features']),
orient='columns')
print('Dataframe Object Created')
print('Loading pickled scaler...')
scaler = load('./models/scalar2.joblib')
print('Pickled scaler loaded')
print('Scaling dataframe object...')
dataframe_scaled = scaler.transform(dataframe)
print('Dataframe scaled')
print('Loading pickled model...')
model = load('./models/model2.joblib')
print('Model loaded')
results = model.kneighbors([dataframe_scaled][0])[1]
print('Prediction executed')
print('song_id_list loading...')
#song_id_list = load('./data/song_id_list2.joblib')
# (added 3.4 sec to run time)
song_id_list = pickle.load(open('./data/song_id_list2.pkl', 'rb'))
print('song_id_list loaded')

print('beginning for loop...')
for song_row in results[0][1:]:
song_id = song_id_list[song_row]
similar_songs.append({'similarity': [.99], 'values': song_id})
json_dict = {"songs": similar_songs}
return json_dict
Binary file added RDS-postgres/SOUNDDRIP/models/scalar2.joblib
Binary file not shown.
84 changes: 84 additions & 0 deletions RDS-postgres/SOUNDDRIP/rds-postgres/bin/activate
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly


if [ "${BASH_SOURCE-}" = "$0" ]; then
echo "You must source this script: \$ source $0" >&2
exit 33
fi

deactivate () {
unset -f pydoc >/dev/null 2>&1

# reset old environment variables
# ! [ -z ${VAR+_} ] returns true if VAR is declared at all
if ! [ -z "${_OLD_VIRTUAL_PATH:+_}" ] ; then
PATH="$_OLD_VIRTUAL_PATH"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if ! [ -z "${_OLD_VIRTUAL_PYTHONHOME+_}" ] ; then
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
hash -r 2>/dev/null
fi

if ! [ -z "${_OLD_VIRTUAL_PS1+_}" ] ; then
PS1="$_OLD_VIRTUAL_PS1"
export PS1
unset _OLD_VIRTUAL_PS1
fi

unset VIRTUAL_ENV
if [ ! "${1-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}

# unset irrelevant variables
deactivate nondestructive

VIRTUAL_ENV="/Users/alexanderbennett/Desktop/repos/Music-Meteorologist-ds/RDS-postrges/SOUNDDRIP/RDS-postgres"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH

# unset PYTHONHOME if set
if ! [ -z "${PYTHONHOME+_}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
unset PYTHONHOME
fi

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1-}"
if [ "x" != x ] ; then
PS1="${PS1-}"
else
PS1="(`basename \"$VIRTUAL_ENV\"`) ${PS1-}"
fi
export PS1
fi

# Make sure to unalias pydoc if it's already there
alias pydoc 2>/dev/null >/dev/null && unalias pydoc || true

pydoc () {
python -m pydoc "$@"
}

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
hash -r 2>/dev/null
fi
55 changes: 55 additions & 0 deletions RDS-postgres/SOUNDDRIP/rds-postgres/bin/activate.csh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# This file must be used with "source bin/activate.csh" *from csh*.
# You cannot run it directly.
# Created by Davide Di Blasi <[email protected]>.

set newline='\
'

alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH:q" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT:q" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate && unalias pydoc'

# Unset irrelevant variables.
deactivate nondestructive

setenv VIRTUAL_ENV "/Users/alexanderbennett/Desktop/repos/Music-Meteorologist-ds/RDS-postrges/SOUNDDRIP/RDS-postgres"

set _OLD_VIRTUAL_PATH="$PATH:q"
setenv PATH "$VIRTUAL_ENV:q/bin:$PATH:q"



if ("" != "") then
set env_name = ""
else
set env_name = '('"$VIRTUAL_ENV:t:q"') '
endif

if ( $?VIRTUAL_ENV_DISABLE_PROMPT ) then
if ( $VIRTUAL_ENV_DISABLE_PROMPT == "" ) then
set do_prompt = "1"
else
set do_prompt = "0"
endif
else
set do_prompt = "1"
endif

if ( $do_prompt == "1" ) then
# Could be in a non-interactive environment,
# in which case, $prompt is undefined and we wouldn't
# care about the prompt anyway.
if ( $?prompt ) then
set _OLD_VIRTUAL_PROMPT="$prompt:q"
if ( "$prompt:q" =~ *"$newline:q"* ) then
:
else
set prompt = "$env_name:q$prompt:q"
endif
endif
endif

unset env_name
unset do_prompt

alias pydoc python -m pydoc

rehash
Loading

0 comments on commit dc50a20

Please sign in to comment.