Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,44 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
-
import torch
|
| 4 |
-
import numpy as np
|
| 5 |
-
import scipy.io.wavfile
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
# --- AI Model Setup ---
|
| 9 |
-
# This
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
outputs=gr.Audio(type="filepath", label="Generated Music"),
|
| 45 |
-
title="Neon MusicGen AI",
|
| 46 |
-
description="Transform your ideas into music with AI. Describe a sound and set the duration to get started."
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
if __name__ == "__main__":
|
| 50 |
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
import scipy.io.wavfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# --- AI Model Setup with a Public Model---
|
| 9 |
+
# This version uses a public model and requires NO authentication token.
|
| 10 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
pipe = pipeline("text-to-audio", "facebook/musicgen-small", device=device)
|
| 12 |
+
|
| 13 |
+
# --- Music Generation Function ---
|
| 14 |
+
def generate_music(prompt, duration):
|
| 15 |
+
try:
|
| 16 |
+
max_new_tokens = int(duration * 50)
|
| 17 |
+
music = pipe(prompt, forward_params={"max_new_tokens": max_new_tokens})
|
| 18 |
+
|
| 19 |
+
sampling_rate = music["sampling_rate"]
|
| 20 |
+
audio_data = music["audio"][0].T
|
| 21 |
+
|
| 22 |
+
output_filename = "music_output.wav"
|
| 23 |
+
audio_int16 = np.int16(audio_data * 32767)
|
| 24 |
+
scipy.io.wavfile.write(output_filename, rate=sampling_rate, data=audio_int16)
|
| 25 |
+
|
| 26 |
+
return output_filename
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"An error occurred: {e}")
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
# --- Gradio Interface ---
|
| 32 |
+
iface = gr.Interface(
|
| 33 |
+
fn=generate_music,
|
| 34 |
+
inputs=[
|
| 35 |
+
gr.Textbox(lines=2, label="Music Description", placeholder="e.g., A futuristic synthwave track..."),
|
| 36 |
+
gr.Slider(minimum=2, maximum=60, value=15, label="Duration (seconds)")
|
| 37 |
+
],
|
| 38 |
+
outputs=gr.Audio(type="filepath", label="Generated Music"),
|
| 39 |
+
title="Neon MusicGen AI",
|
| 40 |
+
description="Transform your ideas into music with AI. Describe a sound and set the duration to get started."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
iface.launch()
|