Spaces:
Sleeping
Sleeping
File size: 3,706 Bytes
132fc1b adabe8f bd44176 adabe8f 75ab712 c9d18d9 adabe8f 132fc1b 75ab712 c9d18d9 adabe8f bd44176 adabe8f c9d18d9 adabe8f c9d18d9 adabe8f 9c7965f bc05d79 c9d18d9 9c7965f bc05d79 9c7965f c9d18d9 9c7965f c9d18d9 9c7965f c9d18d9 9c7965f c9d18d9 9c7965f c9d18d9 9c7965f bd44176 9c7965f c9d18d9 bd44176 9c7965f c9d18d9 9c7965f c9d18d9 9c7965f c9d18d9 bc05d79 9c7965f c9d18d9 9c7965f c9d18d9 bc05d79 c9d18d9 bc05d79 bd44176 c9d18d9 9c7965f c9d18d9 9c7965f adabe8f c9d18d9 bd44176 adabe8f bd44176 |
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 |
import spaces # Für ZeroGPU
import gradio as gr
from transformers import pipeline
import scipy.io.wavfile
import os
# Sicherstellen, dass der 'static'-Ordner existiert
os.makedirs('static', exist_ok=True)
# GPU-abhängige Funktion zur Musikgenerierung
@spaces.GPU(duration=120) # GPU wird für diese Funktion angefordert
def generate_music(prompt, duration, diffusion_steps, cfg_scale):
try:
# MusicGen-Pipeline initialisieren (nur innerhalb der GPU-Session)
synthesizer = pipeline(
"text-to-audio",
"facebook/musicgen-small",
model_kwargs={"attn_implementation": "eager"}
)
# Musik generieren
music = synthesizer(prompt, forward_params={"do_sample": True})
# Datei speichern
output_path = "static/generated_music.wav"
scipy.io.wavfile.write(
output_path,
rate=music["sampling_rate"],
data=music["audio"]
)
return output_path
except Exception as e:
return f"Error: {str(e)}"
# Benutzerdefiniertes CSS
custom_css = """
body {
background-color: #121212;
color: #ffffff;
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
}
#title {
text-align: center;
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
#description {
text-align: center;
font-size: 16px;
margin-bottom: 30px;
}
button {
background-color: #ff5722;
color: white;
font-weight: bold;
border-radius: 8px;
padding: 10px 20px;
border: none;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #ff784e;
}
.slider {
accent-color: #ff5722;
}
textarea, input[type="text"] {
background-color: #1e1e2f;
color: white;
border: 1px solid #444;
padding: 10px;
border-radius: 5px;
font-size: 14px;
}
audio {
border: 2px solid #444;
border-radius: 5px;
margin-top: 10px;
}
"""
# Gradio-Interface erstellen
with gr.Blocks(css=custom_css) as demo:
gr.Markdown("<h1 id='title'>Stable Audio Generator</h1>")
gr.Markdown("<p id='description'>Generate variable-length stereo audio at 44.1kHz from text prompts using Stable Audio Open 1.0.</p>")
with gr.Row():
with gr.Column(scale=2):
prompt = gr.Textbox(
label="Prompt",
placeholder="Describe your music (e.g., 'Relaxing piano music')."
)
duration = gr.Slider(
label="Duration in Seconds",
minimum=10,
maximum=60,
step=1,
value=30
)
diffusion_steps = gr.Slider(
label="Number of Diffusion Steps",
minimum=50,
maximum=200,
step=10,
value=150
)
cfg_scale = gr.Slider(
label="CFG Scale",
minimum=1,
maximum=20,
step=1,
value=10
)
generate_button = gr.Button("Submit")
with gr.Column(scale=1):
output = gr.Audio(
label="Generated Music",
type="filepath",
autoplay=True
)
with gr.Row():
clear_button = gr.Button("Clear")
generate_button.click(
fn=generate_music,
inputs=[prompt, duration, diffusion_steps, cfg_scale],
outputs=output
)
clear_button.click(
fn=lambda: None,
inputs=None,
outputs=[prompt, output]
)
# Anwendung starten
if __name__ == "__main__":
demo.launch()
|