├── README.md └── text-music generation.py /README.md: -------------------------------------------------------------------------------- 1 | # music_generation -------------------------------------------------------------------------------- /text-music generation.py: -------------------------------------------------------------------------------- 1 | from audiocraft.models import MusicGen 2 | import streamlit as st 3 | import torch 4 | import torchaudio 5 | import os 6 | import numpy as np 7 | import base64 8 | 9 | @st.cache_resource 10 | def load_model(): 11 | model = MusicGen.get_pretrained('facebook/musicgen-small') 12 | return model 13 | 14 | def generate_music_tensors(description, duration: int): 15 | model = load_model() 16 | 17 | model.set_generation_params( 18 | use_sampling=True, 19 | top_k=250, 20 | duration=duration 21 | ) 22 | 23 | output = model.generate( 24 | descriptions=[description], 25 | progress=True, 26 | return_tokens=True 27 | ) 28 | 29 | return output 30 | 31 | def save_audio(samples, counter): 32 | sample_rate = 32000 33 | save_path = "audio_output/" 34 | 35 | audio_paths = [] 36 | for idx, audio in enumerate(samples): 37 | audio_path = os.path.join(save_path, f"music{counter}_{idx}.wav") 38 | torchaudio.save(audio_path, audio, sample_rate) 39 | audio_paths.append(audio_path) 40 | return audio_paths 41 | 42 | def get_binary_file_downloader_html(bin_files, file_labels): 43 | hrefs = [] 44 | for bin_file, file_label in zip(bin_files, file_labels): 45 | with open(bin_file, 'rb') as f: 46 | data = f.read() 47 | bin_str = base64.b64encode(data).decode() 48 | href = f'Download {file_label}' 49 | hrefs.append(href) 50 | return hrefs 51 | 52 | st.set_page_config( 53 | page_icon= "musical_note", 54 | page_title= "Music Generator" 55 | ) 56 | 57 | counter = 1 58 | 59 | def main(): 60 | global counter 61 | st.title("Text to Music Generator using Python") 62 | 63 | with st.sidebar: 64 | st.subheader("Available Song Genres:") 65 | st.write("- Pop") 66 | st.write("- Rock") 67 | st.write("- Jazz") 68 | st.write("- Classical") 69 | st.write("- Hip-hop") 70 | st.write("- Electronic") 71 | st.write("- Country") 72 | 73 | st.markdown("---") # Separator between sidebar and main content 74 | 75 | st.write("Please enter a sentence or lyrics describing the mood or style of music you want to generate.") 76 | text_area = st.text_area("Enter your sentence or lyrics") 77 | 78 | time_slider = st.slider("Select time duration (In Seconds)", 0, 20, 10) 79 | 80 | if st.button("Generate Music"): 81 | if text_area and time_slider: 82 | st.json({ 83 | 'Your Description': text_area, 84 | 'Selected Time Duration (in Seconds)': time_slider 85 | }) 86 | 87 | st.subheader("Generated Music") 88 | music_tensors = generate_music_tensors(text_area, time_slider) 89 | audio_filepaths = save_audio(music_tensors, counter) 90 | audio_file_labels = [f"Audio {i+1}" for i in range(len(music_tensors))] 91 | for i in range(len(music_tensors)): 92 | st.audio(open(audio_filepaths[i], 'rb').read(), format='audio/wav') 93 | st.markdown(get_binary_file_downloader_html([audio_filepaths[i]], [audio_file_labels[i]])[0], unsafe_allow_html=True) 94 | 95 | counter += 1 96 | 97 | if _name_ == "_main_": 98 | main() --------------------------------------------------------------------------------