|
import gradio as gr |
|
from transformers import AutoProcessor, AudioSpectrogramPipeline |
|
import soundfile as sf |
|
import tempfile |
|
|
|
|
|
processor = AutoProcessor.from_pretrained("stabilityai/stable-audio-open-small") |
|
pipeline = AudioSpectrogramPipeline.from_pretrained("stabilityai/stable-audio-open-small") |
|
|
|
def generate_audio(prompt): |
|
result = pipeline(prompt=prompt, num_inference_steps=50) |
|
audio = result["audio"] |
|
sr = result["sampling_rate"] |
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f: |
|
sf.write(f.name, audio, sr) |
|
return f.name |
|
|
|
|
|
demo = gr.Interface( |
|
fn=generate_audio, |
|
inputs=gr.Textbox(label="Prompt (in English)", placeholder="e.g. A lo-fi beat with rain sounds"), |
|
outputs=gr.Audio(label="Generated Audio"), |
|
title="🎵 Stable Audio - Text to Sound", |
|
description="Generate short sound clips from text using `stabilityai/stable-audio-open-small`" |
|
) |
|
|
|
demo.launch() |
|
|