Shamkumar commited on
Commit
4cef07a
·
verified ·
1 Parent(s): c7c5c7d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import scipy.io.wavfile as wavfile
4
+ import numpy as np
5
+
6
+ # Load the small version of MusicGen (Fits in Free Tier memory)
7
+ model_id = "facebook/musicgen-small"
8
+ synthesizer = pipeline("text-to-audio", model=model_id)
9
+
10
+ def generate_music(prompt):
11
+ # The model generates the audio based on your prompt
12
+ output = synthesizer(prompt, forward_params={"do_sample": True})
13
+
14
+ # Extract the audio data and sampling rate
15
+ sampling_rate = output["sampling_rate"]
16
+ audio_data = output["audio"]
17
+
18
+ # Hugging Face/Gradio expects a tuple of (rate, data)
19
+ # We ensure the data is in the correct format (float32 or int16)
20
+ return (sampling_rate, audio_data.T)
21
+
22
+ # Create the beautiful web interface
23
+ with gr.Blocks() as demo:
24
+ gr.Markdown("# 🎵 Open Hearts Music Generator")
25
+ gr.Markdown("Enter your cinematic prompt below to generate a unique track.")
26
+
27
+ with gr.Row():
28
+ with gr.Column():
29
+ input_text = gr.Textbox(
30
+ label="Your Music Prompt",
31
+ placeholder="A cinematic pop-folk crossover with piano and strings...",
32
+ lines=3
33
+ )
34
+ generate_btn = gr.Button("Generate Music", variant="primary")
35
+
36
+ with gr.Column():
37
+ audio_output = gr.Audio(label="Resulting Audio")
38
+
39
+ # Connect the button to the function
40
+ generate_btn.click(fn=generate_music, inputs=input_text, outputs=audio_output)
41
+
42
+ # Launch the app
43
+ if __name__ == "__main__":
44
+ demo.launch()