-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·283 lines (254 loc) · 9.14 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from dash import Dash, html, dcc, ctx, ALL,no_update
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
# from statsbombpy import sb
import pandas as pd
from datetime import datetime
import os
import glob
import time
import pickle
import logging
import utils
# --------------- Global vars -------------------------
PATH_EVENTS = 'assets/data/events/'
SOUNDFONT = 'assets/soundfont/GeneralUser.sf2'
from common import DEFAULT_MAIN,DEFAULT_COMP_ID,DEFAULT_MATCH_ID,DEFAULT_SZN_ID
from common import DRUM_INSTRUMENTS,MAIN_INSTRUMENTS
from common import COMPS,COMP_ITN,COMP_TO_SZNS,SZN_ITN,SZN_TO_MATCHES,MATCHES_ITN
from common import NAME_TO_NICKNAME
# --------------- Components -------------------------
card_instructions = dbc.Card(
dbc.CardBody(
[
html.Div([
html.P(
"Create a song using a game's data. Each player is a note, and each event (foul, goal) is a drum sound."
)
], className='',id='p-instr'),
]
,id='card-instr-body')
,id='card-instr')
card_dropdowns = dbc.Card(
dbc.CardBody(
[
html.Div([
dcc.Dropdown(
className='dropdown',
id='dd-comp', clearable=False,
options=[{'label':comp_name,'value':comp_id}
for comp_id, comp_name in COMP_ITN.items()],
value=DEFAULT_COMP_ID,
persistence=False
),
], className='div-dropdown',id='div-dd-comp'),
html.Div([
dcc.Dropdown(
className='dropdown',
id='dd-szn', clearable=False,
options= [{'label': SZN_ITN[szn_id],'value' : szn_id }
for szn_id in COMP_TO_SZNS[DEFAULT_COMP_ID]],
value=DEFAULT_SZN_ID,
persistence=True
)
], className='div-dropdown',id='div-dd-szn'),
html.Div([
dcc.Dropdown(
className='dropdown',
id='dd-match', clearable=False,
options= [{'label': MATCHES_ITN[match_id],'value' : match_id }
for match_id in SZN_TO_MATCHES[DEFAULT_SZN_ID]],
value=DEFAULT_MATCH_ID,
persistence=True
)
], className='div-dropdown',id='div-dd-match')
]
,id='card-dd-body')
,id='card-dd')
card_players_to_notes = dbc.Card([
dbc.CardImg(
src="assets/imgs/mbappe_dance.png",
style={"opacity": 0.3},
id = 'card-players-img'
),
dbc.CardImgOverlay(
dbc.CardBody([
html.Div(id='div-players'),
dbc.Button('Shuffle', id='btn-shuffle', n_clicks=0)
],id='card-players-body')
)
],id='card-players')
card_choose_instruments = dbc.Card(
dbc.CardBody([
html.Div([
html.Label("Ball possession"),
dcc.Dropdown(
className='dropdown',
id='dd-main-instrument', clearable=False,
value=DEFAULT_MAIN,
options = MAIN_INSTRUMENTS,
persistence=True
)
]),
html.Div([
html.Label("Events"),
dcc.Dropdown(
className='dropdown',
id='dd-drum-instrument', clearable=False,
value='SteelDrum',
options = DRUM_INSTRUMENTS,
persistence=True
)
])
]),
)
card_play = dbc.Card(
dbc.CardBody([
dbc.Button('Generate song', id='btn-generate', n_clicks=0),
dcc.Loading(id="song-loading",className='loading-msg',style={'display': 'inline-block'},
children=[html.Div(id="song-loading-output")], type="default"),
dbc.Button('Load song', id='btn-load', n_clicks=0),
html.Div(id='div-play'),
html.Div(id='div-play-mb'),
])
)
card_summary = dbc.Card(
dbc.CardBody([
html.Div(id='div-match-summary'),
],id='card_summary_body'),id='card_summary'
)
# --------------- Build App -------------------------
app = Dash(__name__,
external_stylesheets=[dbc.themes.BOOTSTRAP],
suppress_callback_exceptions=True,
meta_tags = [
{"name": "viewport", "content": "width=device-width, initial-scale=1"}
]
)
server = app.server
app.css.config.serve_locally = True
app.scripts.config.serve_locally = True
app.layout = html.Div([
html.Img(src='assets/imgs/background3.png',id='main-background-img'),
dbc.Container([
dcc.Store(id='store-notes'),
dcc.Store(id='store-events'),
dcc.Store(id='store-players'),
dcc.Store(id='store-timestr'),
dcc.Location(id='url', refresh=True),
dbc.Row(dbc.Col(html.H1("Football Opera"),id='title-row',className='text-center')),
dbc.Row([
dbc.Col(card_instructions)
]),
dbc.Row([
dbc.Col(card_dropdowns)
],id='row-dd'),
dbc.Row([
dbc.Col([
card_choose_instruments,
card_play,
card_summary,
],xs=12,sm=12,md=12,lg=6,xl=6,id='col-load'),
dbc.Col([
card_players_to_notes,
],id='col-players',
xs=12,sm=12,md=12,lg=6,xl=6),
], id='row-main'),
],fluid=False,id='container')
])
# --------------- Callbacks -------------------------
# Generate music
@app.callback(
Output('store-timestr', 'data'),
Output('song-loading-output', 'children'),
Output('div-match-summary', 'children'),
Input("btn-generate", "n_clicks"),
State("store-events", "data"),
State("store-notes","data"),
State("dd-main-instrument","value"),
State("dd-drum-instrument","value"),
prevent_initial_call=True
)
def generate_music(n_clicks,events,dnotes,main_instrument,drum_instrument):
if n_clicks>0:
df_events = pd.DataFrame(events)
# delete previous file in assets
for filename in glob.glob("assets/tmp-wav*"):
os.remove(filename)
timestr = datetime.now().strftime("%d%m%y_%H%M%S")
start_time = time.time()
summary = utils.generate_music21(df_events,
dnotes,
main_instrument,
drum_instrument,
timestr,
soundfont= SOUNDFONT)
dt = time.time()-start_time
logging.warning(f'generating music took {dt}')
print(f'generating music took {dt}')
return timestr, 'Music generated!',utils.make_summary(summary)
# Load music
@app.callback(
Output('div-play', 'children'),
Input("btn-load", "n_clicks"),
State("store-timestr", "data"),
prevent_initial_call=True
)
def load_music(n_clicks,timestr):
return utils.get_player(timestr)
# Callback to display players
@app.callback(
Output('div-players', 'children'),
Output('store-notes','data'),
[Input("store-players", "data"),
Input("btn-shuffle", "n_clicks")
],
)
def update_players_and_notes(players,n_clicks):
start_time = time.time()
dnotes = utils.sample_notes(players,music21=True)
layout = html.Table([
html.Thead([html.Tr([html.Th('Player'),html.Th('Note')])]),
html.Tbody([html.Tr([html.Td(NAME_TO_NICKNAME[player]),html.Td(note,className='td-note')]) for player,note in dnotes.items()])])
logging.warning(f'Updating players took {time.time()-start_time}')
return layout ,dnotes
@app.callback(
Output('dd-szn', 'options'),
Output('dd-szn', 'value'),
[Input("dd-comp", "value")],
prevent_initial_call=True
)
def update_szn_options(comp_id):
options = [{'value' : szn_id ,'label': SZN_ITN[szn_id]}
for szn_id in COMP_TO_SZNS[comp_id]]
return options,options[0]["value"]
@app.callback(
Output('dd-match', 'options'),
Output('dd-match', 'value'),
[Input("dd-szn", "value")],
prevent_initial_call=True
)
def update_matches_options(szn_id):
options = [{'value' : match_id ,'label': MATCHES_ITN[match_id]}
for match_id in SZN_TO_MATCHES[szn_id]]
return options,options[0]['value']
@app.callback(
Output('store-events', 'data'),
Output('store-players','data'),
Input('dd-match', 'value'),
)
def store_events(match_id):
start_time = time.time()
df_events = pd.read_parquet(PATH_EVENTS+f'events_match{match_id}.parquet')
# df_events = sb.events(match_id=match_id)
dt = time.time()-start_time
logging.warning(f'fetching events took {dt}')
# print(f'fetching events took {dt}')
players = list(df_events['player'].dropna().unique())
events = df_events.to_dict("records")
logging.warning(f'fetching events 2 took {time.time()-start_time}')
return events,players
# --------------- __main__ -------------------------
if __name__ == '__main__':
app.run_server(debug=True,port=8000,dev_tools_hot_reload = False)
# app.run_server(debug=True,port=8000)