Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,32 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
iface = gr.Interface(
|
| 13 |
-
fn=
|
| 14 |
-
inputs=gr.Textbox(label="Enter a music
|
| 15 |
outputs=gr.Audio(label="Generated Music"),
|
| 16 |
title="AI Music Generator",
|
| 17 |
-
description="
|
| 18 |
)
|
| 19 |
|
| 20 |
if __name__ == "__main__":
|
| 21 |
iface.launch()
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
| 4 |
+
import scipy
|
| 5 |
|
| 6 |
+
device = "cpu"
|
| 7 |
+
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-stereo-32khz").to(device)
|
| 8 |
+
processor = AutoProcessor.from_pretrained("facebook/musicgen-stereo-32khz")
|
| 9 |
|
| 10 |
+
def generate_music(prompt):
|
| 11 |
+
inputs = processor(text=[prompt], return_tensors="pt").to(device)
|
| 12 |
+
audio_values = model.generate(**inputs, max_new_tokens=256) # Adjust length
|
| 13 |
+
audio_array = audio_values[0].cpu().numpy()
|
| 14 |
+
sample_rate = model.config.audio_encoder.sampling_rate
|
| 15 |
|
| 16 |
+
# Save as WAV file
|
| 17 |
+
output_path = "generated_music.wav"
|
| 18 |
+
scipy.io.wavfile.write(output_path, sample_rate, audio_array.T)
|
| 19 |
+
return output_path
|
| 20 |
+
|
| 21 |
+
# Gradio UI
|
| 22 |
iface = gr.Interface(
|
| 23 |
+
fn=generate_music,
|
| 24 |
+
inputs=gr.Textbox(label="Enter a music prompt (e.g., 'calm piano melody')"),
|
| 25 |
outputs=gr.Audio(label="Generated Music"),
|
| 26 |
title="AI Music Generator",
|
| 27 |
+
description="Describe the music style and let AI generate it."
|
| 28 |
)
|
| 29 |
|
| 30 |
if __name__ == "__main__":
|
| 31 |
iface.launch()
|
| 32 |
+
|