kepsmiling121 commited on
Commit
c321ffd
·
verified ·
1 Parent(s): 027ce5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -4
app.py CHANGED
@@ -1,7 +1,58 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import MusicgenForConditionalGeneration, AutoProcessor
4
+ import numpy as np
5
 
6
+ # Load the model - 'small' is fastest and most reliable for free tiers
7
+ model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
8
+ processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
9
 
10
+ def generate_music(prompt, duration):
11
+ if not prompt:
12
+ return None
13
+
14
+ # Prepare the input
15
+ inputs = processor(text=[prompt], padding=True, return_tensors="pt")
16
+
17
+ # Generate - 1 second is roughly 50 tokens
18
+ max_tokens = int(duration * 50)
19
+ audio_values = model.generate(**inputs, max_new_tokens=max_tokens)
20
+
21
+ # Get sampling rate and clean up audio for Gradio
22
+ sampling_rate = model.config.audio_encoder.sampling_rate
23
+ audio_data = audio_values[0, 0].cpu().numpy()
24
+
25
+ return (sampling_rate, audio_data)
26
+
27
+ # Create a professional interface with Blocks
28
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
29
+ gr.Markdown("""# 🎵 AI Music Studio
30
+ *Describe a vibe, and our neural engine will compose a unique track for your blog.*""")
31
+
32
+ with gr.Row():
33
+ with gr.Column():
34
+ text_input = gr.Textbox(
35
+ label="Music Description",
36
+ placeholder="e.g., 'Lofi hip hop with a relaxing rain background' or '80s synthwave'"
37
+ )
38
+ duration_slider = gr.Slider(
39
+ minimum=2, maximum=15, value=8, step=1,
40
+ label="Duration (Seconds)"
41
+ )
42
+ generate_btn = gr.Button("Compose Music", variant="primary")
43
+
44
+ with gr.Column():
45
+ audio_output = gr.Audio(label="Generated Track", type="numpy")
46
+
47
+ generate_btn.click(
48
+ fn=generate_music,
49
+ inputs=[text_input, duration_slider],
50
+ outputs=audio_output
51
+ )
52
+
53
+ gr.Examples(
54
+ examples=[["Upbeat jazz with a fast trumpet solo", 10], ["Ambient space music with deep bass", 12]],
55
+ inputs=[text_input, duration_slider]
56
+ )
57
+
58
+ demo.launch()