ljcortesr
commited on
Commit
·
cc64d3e
1
Parent(s):
c69ee2f
Add application file
Browse files- audiogen_demo.py +42 -0
audiogen_demo.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torchaudio
|
| 2 |
+
from audiocraft.models import AudioGen
|
| 3 |
+
from audiocraft.data.audio import audio_write
|
| 4 |
+
import os
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
def generate_audio(descriptions):
|
| 8 |
+
if not os.path.exists('audio_files'):
|
| 9 |
+
os.makedirs('audio_files')
|
| 10 |
+
model = AudioGen.get_pretrained('facebook/audiogen-medium')
|
| 11 |
+
model.set_generation_params(duration=5) # generate 5 seconds.
|
| 12 |
+
wav = model.generate([descriptions]) # generates 3 samples.
|
| 13 |
+
results = []
|
| 14 |
+
|
| 15 |
+
for idx, one_wav in enumerate(wav):
|
| 16 |
+
filename = f'{descriptions}.wav'
|
| 17 |
+
file_path = os.path.join('audio_files', filename)
|
| 18 |
+
audio_write(file_path, one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True, add_suffix=False)
|
| 19 |
+
print(f"Generated audio for '{descriptions}'")
|
| 20 |
+
results.append(file_path)
|
| 21 |
+
|
| 22 |
+
return results[0]
|
| 23 |
+
|
| 24 |
+
def ui_full():
|
| 25 |
+
with gr. Blocks() as interface:
|
| 26 |
+
gr.Markdown(
|
| 27 |
+
"""
|
| 28 |
+
# AudioGen Demo
|
| 29 |
+
presented at: [Simple and Controllable Music Generation](https://huggingface.co/)
|
| 30 |
+
"""
|
| 31 |
+
)
|
| 32 |
+
with gr.Row():
|
| 33 |
+
descriptions = gr.Textbox(lines=2, label="Enter descriptions of the audio to generate")
|
| 34 |
+
with gr.Row():
|
| 35 |
+
generate_button = gr.Button("Generate Audio")
|
| 36 |
+
with gr.Row():
|
| 37 |
+
output = gr.Audio(label="Generated Audio")
|
| 38 |
+
|
| 39 |
+
generate_button.click(fn=generate_audio, inputs=descriptions, outputs=[output])
|
| 40 |
+
interface.queue().launch()
|
| 41 |
+
|
| 42 |
+
ui_full()
|