Meta’s AI MusicGen : Easy Local Install Guide

MusicGen AI Robot Playing Piano

Introduction

Hey, AI enthusiasts! Do you recall when Google unveiled its Music LM demo earlier this year? It was a fascinating showcase of AI capabilities, with captions describing various audio tracks and the AI delivering exactly what was described. However, it was just a demo with limited user control. I am still on the waitlist, which you can also request to join.

Now, the game has changed. Facebook Research has launched an open-source version of text-to-music AI called Meta’s AI MusicGen, and it’s fantastic. In this post, I’ll guide you through two methods of installing Meta’s AI MusicGen: the straightforward Google Colab method and the more hands-on local installation.

Installing Meta’s AI MusicGen on Your Local Machine

How to install Meta’s AI MusicGen on your local computer. The local method offers more control over the AI.

You’ll need a few things:

  • Windows 11
  • Nvidia graphics card
  • Anaconda (a Python version management system)
  • Cuda Library from Nvidia.
  • Git Bash

Creating Your Environment

Firstly we need to set up git bash and then configure it to use Anaconda. You can use the Anaconda terminal, but I prefer to use git bash for this project and other AI projects. So it will be easier to follow along with my other guides.

Setting up git bash to use Anaconda:

cd to the path that you installed Anaconda. In my case, it is “C:\Users\User\anaconda3”:

echo ". ${PWD}/conda.sh" >> ~/.bashrc

Next, create a new folder and name it MusicGen. Navigate to this folder from your Anaconda prompt (not the regular terminal) or, in our example, using git bash. Next, create a new Anaconda environment to ensure all your modules and installs go to the same place, preventing version issues. We’re also installing a specific version of Python that works with Cuda and all other AI libraries needed for this project.

mkdir ~/MusicGen

cd ~/MusicGen

Here are the commands to create and activate the environment:

conda create --name myenv python=3.10.11
conda activate myenv

Installing Necessary Libraries

Next, install all the machine learning and AI libraries needed for this project. Here’s the command:

conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c conda-forge

This will take some time to download. Feel free to bookmark this page and return once the download has finished.

Copy and paste the lines of code below to install some dependencies:

pip install xformers
pip install audiocraft
pip install Ipython

Running the Script

Here is a modified script python script from Google Colab to work locally, including converting the audio output to download locally. Copy and paste the below code into a new text file and save it as music-script.py (make sure to select “All files” when saving so it doesn’t save as .txt.py)

from IPython import display as ipd
from audiocraft.models import musicgen
from audiocraft.utils.notebook import display_audio
import torch
import torchaudio
import os

# Define the output directory
output_dir = r'C:\Users\User\MusicGen'

# If you have a weak GPU you can set medium to small
model = musicgen.MusicGen.get_pretrained('medium', device='cuda')

# Here we set the duration to 30 seconds
model.set_generation_params(duration=30)

# Here is where you can set music files you want to create
res = model.generate([
    'drum and bass', 
    'progressive trance',

], progress=True)

# Save the audio files
for i, audio in enumerate(res):
    audio_cpu = audio.cpu()
    file_path = os.path.join(output_dir, f'audio_{i}.wav')
    torchaudio.save(file_path, audio_cpu, sample_rate=32000)

# Display the saved audio files
for i in range(len(res)):
    file_path = os.path.join(output_dir, f'audio_{i}.wav')
    audio, sample_rate = torchaudio.load(file_path)
    display_audio(audio, sample_rate=sample_rate)

Now in the bash terminal, run the below code:

python music-script.py

You might see an error saying, “No module named Triton”, but you can ignore it. Triton is an open AI language that is unnecessary for this project and unavailable on Windows. If everything is working correctly, you should see some output soon.

image 10

It will take some time to complete, and you will now see two audio files in your folder.

image 11

Conclusion

And that’s it! Now we have complete control of the music we create, and it’s local to our computer. You can play around with it, have fun, and let me know what you create. If you have trouble setting it up, jump into the comment section below, and I’ll try to help you.

That concludes our guide on setting up Meta’s AI MusicGen. I hope you found this guide helpful and can now generate your own AI-created music.

Don’t forget to leave a comment below if you have any questions or thoughts to share. Also, feel free to check out my other blog posts for more AI tech-related content. Your feedback is always appreciated!

If you liked this guide, you would enjoy my guide to installing Super AGI – How to install Super AGI.

Leave a Reply

Up ↑

%d