Meta MusicGen Controls - how generate longer music?

Hi, does anyone know if there is a way to control the output of Meta MusicGen? And what can be changed?

Right now, the api works great, but it’s only generating 8 seconds of music. Is there a way to make the length of the generated music longer?

It looks like the duration can be changed. In replicate, it provides this code:

   "duration": {
      "type": "integer",
      "title": "Duration",
      "default": 8,
      "x-order": 3,
      "description": "Duration of the generated audio in seconds."
    }

However, I don’t know how to edit this in Pickaxe

Hey @ryano562. Did you implement this through a connected action with your Pickaxe? Are you using the action code section?

Hi @ned_rvth , I’m using the connected action (Meta MusicGen). But there is no option to edit the action code with the pre-built action. I have done this before with Midjourney for example, and there is an “edit” option at the bottom of the action

@ned_rvth Ok I’ve been playing around with this for a while, and have gotten Meta MusicGen working with a custom action “Build New Action”. It generates the music and file for the length specified by the user.

However, the music file doesn’t actually display or download in my pickaxe. It shows as the image above, but there’s no audio file. Any ideas how to properly save the music file and display it to users?

import os
import requests
import time

def replicate_meta_musicgen(duration: str, prompt: str):
    """
    Generates music using the stereo-melody-large version of Meta MusicGen (version ID: 671ac645ce5e552cc63a54a2bbff63fcf798043055d2dac5fc9e36a837eedcfb)
    through Replicate's API, capping the clip at 12 seconds.

    After generation, creates an HTML file in the root directory to display an audio player
    and a download link, so the user can play or download directly from within Pickaxe.

    Args:
        duration (string): The duration of the music to be generated in seconds. Maxium 12.
        prompt (string): prompt for the music
    Envs:
        REPLICATE_API_TOKEN (string): Replicate API Token
    """

    # Insert your PYTHON code below. You can access environment variables using os.environ[].
    # Currently, only the requests library is supported, but more libraries will be available soon.
    # Use print statements or return values to display results to the user.
    # If you save a png, pdf, csv, jpg, webp, gif, or html file in the root directory, it will be automatically displayed to the user.
    # You do not have to call this function as the bot will automatically call and fill in the parameters.
   
    
    replicate_api_token = os.environ.get("REPLICATE_API_TOKEN")
    
    if not replicate_api_token:
        print("Error: REPLICATE_API_TOKEN environment variable not set.")
        return
    
    url = "https://api.replicate.com/v1/predictions"
    model_version = "671ac645ce5e552cc63a54a2bbff63fcf798043055d2dac5fc9e36a837eedcfb"
    
    # Cap the duration at 12 seconds
    safe_duration = min(int(duration), 12)
    
    # Use the prompt passed to the function
    data = {
        "version": model_version,
        "input": {
            "prompt": prompt,
            "duration": safe_duration
        }
    }
    
    headers = {
        "Authorization": f"Token {replicate_api_token}",
        "Content-Type": "application/json"
    }
    
    # Create the prediction
    try:
        response = requests.post(url, json=data, headers=headers)
        response.raise_for_status()
    except requests.exceptions.RequestException as e:
        print(f"Error creating prediction: {e}")
        return
    
    prediction = response.json()
    get_url = prediction["urls"]["get"]
    status = prediction["status"]
    
    # Poll until the prediction is complete
    while status not in ["succeeded", "failed", "canceled"]:
        time.sleep(2)
        poll_response = requests.get(get_url, headers=headers)
        prediction = poll_response.json()
        status = prediction["status"]
        print(f"Current status: {status}")
    
    # If succeeded, grab output audio URLs and embed in HTML
    if status == "succeeded":
        output_urls = prediction.get("output", [])
        if not output_urls:
            print("No audio URLs found in output.")
            return
        
        audio_url = output_urls[0]
        print(f"Music generation succeeded! Audio URL:\n{audio_url}\n")
        
        html_content = f"""<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Generated Music</title>
</head>
<body>
    <h2>Generated Music (Preview &amp; Download)</h2>
    <p>Below is your {safe_duration}-second track:</p>
    <audio controls>
        <source src="{audio_url}" type="audio/mpeg">
        Your browser does not support the audio element.
    </audio>
    <p>
        <a href="{audio_url}" download="generated_music.mp3">
            Download this audio
        </a>
    </p>
    <p>
        Feel free to tweak the prompt or request a longer/shorter track!
    </p>
</body>
</html>
"""
        # Write HTML file so it's displayed in Pickaxe
        with open("generated_music.html", "w") as f:
            f.write(html_content)
        
        print("An HTML file (generated_music.html) has been created with an embedded audio player.")
        print("Pickaxe should display this file inline, allowing you to play or download the track.")
    else:
        print(f"Music generation failed with status '{status}'. Details:")
        print(prediction)
2 Likes

Does anyone know how to get the audio player to work in the pickaxe? It works with the default Meta MusicGen Action, but when built custom, the player and download doesn’t work.

Hey @ryano562 - I never tried that action tbh as none of my projects use it. Maybe @dev_vlad or @nathaniel can point you in the right direction.