EconLab AI commited on
Commit
adabe8f
·
1 Parent(s): 06e108d

Update to Gradio-based app

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py CHANGED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import scipy.io.wavfile
4
+ import os
5
+
6
+ # Stelle sicher, dass der static-Ordner existiert
7
+ os.makedirs('static', exist_ok=True)
8
+
9
+ print("Lade MusicGen Modell... (Dies kann einige Minuten dauern)")
10
+ synthesiser = pipeline(
11
+ "text-to-audio",
12
+ "facebook/musicgen-small",
13
+ model_kwargs={"attn_implementation": "eager"}
14
+ )
15
+ print("Modell erfolgreich geladen!")
16
+
17
+ # Funktion zur Musikgenerierung
18
+ def generate_music(description):
19
+ try:
20
+ print(f"Generiere Musik für: '{description}'")
21
+ music = synthesiser(description, forward_params={"do_sample": True})
22
+
23
+ # Datei speichern
24
+ output_path = "static/generated_music.wav"
25
+ scipy.io.wavfile.write(
26
+ output_path,
27
+ rate=music["sampling_rate"],
28
+ data=music["audio"]
29
+ )
30
+ print("Musik erfolgreich generiert!")
31
+ return output_path # Rückgabe des Dateipfads
32
+ except Exception as e:
33
+ print(f"Fehler bei der Musikgenerierung: {str(e)}")
34
+ return f"Fehler: {str(e)}"
35
+
36
+ # Gradio-Interface
37
+ description = "Gib eine Beschreibung ein, um Musik zu generieren:"
38
+ title = "MusicGen: Text-to-Audio Generator"
39
+ examples = [["A relaxing piece of piano music for a calm evening."]]
40
+
41
+ interface = gr.Interface(
42
+ fn=generate_music,
43
+ inputs=gr.Textbox(label="Musikbeschreibung", placeholder="Z.B. Ein fröhliches Lied mit Gitarren."),
44
+ outputs=gr.Audio(label="Generierte Musik", type="filepath"),
45
+ title=title,
46
+ description=description,
47
+ examples=examples
48
+ )
49
+
50
+ # App starten
51
+ if __name__ == "__main__":
52
+ interface.launch()