Spaces:
Runtime error
Runtime error
Update app.py
#1
by
DanLeBossDeESGI
- opened
app.py
CHANGED
|
@@ -2,15 +2,315 @@ import streamlit as st
|
|
| 2 |
from PIL import Image, ImageDraw
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Sélection de l'image
|
| 8 |
-
uploaded_image = st.file_uploader("Sélectionnez une image", type=["jpg", "png", "jpeg"])
|
| 9 |
|
| 10 |
-
|
| 11 |
-
# Charger l'image
|
| 12 |
-
image = Image.open(uploaded_image)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
| 2 |
from PIL import Image, ImageDraw
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
#@title Standard/Simple Continuation
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
#@markdown Text-To-Music Settings
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
#@markdown NOTE: You can enter any desired title or artist, or both
|
| 10 |
+
|
| 11 |
+
enter_desired_song_title = "Family Guy" #@param {type:"string"}
|
| 12 |
+
enter_desired_artist = "TV Themes" #@param {type:"string"}
|
| 13 |
+
|
| 14 |
+
#@markdown Generation Settings
|
| 15 |
+
|
| 16 |
+
number_of_tokens_to_generate = 426 #@param {type:"slider", min:30, max:2046, step:33}
|
| 17 |
+
number_of_batches_to_generate = 4 #@param {type:"slider", min:1, max:16, step:1}
|
| 18 |
+
temperature = 0.9 #@param {type:"slider", min:0.1, max:1, step:0.1}
|
| 19 |
+
allow_model_to_stop_generation_if_needed = False #@param {type:"boolean"}
|
| 20 |
+
|
| 21 |
+
print('=' * 70)
|
| 22 |
+
print('Euterpe X TTM Model Generator')
|
| 23 |
+
print('=' * 70)
|
| 24 |
+
|
| 25 |
+
print('Searching titles...Please wait...')
|
| 26 |
+
random.shuffle(AUX_DATA)
|
| 27 |
+
|
| 28 |
+
titles_index = []
|
| 29 |
+
|
| 30 |
+
for A in AUX_DATA:
|
| 31 |
+
titles_index.append(A[0])
|
| 32 |
+
|
| 33 |
+
search_string = ''
|
| 34 |
+
|
| 35 |
+
if enter_desired_song_title != '' and enter_desired_artist != '':
|
| 36 |
+
search_string = enter_desired_song_title + ' --- ' + enter_desired_artist
|
| 37 |
+
|
| 38 |
+
else:
|
| 39 |
+
search_string = enter_desired_song_title + enter_desired_artist
|
| 40 |
+
|
| 41 |
+
search_match = process.extract(query=search_string, choices=titles_index, limit=1)
|
| 42 |
+
search_index = titles_index.index(search_match[0][0])
|
| 43 |
+
|
| 44 |
+
print('Done!')
|
| 45 |
+
print('=' * 70)
|
| 46 |
+
print('Selected title:', AUX_DATA[search_index][0])
|
| 47 |
+
print('=' * 70)
|
| 48 |
+
|
| 49 |
+
if allow_model_to_stop_generation_if_needed:
|
| 50 |
+
min_stop_token = 3343
|
| 51 |
+
else:
|
| 52 |
+
min_stop_token = None
|
| 53 |
+
|
| 54 |
+
# Velocities
|
| 55 |
+
velocities_map = [80, 80, 70, 100, 90, 80, 100, 100, 100, 90, 110, 100]
|
| 56 |
+
vel_map = AUX_DATA[search_index][1]
|
| 57 |
+
|
| 58 |
+
for i in range(12):
|
| 59 |
+
if vel_map[i] != 0:
|
| 60 |
+
velocities_map[i] = vel_map[i]
|
| 61 |
+
|
| 62 |
+
# Loading data...
|
| 63 |
+
outy = AUX_DATA[search_index][2][3:]
|
| 64 |
+
|
| 65 |
+
block_marker = sum([(y * 8) for y in outy if y < 256]) / 1000
|
| 66 |
+
|
| 67 |
+
inp = [outy] * number_of_batches_to_generate
|
| 68 |
+
|
| 69 |
+
inp = torch.LongTensor(inp).cuda()
|
| 70 |
+
|
| 71 |
+
out = model.module.generate(inp,
|
| 72 |
+
number_of_tokens_to_generate,
|
| 73 |
+
temperature=temperature,
|
| 74 |
+
return_prime=True,
|
| 75 |
+
eos_token=min_stop_token,
|
| 76 |
+
verbose=True)
|
| 77 |
+
|
| 78 |
+
out0 = out.tolist()
|
| 79 |
+
print('=' * 70)
|
| 80 |
+
print('Done!')
|
| 81 |
+
print('=' * 70)
|
| 82 |
+
#======================================================================
|
| 83 |
+
print('Rendering results...')
|
| 84 |
+
|
| 85 |
+
for i in range(number_of_batches_to_generate):
|
| 86 |
+
|
| 87 |
+
print('=' * 70)
|
| 88 |
+
print('Batch #', i)
|
| 89 |
+
print('=' * 70)
|
| 90 |
+
|
| 91 |
+
out1 = out0[i]
|
| 92 |
+
|
| 93 |
+
print('Sample INTs', out1[:12])
|
| 94 |
+
print('=' * 70)
|
| 95 |
+
|
| 96 |
+
if len(out) != 0:
|
| 97 |
+
|
| 98 |
+
song = out1
|
| 99 |
+
song_f = []
|
| 100 |
+
|
| 101 |
+
time = 0
|
| 102 |
+
dur = 0
|
| 103 |
+
channel = 0
|
| 104 |
+
pitch = 0
|
| 105 |
+
vel = 90
|
| 106 |
+
|
| 107 |
+
for ss in song:
|
| 108 |
+
|
| 109 |
+
if ss > 0 and ss < 256:
|
| 110 |
+
|
| 111 |
+
time += ss * 8
|
| 112 |
+
|
| 113 |
+
if ss >= 256 and ss < 256+(12*128):
|
| 114 |
+
|
| 115 |
+
dur = ((ss-256) % 128) * 30
|
| 116 |
+
|
| 117 |
+
if ss >= 256+(12*128) and ss < 256+(12*128)+(12*128):
|
| 118 |
+
channel = (ss-(256+(12*128))) // 128
|
| 119 |
+
pitch = (ss-(256+(12*128))) % 128
|
| 120 |
+
vel = velocities_map[channel]
|
| 121 |
+
|
| 122 |
+
song_f.append(['note', time, dur, channel, pitch, vel ])
|
| 123 |
+
|
| 124 |
+
detailed_stats = TMIDIX.Tegridy_SONG_to_MIDI_Converter(song_f,
|
| 125 |
+
output_signature = 'Euterpe X',
|
| 126 |
+
output_file_name = '/content/Euterpe-X-Music-Composition_'+str(i),
|
| 127 |
+
track_name='Project Los Angeles',
|
| 128 |
+
list_of_MIDI_patches=[0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 19, 0, 0, 0, 0],
|
| 129 |
+
number_of_ticks_per_quarter=500)
|
| 130 |
+
print('=' * 70)
|
| 131 |
+
print('Displaying resulting composition...')
|
| 132 |
+
print('=' * 70)
|
| 133 |
+
|
| 134 |
+
fname = '/content/Euterpe-X-Music-Composition_'+str(i)
|
| 135 |
+
|
| 136 |
+
x = []
|
| 137 |
+
y =[]
|
| 138 |
+
c = []
|
| 139 |
+
|
| 140 |
+
colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver']
|
| 141 |
+
|
| 142 |
+
for s in song_f:
|
| 143 |
+
x.append(s[1] / 1000)
|
| 144 |
+
y.append(s[4])
|
| 145 |
+
c.append(colors[s[3]])
|
| 146 |
+
|
| 147 |
+
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))
|
| 148 |
+
display(Audio(str(fname + '.wav'), rate=16000))
|
| 149 |
+
|
| 150 |
+
plt.figure(figsize=(14,5))
|
| 151 |
+
ax=plt.axes(title=fname)
|
| 152 |
+
ax.set_facecolor('black')
|
| 153 |
+
|
| 154 |
+
plt.scatter(x,y, c=c)
|
| 155 |
+
|
| 156 |
+
ax.axvline(x=block_marker, c='w')
|
| 157 |
+
|
| 158 |
+
plt.xlabel("Time")
|
| 159 |
+
plt.ylabel("Pitch")
|
| 160 |
+
plt.show()#@title Standard/Simple Continuation
|
| 161 |
+
|
| 162 |
+
#@markdown Text-To-Music Settings
|
| 163 |
+
|
| 164 |
+
#@markdown NOTE: You can enter any desired title or artist, or both
|
| 165 |
+
|
| 166 |
+
enter_desired_song_title = "Family Guy" #@param {type:"string"}
|
| 167 |
+
enter_desired_artist = "TV Themes" #@param {type:"string"}
|
| 168 |
+
|
| 169 |
+
#@markdown Generation Settings
|
| 170 |
+
|
| 171 |
+
number_of_tokens_to_generate = 426 #@param {type:"slider", min:30, max:2046, step:33}
|
| 172 |
+
number_of_batches_to_generate = 4 #@param {type:"slider", min:1, max:16, step:1}
|
| 173 |
+
temperature = 0.9 #@param {type:"slider", min:0.1, max:1, step:0.1}
|
| 174 |
+
allow_model_to_stop_generation_if_needed = False #@param {type:"boolean"}
|
| 175 |
+
|
| 176 |
+
print('=' * 70)
|
| 177 |
+
print('Euterpe X TTM Model Generator')
|
| 178 |
+
print('=' * 70)
|
| 179 |
+
|
| 180 |
+
print('Searching titles...Please wait...')
|
| 181 |
+
random.shuffle(AUX_DATA)
|
| 182 |
+
|
| 183 |
+
titles_index = []
|
| 184 |
+
|
| 185 |
+
for A in AUX_DATA:
|
| 186 |
+
titles_index.append(A[0])
|
| 187 |
+
|
| 188 |
+
search_string = ''
|
| 189 |
+
|
| 190 |
+
if enter_desired_song_title != '' and enter_desired_artist != '':
|
| 191 |
+
search_string = enter_desired_song_title + ' --- ' + enter_desired_artist
|
| 192 |
+
|
| 193 |
+
else:
|
| 194 |
+
search_string = enter_desired_song_title + enter_desired_artist
|
| 195 |
+
|
| 196 |
+
search_match = process.extract(query=search_string, choices=titles_index, limit=1)
|
| 197 |
+
search_index = titles_index.index(search_match[0][0])
|
| 198 |
+
|
| 199 |
+
print('Done!')
|
| 200 |
+
print('=' * 70)
|
| 201 |
+
print('Selected title:', AUX_DATA[search_index][0])
|
| 202 |
+
print('=' * 70)
|
| 203 |
+
|
| 204 |
+
if allow_model_to_stop_generation_if_needed:
|
| 205 |
+
min_stop_token = 3343
|
| 206 |
+
else:
|
| 207 |
+
min_stop_token = None
|
| 208 |
+
|
| 209 |
+
# Velocities
|
| 210 |
+
velocities_map = [80, 80, 70, 100, 90, 80, 100, 100, 100, 90, 110, 100]
|
| 211 |
+
vel_map = AUX_DATA[search_index][1]
|
| 212 |
+
|
| 213 |
+
for i in range(12):
|
| 214 |
+
if vel_map[i] != 0:
|
| 215 |
+
velocities_map[i] = vel_map[i]
|
| 216 |
+
|
| 217 |
+
# Loading data...
|
| 218 |
+
outy = AUX_DATA[search_index][2][3:]
|
| 219 |
+
|
| 220 |
+
block_marker = sum([(y * 8) for y in outy if y < 256]) / 1000
|
| 221 |
+
|
| 222 |
+
inp = [outy] * number_of_batches_to_generate
|
| 223 |
+
|
| 224 |
+
inp = torch.LongTensor(inp).cuda()
|
| 225 |
+
|
| 226 |
+
out = model.module.generate(inp,
|
| 227 |
+
number_of_tokens_to_generate,
|
| 228 |
+
temperature=temperature,
|
| 229 |
+
return_prime=True,
|
| 230 |
+
eos_token=min_stop_token,
|
| 231 |
+
verbose=True)
|
| 232 |
+
|
| 233 |
+
out0 = out.tolist()
|
| 234 |
+
print('=' * 70)
|
| 235 |
+
print('Done!')
|
| 236 |
+
print('=' * 70)
|
| 237 |
+
#======================================================================
|
| 238 |
+
print('Rendering results...')
|
| 239 |
+
|
| 240 |
+
for i in range(number_of_batches_to_generate):
|
| 241 |
+
|
| 242 |
+
print('=' * 70)
|
| 243 |
+
print('Batch #', i)
|
| 244 |
+
print('=' * 70)
|
| 245 |
+
|
| 246 |
+
out1 = out0[i]
|
| 247 |
+
|
| 248 |
+
print('Sample INTs', out1[:12])
|
| 249 |
+
print('=' * 70)
|
| 250 |
+
|
| 251 |
+
if len(out) != 0:
|
| 252 |
+
|
| 253 |
+
song = out1
|
| 254 |
+
song_f = []
|
| 255 |
+
|
| 256 |
+
time = 0
|
| 257 |
+
dur = 0
|
| 258 |
+
channel = 0
|
| 259 |
+
pitch = 0
|
| 260 |
+
vel = 90
|
| 261 |
+
|
| 262 |
+
for ss in song:
|
| 263 |
+
|
| 264 |
+
if ss > 0 and ss < 256:
|
| 265 |
+
|
| 266 |
+
time += ss * 8
|
| 267 |
+
|
| 268 |
+
if ss >= 256 and ss < 256+(12*128):
|
| 269 |
+
|
| 270 |
+
dur = ((ss-256) % 128) * 30
|
| 271 |
+
|
| 272 |
+
if ss >= 256+(12*128) and ss < 256+(12*128)+(12*128):
|
| 273 |
+
channel = (ss-(256+(12*128))) // 128
|
| 274 |
+
pitch = (ss-(256+(12*128))) % 128
|
| 275 |
+
vel = velocities_map[channel]
|
| 276 |
+
|
| 277 |
+
song_f.append(['note', time, dur, channel, pitch, vel ])
|
| 278 |
+
|
| 279 |
+
detailed_stats = TMIDIX.Tegridy_SONG_to_MIDI_Converter(song_f,
|
| 280 |
+
output_signature = 'Euterpe X',
|
| 281 |
+
output_file_name = '/content/Euterpe-X-Music-Composition_'+str(i),
|
| 282 |
+
track_name='Project Los Angeles',
|
| 283 |
+
list_of_MIDI_patches=[0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 19, 0, 0, 0, 0],
|
| 284 |
+
number_of_ticks_per_quarter=500)
|
| 285 |
+
print('=' * 70)
|
| 286 |
+
print('Displaying resulting composition...')
|
| 287 |
+
print('=' * 70)
|
| 288 |
+
|
| 289 |
+
fname = '/content/Euterpe-X-Music-Composition_'+str(i)
|
| 290 |
+
|
| 291 |
+
x = []
|
| 292 |
+
y =[]
|
| 293 |
+
c = []
|
| 294 |
+
|
| 295 |
+
colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver']
|
| 296 |
+
|
| 297 |
+
for s in song_f:
|
| 298 |
+
x.append(s[1] / 1000)
|
| 299 |
+
y.append(s[4])
|
| 300 |
+
c.append(colors[s[3]])
|
| 301 |
+
|
| 302 |
+
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))
|
| 303 |
+
display(Audio(str(fname + '.wav'), rate=16000))
|
| 304 |
+
|
| 305 |
+
plt.figure(figsize=(14,5))
|
| 306 |
+
ax=plt.axes(title=fname)
|
| 307 |
+
ax.set_facecolor('black')
|
| 308 |
+
|
| 309 |
+
plt.scatter(x,y, c=c)
|
| 310 |
+
|
| 311 |
+
ax.axvline(x=block_marker, c='w')
|
| 312 |
+
|
| 313 |
+
plt.xlabel("Time")
|
| 314 |
+
plt.ylabel("Pitch")
|
| 315 |
+
plt.show()
|
| 316 |
|