-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebapp.py
More file actions
203 lines (181 loc) · 6.39 KB
/
webapp.py
File metadata and controls
203 lines (181 loc) · 6.39 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from tkinter.font import names
from click import option
from dash import *
#from matplotlib.pyplot import figure
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
from pyrsistent import v
import processing as pp
import emotions as em
user_df = pd.DataFrame()
playlists = []
songsList = []
app = Dash(__name__)
app.layout = html.Div(className='row',children=[
html.H1(children='Analizador de canciones'),
html.Div(children='''
Analiza la canción y los sentimientos que transmiten la música que escuchas en Spotify
'''),
html.Br(),
html.Div(children=[
html.Label(children='Introduce tu usuario '),
dcc.Input(
id="inputuser",
type='text',
placeholder="Escribe aquí el usuario",
),
html.Button(id='buscar',children='Buscar',n_clicks=0)
]),
html.Br(),
html.Div(id='sel-playlist',children=[
html.Label(children='Selecciona una playlist '),
dcc.Dropdown(id='playlist'),
]),
html.Br(),
html.Div(children=[
html.Div(children=[
html.Label(children='Características de canciones de la playlist'),
dcc.Dropdown(id='song1'),
html.Button(id='reset-polar',children='Reset',n_clicks=0),
dcc.Graph(id='polar',figure=go.Figure())
],className="row",style={'width': '49%','display': 'inline-block'}),
html.Div(children=[
html.Label(children='Gráfica de canciones explicitas de la playlist seleccionda'),
html.Br(),
dcc.Graph(id='explicit-graph',figure=go.Figure())
],className="row",style={'width': '49%','display': 'inline-block'}),
]),
html.Br(),
html.Div(children=[
html.Br(),
html.Label(children='Indica el número de canciones a analizar'),
dcc.Slider(id='slider',min=1,value=1,step=1,max=2),
html.Button(id='analize',children='Analizar',n_clicks=0),
html.Label(children='Gráfica de sentimiento de la playlist seleccionada'),
html.Br(),
dcc.Graph(id='sentiment-graph'),
]),
])
@app.callback(
Output('playlist','options'),
Input('buscar', 'n_clicks'),
State('inputuser','value'),
)
def get_playlists(clicks,value):
global user_df
user_df = pd.DataFrame()
user_df = pp.getDataframeOfUser(value)
res = user_df['PlaylistName'].unique()
#results = get_playlists_user(value)
# df = pd.DataFrame()
# for item in user_df['PlaylistName']:
# tracknames = item['name']
# ids = item['id']
# df['name'] = tracknames
# df['id'] = ids
# print(df.head())
# playlists = {}
# playlists = {'label':results['items'][0]['name'],'value':results['items'][0]['id']} #0 no, todas
return res
@app.callback(
Output('slider','max'),
Output('song1','options'),
Output('song1','value'),
Input('playlist','value'),
)
def get_songs(value):
global user_df, songsList
songs_df = user_df[user_df['PlaylistName']==value]
songsList = []
options = []
for index, row in songs_df.iterrows():
songsList.append({'artist':row['ArtistName'],'song':row['SongName']})
options.append({'label':row['ArtistName'] + ' - ' + row['SongName'],'value':row['SongId']})
return len(songsList), options, options[0]['value']
@app.callback(
Output('sentiment-graph','figure'),
Input('analize', 'n_clicks'),
State('slider','value')
)
def show_graph(nclicks, value):
global songsList,user_df
songsList = songsList[0:value]
print(songsList)
res_df = em.getLyricsInfo(songsList)
#merged_df = user_df.merge(res_df,'left',left_on='SongName',right_on='title')
res_df.to_csv('carlos2.csv',index='false')
#merged_df.to_csv('carlos3.csv',index='false')
max_range = max(res_df['positive'].max(),res_df['negative'].max())
max_range *= 1.05
fig = []
fig = px.scatter(res_df,'positive', 'negative',range_x=[-0.01,max_range], range_y=[-0.01,max_range],symbol='artist',color="title")
return fig
@app.callback(
Output('explicit-graph','figure'),
Input('playlist','value')
)
def show_explicitness(value):
global user_df
songs = user_df[user_df['PlaylistName']==value]
explicitness = songs['SongIsExplicit'].tolist()
numYes = 0
numNo = 0
#Contamos las columnas de explicit
for song in explicitness:
if song == True:
numYes = numYes + 1
else:
numNo = numNo + 1
values = [numYes, numNo]
names = ['Explicita', 'Familiar']
fig = px.pie(values=values, names=names)
return fig
# songs_df = user_df[user_df['PlaylistName']==value]
# songsList = []
# for row in songs_df.iterrows():
# songsList.append({'explicit':row['SongIsExplicit']})
# @app.callback(
# Output('polar','figure'),
# Input('song1','value')
# )
# def show_polar(value):
# data,tempo = pp.getAudioFeatures(value)
# fig = px.line_polar(data, r='r', theta='theta', line_close=True)
# fig.update_traces(fill='toself')
# return fig
@app.callback(
Output('polar','figure'),
Input('playlist','valuePlay'),
Input('song1','value'),
Input('reset-polar', 'n_clicks'),
State('polar','figure')
)
def show_polar(valuePlaylist,value,clicks, figure):
ctx = callback_context
if not ctx.triggered:
button_id = 'No clicks yet'
else:
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
if(button_id=='reset-polar' or button_id=='playlist'):
figure['data'] = []
return figure
else:
data,tempo = pp.getAudioFeatures(value)
#print(figure)
if(figure['data']!=None):
figure['data'].append(go.Scatterpolar(r=data['r'],
theta=data['theta'],
fill='toself',
name=user_df.loc[user_df['SongId']==value, 'ArtistName'].iloc[0] + ' - ' + user_df.loc[user_df['SongId']==value, 'SongName'].iloc[0]
))
# else:
# figure['data']=[go.Scatterpolar(r=data['r'],
# theta=data['theta'],
# fill='toself',
# name = user_df.loc[user_df['SongId']==value, 'SongName'].iloc[0]
# #name=user_df[user_df['SongId']==value]['ArtistName'].item() + ' - ' + user_df[user_df['SongId']==value]['SongName'].item()
# )]
return figure
if __name__ == '__main__':
app.run_server(debug=False)