rafhiromadoni commited on
Commit
7b59b19
·
verified ·
1 Parent(s): 211d05e

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +67 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import scipy.io.wavfile
4
+ import numpy as np
5
+
6
+ # 1. LOAD MODEL MUSICGEN
7
+ # Kita menggunakan versi 'small' agar cepat & ringan di Free Tier Hugging Face
8
+ print("Sedang memuat model MusicGen...")
9
+ synthesiser = pipeline("text-to-audio", model="facebook/musicgen-small")
10
+
11
+ def generate_music(text, duration):
12
+ if not text:
13
+ return None
14
+
15
+ print(f"🎵 Membuat musik: '{text}' selama {duration} detik...")
16
+
17
+ # Generate audio
18
+ # forward_params max_new_tokens mengatur durasi (kurang lebih 1 token ≈ 0.02 detik)
19
+ # 256 token ≈ 5 detik, 512 ≈ 10 detik
20
+ # Kita buat dinamis berdasarkan input user
21
+ max_tokens = int(duration * 50)
22
+
23
+ music = synthesiser(text, forward_params={"max_new_tokens": max_tokens})
24
+
25
+ # Konversi output ke format yang bisa diputar Gradio
26
+ audio_data = music["audio"]
27
+ sampling_rate = music["sampling_rate"]
28
+
29
+ # Normalisasi audio agar suaranya jelas
30
+ audio_data = audio_data / np.max(np.abs(audio_data))
31
+
32
+ return (sampling_rate, audio_data.T)
33
+
34
+ # 2. ANTARMUKA GRADIO
35
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
36
+ gr.Markdown("""
37
+ <h1 style='text-align: center;'>🎵 SonicGen: AI Music Creator</h1>
38
+ <p style='text-align: center;'>Jadilah komposer musik instan! Ketik deskripsi musik yang Anda inginkan, dan AI akan menciptakannya untuk Anda.</p>
39
+ """)
40
+
41
+ with gr.Row():
42
+ with gr.Column():
43
+ inp_text = gr.Textbox(
44
+ label="🎹 Deskripsi Musik (Prompt)",
45
+ placeholder="Contoh: 80s pop track with synth drums and nostalgic melody",
46
+ lines=2
47
+ )
48
+ inp_duration = gr.Slider(minimum=5, maximum=15, value=10, step=1, label="⏱️ Durasi (Detik)")
49
+ btn = gr.Button("✨ Ciptakan Musik", variant="primary")
50
+
51
+ with gr.Column():
52
+ out_audio = gr.Audio(label="🎧 Hasil Generasi AI", type="numpy")
53
+
54
+ # Contoh prompt agar user bisa langsung mencoba
55
+ gr.Examples(
56
+ examples=[
57
+ ["A dynamic soundtrack for an epic space battle", 10],
58
+ ["Lo-fi hip hop beat perfect for studying and relaxing", 10],
59
+ ["Acoustic guitar melody with birds chirping in background", 10]
60
+ ],
61
+ inputs=[inp_text, inp_duration]
62
+ )
63
+
64
+ btn.click(fn=generate_music, inputs=[inp_text, inp_duration], outputs=out_audio)
65
+
66
+ if __name__ == "__main__":
67
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ scipy
4
+ numpy