MusicGen Large
A text-to-audio model for generating short music samples in specified styles or moods.
Deploy MusicGen Large 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 two 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 file
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
8data = {
9 "prompts": [
10 "a chill synthwave mix"
11 ],
12 "duration": 10
13}
14
15# Call model endpoint
16res = requests.post(
17 f"https://model-{model_id}.api.baseten.co/production/predict",
18 headers={"Authorization": f"Api-Key {baseten_api_key}"},
19 json=data
20)
21
22# Convert the base64 output to an audio file
23res = res.json()
24output = res.get("data")
25for idx, clip in enumerate(output):
26 with open(f"musicgen_output_{idx}.wav", "wb") as f:
27 f.write(base64.b64decode(clip))
1{
2 "data": [
3 "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAA..."
4 ]
5}
Here is another example using the following prompt:
90s rock song with electric guitar and heavy drums
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
8data = {
9 "prompts": [
10 "90s rock song with electric guitar and heavy drums"
11 ],
12 "duration": 10
13}
14
15# Call model endpoint
16res = requests.post(
17 f"https://model-{model_id}.api.baseten.co/production/predict",
18 headers={"Authorization": f"Api-Key {baseten_api_key}"},
19 json=data
20)
21
22# Convert the base64 output to an audio file
23res = res.json()
24output = res.get("data")
25for idx, clip in enumerate(output):
26 with open(f"musicgen_output_{idx}.wav", "wb") as f:
27 f.write(base64.b64decode(clip))
1{
2 "data": [
3 "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAA..."
4 ]
5}
Final example using the prompt:
lofi slow bpm electro chill with organic samples
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
8data = {
9 "prompts": [
10 "lofi slow bpm electro chill with organic samples"
11 ],
12 "duration": 10
13}
14
15# Call model endpoint
16res = requests.post(
17 f"https://model-{model_id}.api.baseten.co/production/predict",
18 headers={"Authorization": f"Api-Key {baseten_api_key}"},
19 json=data
20)
21
22# Convert the base64 output to an audio file
23res = res.json()
24output = res.get("data")
25for idx, clip in enumerate(output):
26 with open(f"musicgen_output_{idx}.wav", "wb") as f:
27 f.write(base64.b64decode(clip))
1{
2 "data": [
3 "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAA..."
4 ]
5}