Spaces:
Running on Zero
Running on Zero
File size: 4,248 Bytes
db449e5 03ba8f9 db449e5 03ba8f9 87b1fa8 03ba8f9 b30a148 03ba8f9 87b1fa8 03ba8f9 a853813 03ba8f9 80b8c59 03ba8f9 a853813 03ba8f9 1f9373b a853813 03ba8f9 0d01db3 03ba8f9 efe1041 a853813 03ba8f9 efe1041 03ba8f9 f7a709f 0d01db3 f9f7be8 efe1041 f7a709f 0d01db3 efe1041 f7a709f efe1041 ea078e0 03ba8f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | import spaces
import os
import random
import gradio as gr
import numpy as np
import torch
from diffusers import StableAudioPipeline
MODEL_ID = "alfredplpl/SAO-Re-2"
token=os.environ["HF_TOKEN"]
device = "cuda"
dtype = torch.bfloat16
pipe = StableAudioPipeline.from_pretrained(
MODEL_ID,
torch_dtype=dtype,
token=token
)
pipe = pipe.to(device)
if device == "cuda":
pipe.enable_model_cpu_offload()
@spaces.GPU
def generate_audio(
prompt: str,
negative_prompt: str,
duration: float,
steps: int,
guidance_scale: float,
seed: int,
):
if not prompt.strip():
raise gr.Error("Promptを入力してください。")
if seed < 0:
seed = random.randint(0, 2**32 - 1)
generator = torch.Generator(device=device).manual_seed(seed)
with torch.inference_mode():
audio = pipe(
prompt=prompt,
negative_prompt=negative_prompt or None,
num_inference_steps=steps,
guidance_scale=guidance_scale,
audio_end_in_s=duration,
num_waveforms_per_prompt=1,
generator=generator,
).audios[0]
# diffusers StableAudio: [channels, samples] torch tensor の想定
audio = audio.T.float().cpu().numpy()
# Gradio向けに軽く安全化
audio = np.nan_to_num(audio)
peak = np.max(np.abs(audio))
if peak > 1.0:
audio = audio / peak
sample_rate = pipe.vae.sampling_rate
return (sample_rate, audio), seed
with gr.Blocks(title="Text-to-Audio") as demo:
gr.Markdown(
"""
# Text-to-Audio Demo
Text-to-Audio Demo of Stable Audio Open ReImplementation. You may use this output for commercial purposes.
"""
)
with gr.Row():
with gr.Column():
prompt = gr.Textbox(
label="Prompt",
value="A woman is speaking.",
lines=2,
)
negative_prompt = gr.Textbox(
label="Negative Prompt",
value="low quality, quality, music",
lines=2,
)
duration = gr.Slider(
minimum=1.0,
maximum=10.0,
value=5.0,
step=0.5,
label="Duration seconds",
)
steps = gr.Slider(
minimum=10,
maximum=300,
value=200,
step=1,
label="Inference steps",
)
guidance_scale = gr.Slider(
minimum=1.0,
maximum=15.0,
value=7.0,
step=0.5,
label="Guidance scale",
)
seed = gr.Number(
label="Seed -1 means random",
value=-1,
precision=0,
)
button = gr.Button("Generate", variant="primary")
with gr.Column():
output_audio = gr.Audio(
label="Generated Audio",
type="numpy",
)
used_seed = gr.Number(
label="Used Seed",
precision=0,
)
button.click(
fn=generate_audio,
inputs=[
prompt,
negative_prompt,
duration,
steps,
guidance_scale,
seed,
],
outputs=[
output_audio,
used_seed,
],
)
gr.Examples(
examples=[
[
"Playing a guitar.",
"low quality, noisy",
5.0,
200,
7.0,
-1,
],
[
"Playing a piano.",
"low quality, noisy",
5.0,
200,
7.0,
-1,
],
[
"wind",
"",
5.0,
200,
7.0,
-1,
],
],
inputs=[
prompt,
negative_prompt,
duration,
steps,
guidance_scale,
seed,
],
)
if __name__ == "__main__":
demo.queue().launch() |