-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
159 lines (138 loc) · 6.34 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import pandas as pd
import numpy as np
import os
from contextlib import suppress
import warnings
warnings.filterwarnings("ignore")
from sklearn.preprocessing import StandardScaler
from sklearn.metrics.pairwise import cosine_similarity
import pickle
import requests
from flask import Flask, request, jsonify, render_template, send_file, safe_join, abort
from commons import *
with open('dist/dict_columns_groups.pkl', 'rb') as pkl:
dict_columns_groups = pickle.load(pkl)
with open('dist/columns_not_categorized.pkl', 'rb') as pkl:
columns_not_categorized = pickle.load(pkl)
with open('dist/lgbm_clf.pkl', 'rb') as pkl:
LightGBM = pickle.load(pkl)
with open('dist/sc.pkl', 'rb') as pkl:
sc = pickle.load(pkl)
df_scaled = pd.read_csv('df_scaled.csv',sep=",",index_col='indice')
data_index = pd.read_csv('data_index.csv',sep=",",index_col='indice.1')
# Iniciamos nuestra API
app = Flask(__name__)
app.config["FILES_PATH"] = "Full_MIDI"
@app.route("/search_titles",methods=['GET'])
def search_titles(data_index=data_index, sc=sc):
search=request.args['search']
page_number =int(request.args['page_number'])
cant_resultados = int(request.args['cant_resultados'])
page_from = page_number*cant_resultados
page_to = page_from + cant_resultados
print(page_from,page_to)
result = data_index[data_index['indice_lower_search'].str.contains(search.lower())].indice[page_from:page_to].to_dict()
response = jsonify(result)
# Enable Access-Control-Allow-Origin
response.headers.add("Access-Control-Allow-Origin", "*")
return response
@app.route("/related_songs",methods=['GET'])
def get_related_songs(df_scaled=df_scaled, sc=sc):
data=df_scaled.copy()
search=request.args['search']
predict=request.args['predict']
filtrar=request.args['filtrar']
duracion_notas =float(request.args['duracion_notas'])
amplitud_tonal = float(request.args['amplitud_tonal'])
ritmica_instrument = float(request.args['ritmica_instrument'])
ritmica_drums = float(request.args['ritmica_drums'])
armonia = float(request.args['armonia'])
dinamica = float(request.args['dinamica'])
instrumentacion = float(request.args['instrumentacion'])
tempo = float(request.args['tempo'])
notas_simultaneas = float(request.args['notas_simultaneas'])
duracion_tema = float(request.args['duracion_tema'])
cant_resultados = int(request.args['cant_resultados'])
others = float(request.args['others'])
penalizacion = float(request.args['penalizacion'])
page_number =int(request.args['page_number'])
clusterizar=request.args['clusterizar']
print(search)
search_song = data_index[data_index['indice_lower'] == search.lower()].indice.iloc[0]
new_midi = ""
cluster = ""
if (search.lower() == 'false'):
new_midi = get_midi_from_path()
tema = new_midi.tema
midi_df_cols = pd.DataFrame(columns=data.columns).drop(['Cluster'],axis=1)
mask_columns = list(set(df_scaled.columns) & set(new_midi.columns))
new_midi = new_midi[mask_columns]
midi_df = pd.concat([midi_df_cols, new_midi])
midi_df.fillna(0, inplace=True)
new_midi = sc.transform(midi_df)
cluster = LightGBM.predict(new_midi)[0]
print('predicted cluster:',cluster)
midi_df = pd.DataFrame(new_midi, columns=midi_df_cols.columns)
midi_df['Cluster'] = cluster
data = pd.concat([data, midi_df])
path = '..\GET_FILE\\'
data.iloc[data.shape[0] - 1:data.shape[0],:].index = path + tema
search_song = data.iloc[data.shape[0] - 1:data.shape[0],:].index
print('tema:',search_song)
else:
cluster = data[data.index == search_song].Cluster.iloc[0]
print('cluster:',cluster)
mask_cluster = data['Cluster'] == cluster
if (clusterizar.lower() == 'true'):
data = data[mask_cluster]
print(cluster, data.shape)
dict_key_values = {'instrumentacion':instrumentacion,
'ritmica_drums':ritmica_drums ,
'ritmica_instrument':ritmica_instrument ,
'amplitud_tonal':amplitud_tonal ,
'dinamica':dinamica,
'duracion_notas1':duracion_notas,
'duracion_notas2':duracion_notas,
'notas_simultaneas':notas_simultaneas ,
'tempo':tempo,
'duracion_tema':duracion_tema ,
'armonia1':armonia,
'armonia2':armonia,
'armonia3':armonia,
'armonia4':armonia}
min_sim_distance = 1 / 10**penalizacion
for keys in dict_key_values.keys():
cant_keys = len(dict_columns_groups[keys])
for column in dict_columns_groups[keys]:
data[column] = (data[column] / np.sqrt(cant_keys)) * (dict_key_values[keys] + min_sim_distance)
for column in columns_not_categorized:
data[column] = (data[column] / np.sqrt(cant_keys) ) * (others + min_sim_distance)
song = data.loc[search_song,:]
if (isinstance(song, pd.Series) == False):
song = song.iloc[0]
if (filtrar.lower() == 'true'):
song_values_mask = (song > 0).index
data = data.loc[:,song_values_mask]
print(type(data.loc[search_song,:]))
similarity = cosine_similarity_row(data.to_numpy(), song.array, data.index)
page_from = page_number*cant_resultados
page_to = page_from + cant_resultados
result = similarity.iloc[page_from:page_to:].reset_index()
result.columns = ['path', 'value']
result = result.to_dict()
response = jsonify(result)
# Enable Access-Control-Allow-Origin
response.headers.add("Access-Control-Allow-Origin", "*")
return response
@app.route("/get-file/<path:filename>")
def get_file(filename):
safe_path = safe_join(app.config["FILES_PATH"], filename)
print(safe_path)
try:
response = send_file(safe_path, as_attachment=True)
response.headers.add("Access-Control-Allow-Origin", "*")
return response
except FileNotFoundError:
abort(404)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)