-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteract.py
More file actions
executable file
·276 lines (180 loc) · 8.49 KB
/
interact.py
File metadata and controls
executable file
·276 lines (180 loc) · 8.49 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
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
import config
from ext import cls
from data import note_dict, note_reverse_dict, empty_vector, normalize_vector
from data import convert_to_stream
from model import load_model, empty_states, prop_model_nocircuit
from circuit import find_backend, prop_circuit
from simu import prop_circuits
from torch import tensor, Tensor, zeros
from torch import no_grad, float32
from random import choices
##
states, outs, track = [], [], []
def main():
if not (model := load_model()):
input(f'Error: model not found, run training..')
return
global states, outs, track
if not states:
states = [empty_states(model)]
else:
possible_states = empty_states(model)
inherited_states = states[-1]
if len(possible_states) != len(inherited_states):
states = [empty_states(model)]
elif not all(layer_state_1.size(1)==layer_state_2.size(1) for layer_state_1,layer_state_2 in zip(possible_states,inherited_states)):
states = [empty_states(model)]
while 1:
cls()
if (inp := input('Select: \n\t(B)uild \n\t(S)how \n\t(P)lay \n\t(R)emoveLast \n\t(C)lear \n\n > ')) == '': break
cls()
if inp.lower()[0] == 'u':
toggle_online = False
time_signature = 4
if not outs:
if (inp:=input('Enter a note: ').upper()) != '':
if inp[-1] == ';':
toggle_online = True
inp = inp[:-1]
found = False
while not found:
found = all([e in note_dict.keys() for e in inp.split(',')])
if not found:
inp = input('> example: G,B,F# or g,b \nEnter a note: ').upper()
else: break
track.append(inp)
outs.append(human_2_ai(inp))
if outs:
try: hm_timesteps = (int(input('amount of bars: ')))*config.beat_resolution*time_signature
except:
hm_timesteps = config.hm_bars_grouped*config.beat_resolution*time_signature
print(f'> set to {config.hm_bars_grouped}')
if not toggle_online:
try:
config.note_pick_mode = int(input('note pick mode: '))
config.polyphony = (config.note_pick_mode != 3)
except: print(f'> set to {config.note_pick_mode}')
else:
config.polyphony = False
print(f'unrolling {hm_timesteps} steps..')
for _ in range(hm_timesteps):
if (_:=_+1)%50 == 0:
print(_)
out, state, [theo,exp,final_exp] = \
prop_model(model, states[-1], outs[-1], online_collapse=toggle_online)
states.append(state)
if toggle_online:
resp = ai_2_human(final_exp)
else:
if config.note_pick_mode == 0:
theoretical = [[note_reverse_dict[id], prob] for id, prob in enumerate(theo) if id < config.timestep_size]
theoretical = sorted(theoretical, key=lambda x: x[1], reverse=True)
resp = theoretical[0][0]
elif config.note_pick_mode == 1:
experimental = [[note_reverse_dict[id], prob] for id, prob in enumerate(exp) if id < config.timestep_size]
experimental = sorted(experimental, key=lambda x: x[1], reverse=True)
resp = experimental[0][0]
else:
resp = ai_2_human(out)
track.append(resp)
out_ = zeros(1,config.timestep_size)
out_ += out[:,:config.timestep_size]
for i in range(config.timestep_size, config.statevec_size):
out_[-1] += out[:,i]
outs.append(out_)
elif inp.lower()[0] == 'b':
enter_manually = True
toggle_online = False
while 1:
if enter_manually:
if (inp := str(input('> Enter a note: ')).upper()) == '':
break
if inp[-1] == ';':
toggle_online = True
inp = inp[:-1]
if all([e in note_dict.keys() for e in inp.split(',')]):
track.append(inp)
outs.append(human_2_ai(inp))
enter_manually = False
else:
out, state, [theo,exp,final_exp] = \
prop_model(model, states[-1], outs[-1], online_collapse=toggle_online)
states.append(state)
theoretical = [[note_reverse_dict[id],prob] for id,prob in enumerate(theo) if id<config.timestep_size]
theoretical = sorted(theoretical, key=lambda x: x[1], reverse=True)
experimental = [[note_reverse_dict[id],prob] for id,prob in enumerate(exp) if id<config.timestep_size]
experimental = sorted(experimental, key=lambda x: x[1], reverse=True)
print(f'\n> Theoretical q-state: {theoretical}\n')
print(f'> Experimental q-state: {experimental}\n')
if toggle_online:
print(f'\n> Real Experimental q-state: {final_exp}\n')
resp = ai_2_human(final_exp)
if input(f'> Collapsed to: {resp} - Keep it? (y/n): ').lower() == 'y':
track.append(resp)
out_ = zeros(1, config.timestep_size)
out_ += out[:,:config.timestep_size]
for i in range(config.timestep_size, config.statevec_size):
out_[-1] += out[:,i]
outs.append(out_)
enter_manually = False
print('collapse added')
else:
enter_manually = True
print('collapse not added')
if toggle_online:
toggle_online = False
elif inp.lower()[0] == 's' and track:
convert_to_stream(track).plot(title='my musiplot')
elif inp.lower()[0] == 'p' and track:
try:
convert_to_stream(track).show()
except Exception: pass
elif inp.lower()[0] == 'r' and track:
del track[-1]
del states[-1]
del outs[-1]
elif inp.lower()[0] == 'c':
outs, track, states = [], [], [empty_states(model)]
else:
pass
##
def human_2_ai(note_names):
vector = empty_vector.copy()
for note_name in note_names.split(','):
note_id = note_dict[note_name.upper()]
vector[note_id] +=1
vector = normalize_vector(vector)
return tensor(vector, dtype=float32).view(1,len(vector))
def prop_model(model, states, inp, online_collapse=False):
with no_grad():
out, new_states = prop_model_nocircuit(model,states,inp)
if config.act_classical_rnn:
print(f'output: {out}')
theoretical, experimental, real_experimental = [],[],[]
else:
theoretical = prop_circuit(out,inp,'theoretical')
experimental = prop_circuit(out,inp,'experimental')
real_experimental = experimental
if online_collapse:
real_experimental = prop_circuit(out,inp,'experimental',find_backend(),show_details=True,hm_trials=1 if not config.polyphony else 1_000)
out = prop_circuits(out, inp)
return out, new_states, [theoretical,experimental,real_experimental]
def ai_2_human(out):
out = out.flatten()
if not config.polyphony:
collapsed = choices(range(len(out)),weights=out,k=1)[0]
for i in range(len(out)):
out[i] = 1. if i==collapsed else .0
out_converted = ""
for ii, i in enumerate(out):
if i >= 1/config.statevec_size:
if element := note_reverse_dict.get(ii):
out_converted += element
else:
out_converted += "R"
out_converted += ","
out_converted = out_converted[:-1]
return out_converted
##
if __name__ == '__main__':
main()