mattricesound commited on
Commit
2f1f645
1 Parent(s): d2a2e3c

Add audio logging

Browse files
app.py CHANGED
@@ -21,6 +21,7 @@ import csv
21
  import torch
22
  import gradio as gr
23
  import numpy as np
 
24
  from audiocraft.data.audio_utils import convert_audio
25
  from audiocraft.data.audio import audio_write, audio_read
26
  from audiocraft.models import MusicGen
@@ -34,13 +35,14 @@ import huggingface_hub
34
  from huggingface_hub import Repository
35
  from datetime import datetime
36
 
37
- LOCAL = True
38
  USE_MIDI = True
39
 
40
  # LOGS
41
  DATASET_REPO_URL = "https://huggingface.co/datasets/soundsauce/soundsauce-logs"
42
  DATA_FILENAME = "ratings.csv"
43
  DATA_FILE = os.path.join("data", DATA_FILENAME)
 
44
  HF_TOKEN = os.environ.get("HF_TOKEN")
45
  print("is none?", HF_TOKEN is None)
46
 
@@ -69,16 +71,22 @@ crops = [(0, 5), (0, 10), (0, 15)]
69
  selected_melody = ""
70
  selected_crop = None
71
  selected_text = ""
 
72
 
73
 
74
  def store_message(message: dict):
75
- repo.git_pull()
76
- if message:
 
 
77
  with open(DATA_FILE, "a") as csvfile:
78
  writer = csv.DictWriter(csvfile, fieldnames=message.keys())
79
  writer.writerow(message)
 
 
 
80
  commit_url = repo.push_to_hub()
81
- print(commit_url)
82
 
83
 
84
  def _call_nostderr(*args, **kwargs):
@@ -149,6 +157,7 @@ def connect_to_endpoint():
149
 
150
 
151
  def _do_predictions(texts, melodies, duration, progress=False, **gen_kwargs):
 
152
  MODEL.set_generation_params(duration=duration, cfg_coef=5, **gen_kwargs)
153
  print("new batch", len(texts), texts, [None if m is None else (m[0], m[1].shape) for m in melodies])
154
  be = time.time()
@@ -166,16 +175,12 @@ def _do_predictions(texts, melodies, duration, progress=False, **gen_kwargs):
166
  melody = convert_audio(melody, sr, target_sr, target_ac)
167
  processed_melodies.append(melody)
168
 
169
- if any(m is not None for m in processed_melodies):
170
- outputs = MODEL.generate_with_chroma(
171
- descriptions=texts,
172
- melody_wavs=processed_melodies,
173
- melody_sample_rate=target_sr,
174
- progress=progress,
175
- )
176
- else:
177
- outputs = MODEL.generate(texts, progress=progress)
178
-
179
  outputs = outputs.detach().float()
180
 
181
  out_files = []
@@ -204,9 +209,10 @@ def _do_predictions(texts, melodies, duration, progress=False, **gen_kwargs):
204
 
205
  audio_write(
206
  d_filename, demucs_output, MODEL.sample_rate, strategy="loudness",
207
- loudness_headroom_db=16, loudness_compressor=True, add_suffix=False)
208
  out_files.append(d_filename)
209
  file_cleaner.add(d_filename)
 
210
  res = [out_file for out_file in out_files]
211
  for file in res:
212
  file_cleaner.add(file)
@@ -266,7 +272,7 @@ def crop_melody(melody_file, fname):
266
  audio_write(fname, melody, sr, strategy="loudness", loudness_headroom_db=16, loudness_compressor=True, add_suffix=False)
267
 
268
  def run_remote_model(text, melody, num_retries=3):
269
- global selected_text
270
  print("Running Audiocraft API model with text", text, "and melody", melody.split("/")[-1])
271
  result = client.predict(
272
  text, # str in 'Describe your music' Textbox component
@@ -287,7 +293,7 @@ def run_remote_model(text, melody, num_retries=3):
287
  # Load wav file, if there is an issue with audiocraft, file will not exist
288
  try:
289
  output, sr = audio_read(d_filename)
290
- except FileNotFoundError:
291
  print("Audiocraft API failed, trying again...")
292
  if num_retries == 0:
293
  print("Audiocraft API failed, returning empty file...")
@@ -308,25 +314,28 @@ def run_remote_model(text, melody, num_retries=3):
308
  output = output.cpu()
309
  demucs_output = demucs_output.cpu()
310
 
311
-
 
312
  audio_write(
313
  d_filename, demucs_output, 32000, strategy="loudness",
314
- loudness_headroom_db=16, loudness_compressor=True, add_suffix=False)
315
  file_cleaner.add(d_filename)
316
  selected_text = text
317
 
318
  print("Finished", text)
319
  print("Tempfiles currently stored: ", len(file_cleaner.files))
 
320
  return d_filename#, gr.File.update(value=d_filename, visible=True)
321
 
322
- def rating_callback(rating):
 
323
  rating_data = {
324
  "TEXT": selected_text,
325
  "MELODY": selected_melody,
326
  "CROP": selected_crop,
327
  "RATING": rating,
328
  "VERSION": "local" if LOCAL else "api",
329
- "TIME": str(datetime.now())
330
  }
331
  print(rating_data)
332
  store_message(rating_data)
@@ -361,6 +370,18 @@ def ui_full(launch_kwargs):
361
  with gr.Row():
362
  slider = gr.Slider(label="Rating", minimum=0, maximum=10, step=1, value=0, scale=2)
363
  submit_button = gr.Button("Submit Rating", scale=1)
 
 
 
 
 
 
 
 
 
 
 
 
364
 
365
  # file_download_no_drum = gr.File(label="Download", visible=False)
366
  # gr.Markdown(
@@ -393,6 +414,7 @@ def ui_full(launch_kwargs):
393
  ["Enchanting Kalimba Melodies atop Mystical Atmosphere"],
394
  ],
395
  inputs=[text],
 
396
  outputs=[output_without_drum]#, file_download_no_drum]
397
  )
398
 
@@ -408,9 +430,9 @@ if __name__ == "__main__":
408
  help='IP to listen on for connections to Gradio',
409
  )
410
  parser.add_argument("--local", action="store_true", help="Run locally instead of using API")
411
- parser.add_argument("--midi", action="store_true", help="Render midi instead of wav")
412
 
413
  args = parser.parse_args()
 
414
 
415
  launch_kwargs = {}
416
  launch_kwargs['server_name'] = args.listen
 
21
  import torch
22
  import gradio as gr
23
  import numpy as np
24
+ import shutil
25
  from audiocraft.data.audio_utils import convert_audio
26
  from audiocraft.data.audio import audio_write, audio_read
27
  from audiocraft.models import MusicGen
 
35
  from huggingface_hub import Repository
36
  from datetime import datetime
37
 
38
+ LOCAL = False
39
  USE_MIDI = True
40
 
41
  # LOGS
42
  DATASET_REPO_URL = "https://huggingface.co/datasets/soundsauce/soundsauce-logs"
43
  DATA_FILENAME = "ratings.csv"
44
  DATA_FILE = os.path.join("data", DATA_FILENAME)
45
+ AUDIO_DIR = os.path.join("data", "audio")
46
  HF_TOKEN = os.environ.get("HF_TOKEN")
47
  print("is none?", HF_TOKEN is None)
48
 
 
71
  selected_melody = ""
72
  selected_crop = None
73
  selected_text = ""
74
+ output_file = ""
75
 
76
 
77
  def store_message(message: dict):
78
+ if message and output_file:
79
+ if not os.path.exists(AUDIO_DIR):
80
+ os.makedirs(AUDIO_DIR)
81
+ repo.git_pull()
82
  with open(DATA_FILE, "a") as csvfile:
83
  writer = csv.DictWriter(csvfile, fieldnames=message.keys())
84
  writer.writerow(message)
85
+
86
+ filepath = os.path.join(AUDIO_DIR, message["TIME"]) + ".mp3"
87
+ shutil.copy(output_file, filepath)
88
  commit_url = repo.push_to_hub()
89
+ print("Commited to", commit_url)
90
 
91
 
92
  def _call_nostderr(*args, **kwargs):
 
157
 
158
 
159
  def _do_predictions(texts, melodies, duration, progress=False, **gen_kwargs):
160
+ global output_file
161
  MODEL.set_generation_params(duration=duration, cfg_coef=5, **gen_kwargs)
162
  print("new batch", len(texts), texts, [None if m is None else (m[0], m[1].shape) for m in melodies])
163
  be = time.time()
 
175
  melody = convert_audio(melody, sr, target_sr, target_ac)
176
  processed_melodies.append(melody)
177
 
178
+ outputs = MODEL.generate_with_chroma(
179
+ descriptions=texts,
180
+ melody_wavs=processed_melodies,
181
+ melody_sample_rate=target_sr,
182
+ progress=progress,
183
+ )
 
 
 
 
184
  outputs = outputs.detach().float()
185
 
186
  out_files = []
 
209
 
210
  audio_write(
211
  d_filename, demucs_output, MODEL.sample_rate, strategy="loudness",
212
+ loudness_headroom_db=16, loudness_compressor=True, add_suffix=False, format="mp3")
213
  out_files.append(d_filename)
214
  file_cleaner.add(d_filename)
215
+ output_file = d_filename
216
  res = [out_file for out_file in out_files]
217
  for file in res:
218
  file_cleaner.add(file)
 
272
  audio_write(fname, melody, sr, strategy="loudness", loudness_headroom_db=16, loudness_compressor=True, add_suffix=False)
273
 
274
  def run_remote_model(text, melody, num_retries=3):
275
+ global selected_text, output_file
276
  print("Running Audiocraft API model with text", text, "and melody", melody.split("/")[-1])
277
  result = client.predict(
278
  text, # str in 'Describe your music' Textbox component
 
293
  # Load wav file, if there is an issue with audiocraft, file will not exist
294
  try:
295
  output, sr = audio_read(d_filename)
296
+ except RuntimeError:
297
  print("Audiocraft API failed, trying again...")
298
  if num_retries == 0:
299
  print("Audiocraft API failed, returning empty file...")
 
314
  output = output.cpu()
315
  demucs_output = demucs_output.cpu()
316
 
317
+ file_cleaner.add(d_filename)
318
+ d_filename = d_filename.replace(".wav", ".mp3")
319
  audio_write(
320
  d_filename, demucs_output, 32000, strategy="loudness",
321
+ loudness_headroom_db=16, loudness_compressor=True, add_suffix=False, format="mp3")
322
  file_cleaner.add(d_filename)
323
  selected_text = text
324
 
325
  print("Finished", text)
326
  print("Tempfiles currently stored: ", len(file_cleaner.files))
327
+ output_file = d_filename
328
  return d_filename#, gr.File.update(value=d_filename, visible=True)
329
 
330
+ def rating_callback(rating: int):
331
+ timestamp = str(datetime.now())
332
  rating_data = {
333
  "TEXT": selected_text,
334
  "MELODY": selected_melody,
335
  "CROP": selected_crop,
336
  "RATING": rating,
337
  "VERSION": "local" if LOCAL else "api",
338
+ "TIME": timestamp
339
  }
340
  print(rating_data)
341
  store_message(rating_data)
 
370
  with gr.Row():
371
  slider = gr.Slider(label="Rating", minimum=0, maximum=10, step=1, value=0, scale=2)
372
  submit_button = gr.Button("Submit Rating", scale=1)
373
+ with gr.Accordion("Show Example Ratings", open=False):
374
+ gr.Markdown("""
375
+ ## Example Ratings
376
+ """)
377
+ gr.Audio(label="Rating = 0", value="examples/0-rating.mp3")
378
+ gr.Audio(label="Rating = 1", value="examples/1-rating.mp3")
379
+ gr.Audio(label="Rating = 2", value="examples/2-rating.mp3")
380
+ gr.Audio(label="Rating = 3", value="examples/3-rating.mp3")
381
+ gr.Audio(label="Rating = 4", value="examples/4-rating.mp3")
382
+ gr.Audio(label="Rating = 5", value="examples/5-rating.mp3")
383
+
384
+
385
 
386
  # file_download_no_drum = gr.File(label="Download", visible=False)
387
  # gr.Markdown(
 
414
  ["Enchanting Kalimba Melodies atop Mystical Atmosphere"],
415
  ],
416
  inputs=[text],
417
+ label="Example Inputs",
418
  outputs=[output_without_drum]#, file_download_no_drum]
419
  )
420
 
 
430
  help='IP to listen on for connections to Gradio',
431
  )
432
  parser.add_argument("--local", action="store_true", help="Run locally instead of using API")
 
433
 
434
  args = parser.parse_args()
435
+ LOCAL = args.local
436
 
437
  launch_kwargs = {}
438
  launch_kwargs['server_name'] = args.listen
clips/.DS_Store DELETED
Binary file (8.2 kB)
 
clips/Gems-Ethnic/.DS_Store DELETED
Binary file (6.15 kB)
 
examples/0-rating.mp3 ADDED
Binary file (154 kB). View file
 
examples/1-rating.mp3 ADDED
Binary file (139 kB). View file
 
examples/2-rating.mp3 ADDED
Binary file (142 kB). View file
 
examples/3-rating.mp3 ADDED
Binary file (143 kB). View file
 
examples/4-rating.mp3 ADDED
Binary file (205 kB). View file
 
examples/5-rating.mp3 ADDED
Binary file (205 kB). View file