Spaces:
Paused
Paused
File size: 4,403 Bytes
c5c143f | 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 | from __future__ import annotations
import gradio as gr
import soundfile as sf
try:
import spaces
except ImportError:
class spaces:
class GPU:
def __init__(self, func=None, duration=180):
self.func = func
def __call__(self, *args, **kwargs):
if self.func is not None:
return self.func(*args, **kwargs)
return args[0]
from pyharp import ModelCard, build_endpoint
from stable_audio_runtime import edit_audio
MIN_AUDIO_SECONDS = 5
MAX_AUDIO_SECONDS = 30
model_card = ModelCard(
name="Stable Audio 3",
description=(
"Restyle, inpaint, or continue music audio using a text prompt."
),
author="Stability AI",
tags=[
"audio-generation",
"music-editing",
"audio-inpainting",
"audio-continuation",
],
)
def _audio_duration(path: str | None) -> float:
if not path:
raise gr.Error("Please upload a music clip.")
try:
duration = float(sf.info(path).duration)
except Exception as exc:
raise gr.Error(f"Could not read the audio file: {exc}") from exc
if duration < MIN_AUDIO_SECONDS:
raise gr.Error(f"Audio must be at least {MIN_AUDIO_SECONDS} seconds.")
if duration > MAX_AUDIO_SECONDS:
raise gr.Error(f"Audio must be no longer than {MAX_AUDIO_SECONDS} seconds.")
return duration
@spaces.GPU(duration=180)
def process_fn(
audio_path: str | None,
prompt: str,
mode: str,
edit_start: float,
edit_end: float,
continuation_length: float,
strength: float,
seed: int,
) -> str:
duration = _audio_duration(audio_path)
prompt = (prompt or "").strip()
if not prompt:
raise gr.Error("Please enter an editing prompt.")
if mode == "Inpaint" and (
edit_start < 0 or edit_end > duration or edit_end <= edit_start
):
raise gr.Error(
"The inpaint region must be inside the uploaded clip, "
"with the end after the start."
)
try:
return edit_audio(
audio_path=audio_path,
prompt=prompt,
mode=mode,
edit_start=float(edit_start),
edit_end=float(edit_end),
continuation_length=float(continuation_length),
strength=float(strength),
seed=int(seed),
)
except gr.Error:
raise
except Exception as exc:
raise gr.Error(f"Stable Audio 3 inference failed: {exc}") from exc
with gr.Blocks(title="Stable Audio 3") as demo:
input_components = [
gr.Audio(type="filepath", label="Music Audio")
.harp_required(True)
.set_info("Music clip between 5 and 30 seconds."),
gr.Textbox(
label="Prompt",
placeholder="A warm synthwave groove with punchy drums",
).harp_required(True),
gr.Dropdown(
choices=["Restyle", "Inpaint", "Continue"],
value="Restyle",
label="Edit Mode",
),
gr.Slider(
minimum=0,
maximum=30,
value=4,
step=0.1,
label="Edit Start (seconds)",
).set_info("Used by Inpaint mode."),
gr.Slider(
minimum=0,
maximum=30,
value=8,
step=0.1,
label="Edit End (seconds)",
).set_info("Used by Inpaint mode."),
gr.Slider(
minimum=1,
maximum=15,
value=8,
step=1,
label="Continuation Length (seconds)",
).set_info("Used by Continue mode."),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.75,
step=0.05,
label="Transformation Strength",
).set_info("Used by Restyle mode."),
gr.Slider(
minimum=0,
maximum=99999,
value=0,
step=1,
label="Seed",
).set_info("Use 0 for a random seed."),
]
output_components = [
gr.Audio(type="filepath", label="Edited Audio"),
]
build_endpoint(
model_card=model_card,
input_components=input_components,
output_components=output_components,
process_fn=process_fn,
)
if __name__ == "__main__":
demo.queue(default_concurrency_limit=1).launch(show_error=True, pwa=True)
|