|
import gradio as gr |
|
import torch |
|
from diffusers import StableAudioPipeline |
|
from huggingface_hub import hf_hub_download |
|
import spaces |
|
from translatepy import Translator |
|
import numpy as np |
|
import random |
|
import soundfile as sf |
|
|
|
translator = Translator() |
|
|
|
|
|
model = "stabilityai/stable-audio-open-1.0" |
|
|
|
|
|
CSS = """ |
|
.gradio-container { |
|
max-width: 690px !important; |
|
} |
|
footer { |
|
visibility: hidden; |
|
} |
|
""" |
|
|
|
JS = """function () { |
|
gradioURL = window.location.href |
|
if (!gradioURL.endsWith('?__theme=dark')) { |
|
window.location.replace(gradioURL + '?__theme=dark'); |
|
} |
|
}""" |
|
|
|
|
|
vae = AutoencoderKL.from_pretrained( |
|
vae_model, |
|
torch_dtype=torch.float16 |
|
) |
|
|
|
|
|
if torch.cuda.is_available(): |
|
pipe = StableAudioPipeline.from_pretrained( |
|
model, |
|
torch_dtype=torch.float16).to("cuda") |
|
|
|
|
|
|
|
@spaces.GPU(duration=120) |
|
def generate_image( |
|
prompt, |
|
negative="low quality", |
|
second: float = 10.0): |
|
|
|
|
|
|
|
|
|
|
|
|
|
prompt = str(translator.translate(prompt, 'English')) |
|
|
|
print(f'prompt:{prompt}') |
|
|
|
audio = pipe( |
|
prompt, |
|
negative_prompt=negative, |
|
audio_end_in_s=second, |
|
).audios |
|
|
|
os.makedirs("outputs", exist_ok=True) |
|
base_count = len(glob(os.path.join("outputs", "*.mp4"))) |
|
audio_path = os.path.join("outputs", f"{base_count:06d}.wav") |
|
|
|
sf.write(audio_path, audio[0].T.float().cpu().numpy(), pipe.vae.samping_rate) |
|
|
|
return audio_path |
|
|
|
|
|
|
|
with gr.Blocks(theme='soft', css=css, title="Stable Audio Open") as iface: |
|
with gr.Accordion(""): |
|
gr.Markdown(DESCRIPTION) |
|
with gr.Row(): |
|
output = gr.Audio(label="Podcast", type="filepath", interactive=False, autoplay=True, elem_classes="audio") |
|
with gr.Row(): |
|
prompt = gr.Textbox(label="Prompt", placeholder="1000 BPM percussive sound of water drops") |
|
with gr.Row(): |
|
negative = gr.Textbox(label="Negative prompt", placeholder="Low quality") |
|
second = gr.Slider(5.0, 60.0, value=10.0, label="Second", step=0.1), |
|
with gr.Row(): |
|
submit_btn = gr.Button("๐ Send") |
|
clear_btn = gr.ClearButton(output_box, value="๐๏ธ Clear") |
|
|
|
|
|
submit_btn.click(main, inputs=[prompt, negative, second], outputs=output) |
|
|
|
|
|
|
|
|
|
iface.queue().launch(show_api=False) |