Gpagejr12 commited on
Commit
e0c1514
·
verified ·
1 Parent(s): 92fa5f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py CHANGED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ import torchaudio
4
+ from audiocraft.models import MusicGen
5
+ import os
6
+ import numpy as np
7
+ import base64
8
+
9
+ genres = ["Pop", "Rock", "Jazz", "Electronic", "Hip-Hop", "Classical", "Lofi", "Chillpop"]
10
+
11
+ @st.cache_resource()
12
+ def load_model():
13
+ model = MusicGen.get_pretrained('facebook/musicgen-small')
14
+ return model
15
+
16
+ def generate_music_tensors(descriptions, duration: int):
17
+ model = load_model()
18
+
19
+ model.set_generation_params(
20
+ use_sampling=True,
21
+ top_k=250,
22
+ duration=duration
23
+ )
24
+
25
+ with st.spinner("Generating Music..."):
26
+ output = model.generate(
27
+ descriptions=descriptions,
28
+ progress=True,
29
+ return_tokens=True
30
+ )
31
+
32
+ st.success("Music Generation Complete!")
33
+ return output
34
+
35
+
36
+ def save_audio(samples: torch.Tensor):
37
+ sample_rate = 30000
38
+ save_path = "/content/drive/MyDrive/Colab Notebooks/audio_output"
39
+ assert samples.dim() == 2 or samples.dim() == 3
40
+
41
+ samples = samples.detach().cpu()
42
+ if samples.dim() == 2:
43
+ samples = samples[None, ...]
44
+
45
+ for idx, audio in enumerate(samples):
46
+ audio_path = os.path.join(save_path, f"audio_{idx}.wav")
47
+ torchaudio.save(audio_path, audio, sample_rate)
48
+
49
+ def get_binary_file_downloader_html(bin_file, file_label='File'):
50
+ with open(bin_file, 'rb') as f:
51
+ data = f.read()
52
+ bin_str = base64.b64encode(data).decode()
53
+ href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{os.path.basename(bin_file)}">Download {file_label}</a>'
54
+ return href
55
+
56
+ st.set_page_config(
57
+ page_icon= "musical_note",
58
+ page_title= "Music Gen"
59
+ )
60
+
61
+ def main():
62
+ with st.sidebar:
63
+ st.header("""⚙️Generate Music ⚙️""",divider="rainbow")
64
+ st.text("")
65
+ st.subheader("1. Enter your music description.......")
66
+ bpm = st.number_input("Enter Speed in BPM", min_value=60)
67
+
68
+ text_area = st.text_area('Ex : 80s rock song with guitar and drums')
69
+ st.text('')
70
+ # Dropdown for genres
71
+ selected_genre = st.selectbox("Select Genre", genres)
72
+
73
+ st.subheader("2. Select time duration (In Seconds)")
74
+ time_slider = st.slider("Select time duration (In Seconds)", 0, 60, 10)
75
+
76
+ st.title("""🎵 Song Lab AI 🎵""")
77
+ st.text('')
78
+ left_co,right_co = st.columns(2)
79
+ left_co.write("""Music Generation through a prompt""")
80
+ left_co.write(("""PS : First generation may take some time ......."""))
81
+
82
+ if st.sidebar.button('Generate !'):
83
+ with left_co:
84
+ st.text('')
85
+ st.text('')
86
+ st.text('')
87
+ st.text('')
88
+ st.text('')
89
+ st.text('')
90
+ st.subheader("Generated Music")
91
+
92
+ # Generate audio
93
+ descriptions = [f"{text_area} {selected_genre} {bpm} BPM" for _ in range(5)] # Adjust the batch size (5 in this case)
94
+ music_tensors = generate_music_tensors(descriptions, time_slider)
95
+
96
+ # Only play the full audio for index 0
97
+ idx = 0
98
+ music_tensor = music_tensors[idx]
99
+ save_music_file = save_audio(music_tensor)
100
+ audio_filepath = f'/content/drive/MyDrive/Colab Notebooks/audio_output/audio_{idx}.wav'
101
+ audio_file = open(audio_filepath, 'rb')
102
+ audio_bytes = audio_file.read()
103
+
104
+ # Play the full audio
105
+ st.audio(audio_bytes, format='audio/wav')
106
+ st.markdown(get_binary_file_downloader_html(audio_filepath, f'Audio_{idx}'), unsafe_allow_html=True)
107
+
108
+
109
+ if __name__ == "__main__":
110
+ main()
111
+