Fathima-Firose commited on
Commit
48a8575
·
verified ·
1 Parent(s): eb6c58c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -49
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 code securely reads the token from your Space's secrets.
10
- auth_token = os.getenv("HUGGING_FACE_HUB_TOKEN")
11
- device = "cuda:0" if torch.cuda.is_available() else "cpu"
12
- pipe = pipeline(
13
- "text-to-audio",
14
- "meta-llama/musicgen-small",
15
- device=device,
16
- use_auth_token=auth_token
17
- )
18
-
19
- # --- Music Generation Function ---
20
- def generate_music(prompt, duration):
21
- try:
22
- max_new_tokens = int(duration * 50)
23
- music = pipe(prompt, forward_params={"max_new_tokens": max_new_tokens})
24
-
25
- sampling_rate = music["sampling_rate"]
26
- audio_data = music["audio"][0].T
27
-
28
- output_filename = "music_output.wav"
29
- audio_int16 = np.int16(audio_data * 32767)
30
- scipy.io.wavfile.write(output_filename, rate=sampling_rate, data=audio_int16)
31
-
32
- return output_filename
33
- except Exception as e:
34
- print(f"An error occurred: {e}")
35
- return None
36
-
37
- # --- Gradio Interface ---
38
- iface = gr.Interface(
39
- fn=generate_music,
40
- inputs=[
41
- gr.Textbox(lines=2, label="Music Description", placeholder="e.g., A futuristic synthwave track..."),
42
- gr.Slider(minimum=2, maximum=60, value=15, label="Duration (seconds)")
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()