Spaces:
Sleeping
Sleeping
| 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) | |
| # MusicGen-Pipeline initialisieren | |
| synthesizer = pipeline( | |
| "text-to-audio", | |
| "facebook/musicgen-small", | |
| model_kwargs={"attn_implementation": "eager"} | |
| ) | |
| # Funktion zur Musikgenerierung | |
| def generate_music(prompt, duration, sampling_rate): | |
| try: | |
| # Musik generieren | |
| music = synthesizer( | |
| prompt, | |
| forward_params={"do_sample": True, "max_new_tokens": duration * sampling_rate} | |
| ) | |
| # Ausgabe-Pfad definieren | |
| output_path = "static/generated_music.wav" | |
| # Musikdatei speichern | |
| scipy.io.wavfile.write( | |
| output_path, | |
| rate=music["sampling_rate"], | |
| data=music["audio"] | |
| ) | |
| return output_path # Pfad zur generierten Musikdatei zurückgeben | |
| except Exception as e: | |
| return str(e) # Fehlernachricht zurückgeben | |
| # Benutzerdefiniertes CSS | |
| custom_css = """ | |
| body { | |
| background-color: #1e1e2f; | |
| color: #ffffff; | |
| font-family: 'Arial', sans-serif; | |
| } | |
| button { | |
| background-color: #ff5722; | |
| color: white; | |
| font-weight: bold; | |
| border-radius: 5px; | |
| padding: 10px 20px; | |
| border: none; | |
| cursor: pointer; | |
| font-size: 16px; | |
| } | |
| button:hover { | |
| background-color: #ff784e; | |
| } | |
| input[type="text"], textarea { | |
| background-color: #2a2a3d; | |
| color: white; | |
| border: 1px solid #444; | |
| padding: 10px; | |
| border-radius: 5px; | |
| font-size: 14px; | |
| width: 100%; | |
| } | |
| .slider { | |
| accent-color: #ff5722; | |
| } | |
| 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 style='text-align: center;'>🎶 Stable Audio Generator</h1>", | |
| elem_id="title" | |
| ) | |
| gr.Markdown( | |
| "<p style='text-align: center;'>Generate high-quality music from your text prompts.</p>" | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| prompt = gr.Textbox( | |
| label="Prompt", | |
| placeholder="Describe the music you want to generate", | |
| elem_id="prompt-box" | |
| ) | |
| duration = gr.Slider( | |
| label="Duration in Seconds", | |
| minimum=1, | |
| maximum=60, | |
| value=30, | |
| elem_id="duration-slider" | |
| ) | |
| sampling_rate = gr.Slider( | |
| label="Sampling Rate (kHz)", | |
| minimum=16, | |
| maximum=48, | |
| value=24, | |
| elem_id="sampling-slider" | |
| ) | |
| generate_button = gr.Button("Generate", elem_id="generate-button") | |
| with gr.Column(scale=3): | |
| output = gr.Audio(label="Generated Music", type="filepath", autoplay=True) | |
| # Ereignisbindung | |
| generate_button.click( | |
| fn=generate_music, | |
| inputs=[prompt, duration, sampling_rate], | |
| outputs=output | |
| ) | |
| # Anwendung starten | |
| if __name__ == "__main__": | |
| demo.launch() | |