Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added README.md #28

Open
wants to merge 9 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Music Meteorologist Data Science API

This Flask app takes in the ID of a Spotify song from an HTTP POST request and outputs three recommended songs (specifically their IDs and cosine similarity scores).

Once the Flask app has received a request, it calls the Spotify API to retrieve the characteristics of the Spotify song and converts those characteristics to a NumPy array. Then, the function all_similarities() is called, where a is the numpy array of the song characteristics and dfy is a component of our database of songs (to which this Spotify song will be compared, and from where we will receive our predictions).

```
def cosine_similarity(a, b):
dot_product = np.dot(a,b)
norm_a = np.linalg.norm(a)
norm_b = np.linalg.norm(b)
return dot_product / (norm_a * norm_b)
#return np.sqrt(np.sum((a - b) ** 2))
def all_similarities(a, dfy):
similar_songs = []
for spotify_song, metadata in zip(array, dfy.values):
similarity = cosine_similarity(a, spotify_song)
similar_songs.append({'similarity': ''.join(str(similarity.tolist())), 'values': metadata[1]})
return similar_songs
```


7 changes: 2 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,21 @@ def all_similarities(a, dfy):
similar_songs = []
for spotify_song, metadata in zip(array, dfy.values):
similarity = cosine_similarity(a, spotify_song)
similar_songs.append({'similarity': similarity, 'values': metadata[1]})
similar_songs.append({'similarity': ''.join(str(similarity.tolist())), 'values': metadata[1]})
return similar_songs


@app.route("/", methods=['GET', 'POST'])
def default():

content = request.get_json(silent=True)
#print("song", content)
dataframe = pd.DataFrame.from_dict(json_normalize(content['audio_features']), orient='columns')
#print("dataframe", dataframe)

song = dataframe.values
#print("content", song)

#song = array[1549]
similarities = all_similarities(song, dfy)
sorted_list = sorted(similarities, key=lambda i: i['similarity'], reverse=True)[1:3]
sorted_list = sorted(similarities, key=lambda i: i['similarity'], reverse=True)[:3]
json_dict = {"songs": sorted_list}
#data = json.dumps(json_dict)
return jsonify(json_dict)
Expand Down