sadafwalliyani commited on
Commit
18b406b
·
verified ·
1 Parent(s): b356ee6

Upload 7 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ audio_output/audio_0.wav filter=lfs diff=lfs merge=lfs -text
app.py CHANGED
@@ -1,4 +1,119 @@
1
  import streamlit as st
 
 
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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",
10
+ "Lofi", "Chillpop","Country","R&G", "Folk","Heavy Metal",
11
+ "EDM", "Soil", "Funk","Reggae", "Disco", "Punk Rock", "House",
12
+ "Techno","Indie Rock", "Grunge", "Ambient","Gospel", "Latin Music","Grime" ,"Trap", "Psychedelic Rock" ]
13
+
14
+ @st.cache_resource()
15
+ def load_model():
16
+ model = MusicGen.get_pretrained('facebook/musicgen-medium')
17
+ return model
18
+
19
+ def generate_music_tensors(descriptions, duration: int):
20
+ model = load_model()
21
+ # model = load_model().to('cpu')
22
+
23
+
24
+ model.set_generation_params(
25
+ use_sampling=True,
26
+ top_k=250,
27
+ duration=duration
28
+ )
29
+
30
+ with st.spinner("Generating Music with Medium-Model..."):
31
+ output = model.generate(
32
+ descriptions=descriptions,
33
+ progress=True,
34
+ return_tokens=True
35
+ )
36
+
37
+ st.success("Music Generation Complete!")
38
+ return output
39
+
40
+
41
+ def save_audio(samples: torch.Tensor):
42
+ sample_rate = 30000
43
+ save_path = "audio_output"
44
+ assert samples.dim() == 2 or samples.dim() == 3
45
+
46
+ samples = samples.detach().cpu()
47
+ if samples.dim() == 2:
48
+ samples = samples[None, ...]
49
+
50
+ for idx, audio in enumerate(samples):
51
+ audio_path = os.path.join(save_path, f"audio_{idx}.wav")
52
+ torchaudio.save(audio_path, audio, sample_rate)
53
+
54
+ def get_binary_file_downloader_html(bin_file, file_label='File'):
55
+ with open(bin_file, 'rb') as f:
56
+ data = f.read()
57
+ bin_str = base64.b64encode(data).decode()
58
+ href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{os.path.basename(bin_file)}">Download {file_label}</a>'
59
+ return href
60
+
61
+ st.set_page_config(
62
+ page_icon= "musical_note",
63
+ page_title= "Music Gen"
64
+ )
65
+
66
+ def main():
67
+ with st.sidebar:
68
+ st.header("""⚙️Generate Music ⚙️""",divider="rainbow")
69
+ st.text("")
70
+ st.subheader("1. Enter your music description.......")
71
+ bpm = st.number_input("Enter Speed in BPM", min_value=60)
72
+
73
+ text_area = st.text_area('Ex : 80s rock song with guitar and drums')
74
+ st.text('')
75
+ # Dropdown for genres
76
+ selected_genre = st.selectbox("Select Genre", genres)
77
+
78
+ st.subheader("2. Select time duration (In Seconds)")
79
+ time_slider = st.slider("Select time duration (In Seconds)", 0, 60, 10)
80
+ # time_slider = st.slider("Select time duration (In Minutes)", 0,300,10, step=1)
81
+
82
+
83
+ st.title("""🎵 Song Lab AI 🎵""")
84
+ st.text('')
85
+ left_co,right_co = st.columns(2)
86
+ left_co.write("""Music Generation through a prompt""")
87
+ left_co.write(("""PS : First generation may take some time ......."""))
88
+
89
+ if st.sidebar.button('Generate !'):
90
+ with left_co:
91
+ st.text('')
92
+ st.text('')
93
+ st.text('')
94
+ st.text('')
95
+ st.text('')
96
+ st.text('')
97
+ st.text('\n\n')
98
+ st.subheader("Generated Music")
99
+
100
+ # Generate audio
101
+ # descriptions = [f"{text_area} {selected_genre} {bpm} BPM" for _ in range(5)]
102
+ descriptions = [f"{text_area} {selected_genre} {bpm} BPM" for _ in range(1)] # Change the batch size to 1
103
+ music_tensors = generate_music_tensors(descriptions, time_slider)
104
+
105
+ # Only play the full audio for index 0
106
+ idx = 0
107
+ music_tensor = music_tensors[idx]
108
+ save_music_file = save_audio(music_tensor)
109
+ audio_filepath = f'audio_output/audio_{idx}.wav'
110
+ audio_file = open(audio_filepath, 'rb')
111
+ audio_bytes = audio_file.read()
112
+
113
+ # Play the full audio
114
+ st.audio(audio_bytes, format='audio/wav')
115
+ st.markdown(get_binary_file_downloader_html(audio_filepath, f'Audio_{idx}'), unsafe_allow_html=True)
116
+
117
+
118
+ if __name__ == "__main__":
119
+ main()
audio_output/audio_0.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0171292ef543a0edbb5caf26df6a04c5e8969cca2fab385874990a373388d458
3
+ size 3200078
audio_output/audio_output_.DS_Store ADDED
Binary file (6.15 kB). View file
 
audio_output/audio_output_audio_0.wav ADDED
Binary file (448 kB). View file
 
audio_output/dataset_example_electro_2.mp3 ADDED
Binary file (321 kB). View file
 
musicGen_Medium.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ audiocraft
2
+ numpy==1.23.5
3
+ torch==2.0.1
4
+ torchaudio==2.0.2
5
+ huggingface_hub
6
+ transformers==4.33.3
7
+ torchmetrics
8
+ encodec==0.1.1
9
+ xformers==0.0.22
10
+ streamlit
11
+ librosa
12
+ protobuf==3.20.0