MusicGen Melody
A melody-guided music generation model that reimagines a provided melody in a specified style.
Deploy MusicGen Melody behind an API endpoint in seconds.
Deploy modelExample usage
This code example shows how to invoke the model using the requests library in Python. The model has three inputs:
prompts
: This is a list of texts which the model uses to determine the type of music to generate.duration
: The duration in seconds for each output audio filemelody
: The user must provide an audio file as a base64 string that represents a melody or beat. The model will use this melody to create the final audio clip.
The output of the model is a JSON object that contains a key called data
which has a list of all the generated audio files. Each audio file in the list is represented as a base64 string.
1import requests
2import os
3
4# Replace the empty string with your model id below
5model_id = ""
6baseten_api_key = os.environ["BASETEN_API_KEY"]
7
8def wav_to_base64(file_path):
9 with open(file_path, "rb") as wav_file:
10 binary_data = wav_file.read()
11 base64_data = base64.b64encode(binary_data)
12 base64_string = base64_data.decode("utf-8")
13 return base64_string
14
15data = {
16 "prompts": [
17 "An 80s driving pop song with heavy drums and synth pads in the background"
18 ],
19 "duration": 10,
20 "melody": wav_to_base64("musicgen-melody-input.wav")
21}
22
23# Call model endpoint
24res = requests.post(
25 f"https://model-{model_id}.api.baseten.co/production/predict",
26 headers={"Authorization": f"Api-Key {baseten_api_key}"},
27 json=data
28)
29
30# Convert the base64 output to an audio file
31res = res.json()
32output = res.get("data")
33for idx, clip in enumerate(output):
34 with open(f"musicgen_output_{idx}.wav", "wb") as f:
35 f.write(base64.b64decode(clip))
1{
2 "data": [
3 "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAA..."
4 ]
5}
Another example with a different prompt:
classic jazz music
1import requests
2import os
3
4# Replace the empty string with your model id below
5model_id = ""
6baseten_api_key = os.environ["BASETEN_API_KEY"]
7
8def wav_to_base64(file_path):
9 with open(file_path, "rb") as wav_file:
10 binary_data = wav_file.read()
11 base64_data = base64.b64encode(binary_data)
12 base64_string = base64_data.decode("utf-8")
13 return base64_string
14
15data = {
16 "prompts": [
17 "classic jazz music"
18 ],
19 "duration": 10,
20 "melody": wav_to_base64("musicgen-melody-input.wav")
21}
22
23# Call model endpoint
24res = requests.post(
25 f"https://model-{model_id}.api.baseten.co/production/predict",
26 headers={"Authorization": f"Api-Key {baseten_api_key}"},
27 json=data
28)
29
30# Convert the base64 output to an audio file
31res = res.json()
32output = res.get("data")
33for idx, clip in enumerate(output):
34 with open(f"musicgen_output_{idx}.wav", "wb") as f:
35 f.write(base64.b64decode(clip))
1{
2 "data": [
3 "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAA..."
4 ]
5}