Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
import scipy.io.wavfile as wav
|
| 2 |
import io
|
| 3 |
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
| 4 |
from IPython.display import Audio
|
| 5 |
import torch
|
| 6 |
import streamlit as st
|
|
|
|
| 7 |
|
| 8 |
def mu_gen(prompt):
|
| 9 |
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
|
|
@@ -13,7 +13,7 @@ def mu_gen(prompt):
|
|
| 13 |
model.to(device)
|
| 14 |
|
| 15 |
inputs = processor(
|
| 16 |
-
text=[str(prompt)],
|
| 17 |
padding=True,
|
| 18 |
return_tensors="pt",
|
| 19 |
)
|
|
@@ -25,11 +25,15 @@ def mu_gen(prompt):
|
|
| 25 |
sampling_rate = model.config.audio_encoder.sampling_rate
|
| 26 |
|
| 27 |
# Convert audio data to WAV format
|
| 28 |
-
wav_data = audio_values[0].numpy()
|
| 29 |
|
| 30 |
# Create an in-memory WAV file
|
| 31 |
with io.BytesIO() as wav_file:
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
wav_bytes = wav_file.getvalue()
|
| 34 |
|
| 35 |
return wav_bytes # Return the WAV audio data as bytes
|
|
|
|
|
|
|
| 1 |
import io
|
| 2 |
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
| 3 |
from IPython.display import Audio
|
| 4 |
import torch
|
| 5 |
import streamlit as st
|
| 6 |
+
import wave
|
| 7 |
|
| 8 |
def mu_gen(prompt):
|
| 9 |
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
|
|
|
|
| 13 |
model.to(device)
|
| 14 |
|
| 15 |
inputs = processor(
|
| 16 |
+
text=[str(prompt)],
|
| 17 |
padding=True,
|
| 18 |
return_tensors="pt",
|
| 19 |
)
|
|
|
|
| 25 |
sampling_rate = model.config.audio_encoder.sampling_rate
|
| 26 |
|
| 27 |
# Convert audio data to WAV format
|
| 28 |
+
wav_data = audio_values[0].numpy()
|
| 29 |
|
| 30 |
# Create an in-memory WAV file
|
| 31 |
with io.BytesIO() as wav_file:
|
| 32 |
+
with wave.open(wav_file, 'wb') as wf:
|
| 33 |
+
wf.setnchannels(1) # Mono audio
|
| 34 |
+
wf.setsampwidth(2) # 16-bit audio
|
| 35 |
+
wf.setframerate(sampling_rate)
|
| 36 |
+
wf.writeframes(wav_data.tobytes())
|
| 37 |
wav_bytes = wav_file.getvalue()
|
| 38 |
|
| 39 |
return wav_bytes # Return the WAV audio data as bytes
|