jbilcke-hf HF staff commited on
Commit
65434a9
1 Parent(s): bad5667

Update demos/musicgen_app.py

Browse files
Files changed (1) hide show
  1. demos/musicgen_app.py +16 -8
demos/musicgen_app.py CHANGED
@@ -29,6 +29,9 @@ from audiocraft.data.audio import audio_write
29
  from audiocraft.models.encodec import InterleaveStereoCompressionModel
30
  from audiocraft.models import MusicGen, MultiBandDiffusion
31
 
 
 
 
32
  SECRET_TOKEN = os.getenv('SECRET_TOKEN', 'default_secret')
33
 
34
  MODEL = None # Last used model
@@ -214,18 +217,23 @@ def predict_full(secret_token, model, model_path, decoder, text, melody, duratio
214
  wav_path = wavs[0]
215
  if USE_DIFFUSION:
216
  wav_path = wavs[1]
217
-
218
-
219
  wav_base64 = ""
220
 
221
- # Read the content of the video file and encode it to base64
222
- with open(wav_path, "rb") as wav_file:
223
- wav_base64 = base64.b64encode(wav_file.read()).decode('utf-8')
224
 
225
- # Prepend the appropriate data URI header with MIME type
226
- wav_base64_data_uri = 'data:audio/wav;base64,' + wav_base64
 
 
 
 
 
 
 
 
 
 
227
 
228
- return wav_base64_data_uri
229
 
230
  def toggle_audio_src(choice):
231
  if choice == "mic":
 
29
  from audiocraft.models.encodec import InterleaveStereoCompressionModel
30
  from audiocraft.models import MusicGen, MultiBandDiffusion
31
 
32
+ from pydub import AudioSegment
33
+ import io
34
+
35
  SECRET_TOKEN = os.getenv('SECRET_TOKEN', 'default_secret')
36
 
37
  MODEL = None # Last used model
 
217
  wav_path = wavs[0]
218
  if USE_DIFFUSION:
219
  wav_path = wavs[1]
 
 
220
  wav_base64 = ""
221
 
 
 
 
222
 
223
+ # Convert WAV to MP3
224
+ mp3_path = wav_path.replace(".wav", ".mp3")
225
+ sound = AudioSegment.from_wav(wav_path)
226
+ sound.export(mp3_path, format="mp3")
227
+
228
+ # Encode the MP3 file to base64
229
+ mp3_base64 = ""
230
+ with open(mp3_path, "rb") as mp3_file:
231
+ mp3_base64 = base64.b64encode(mp3_file.read()).decode('utf-8')
232
+
233
+ # Prepend the appropriate data URI header
234
+ mp3_base64_data_uri = 'data:audio/mp3;base64,' + mp3_base64
235
 
236
+ return mp3_base64_data_uri
237
 
238
  def toggle_audio_src(choice):
239
  if choice == "mic":