Close Menu
techwavedispatch.com

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    [2512.10817] Extrapolation of Periodic Functions Using Binary Encoding of Continuous Numerical Values

    November 13, 2026

    Israel’s VCs see opportunity for challengers in Nvidia’s turmoil

    November 13, 2026

    Deploy MusicGen in no time with Inference Endpoints

    November 13, 2026
    Facebook X (Twitter) Instagram
    Trending
    • [2512.10817] Extrapolation of Periodic Functions Using Binary Encoding of Continuous Numerical Values
    • Israel’s VCs see opportunity for challengers in Nvidia’s turmoil
    • Deploy MusicGen in no time with Inference Endpoints
    • 4 Best Walking Pads for Small Spaces and Standing Desks (2026)
    • From Nodes to Pixels: Topological and Structural Two-View Graph Imaging
    • Fine-tune Llama 2 with DPO
    • [2611.17309] Classical Energies Learn to Reason 1,000x Faster than Looped Models
    • Jensen Huang didn’t show confidence in his successor, report says
    Facebook X (Twitter) Instagram
    techwavedispatch.com
    Subscribe
    Friday, November 13
    • Home
    • AI Tools & Software
    • Machine Learning
    • Generative AI
    • Robotics & Automation
    • Future Tech & Hardware
    • AI in Business
    • Ethics & Regulations
    • More
      • Gadgets & Smart Devices
      • Cybersecurity & AI
      • Tech Breakthroughs
    techwavedispatch.com
    Home»AI Tools & Software»Deploy MusicGen in no time with Inference Endpoints
    AI Tools & Software

    Deploy MusicGen in no time with Inference Endpoints

    adminBy adminNovember 13, 2026No Comments5 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Deploy MusicGen in no time with Inference Endpoints
    Share
    Facebook Twitter LinkedIn Pinterest Email

    MusicGen is a powerful music generation model that takes in text prompt and an optional melody to output music. This blog post will guide you through generating music with MusicGen using Inference Endpoints.

    Inference Endpoints allow us to write custom inference functions called custom handlers. These are particularly useful when a model is not supported out-of-the-box by the transformers high-level abstraction pipeline.

    transformers pipelines offer powerful abstractions to run inference with transformers-based models. Inference Endpoints leverage the pipeline API to easily deploy models with only a few clicks. However, Inference Endpoints can also be used to deploy models that don’t have a pipeline, or even non-transformer models! This is achieved using a custom inference function that we call a custom handler.

    Let’s demonstrate this process using MusicGen as an example. To implement a custom handler function for MusicGen and deploy it, we will need to:

    1. Duplicate the MusicGen repository we want to serve,
    2. Write a custom handler in handler.py and any dependencies in requirements.txt and add them to the duplicated repository,
    3. Create Inference Endpoint for that repository.

    Or simply use the final result and deploy our custom MusicGen model repo, where we just followed the steps above 🙂



    Let’s go!

    First, we will duplicate the facebook/musicgen-large repository to our own profile using repository duplicator.

    Then, we will add handler.py and requirements.txt to the duplicated repository.
    First, let’s take a look at how to run inference with MusicGen.

    from transformers import AutoProcessor, MusicgenForConditionalGeneration
    
    processor = AutoProcessor.from_pretrained("facebook/musicgen-large")
    model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-large")
    
    inputs = processor(
        text=["80s pop track with bassy drums and synth"],
        padding=True,
        return_tensors="pt",
    )
    audio_values = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=256)
    

    Let’s hear what it sounds like:

    Your browser does not support the audio element.

    Optionally, you can also condition the output with an audio snippet i.e. generate a complimentary snippet which combines the text generated audio with an input audio.

    from transformers import AutoProcessor, MusicgenForConditionalGeneration
    from datasets import load_dataset
    
    processor = AutoProcessor.from_pretrained("facebook/musicgen-large")
    model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-large")
    
    dataset = load_dataset("sanchit-gandhi/gtzan", split="train", streaming=True)
    sample = next(iter(dataset))["audio"]
    
    
    sample["array"] = sample["array"][: len(sample["array"]) // 2]
    
    inputs = processor(
        audio=sample["array"],
        sampling_rate=sample["sampling_rate"],
        text=["80s blues track with groovy saxophone"],
        padding=True,
        return_tensors="pt",
    )
    audio_values = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=256)
    

    Let’s give it a listen:

    Your browser does not support the audio element.

    In both the cases the model.generate method produces the audio and follows the same principles as text generation. You can read more about it in our how to generate blog post.

    Alright! With the basic usage outlined above, let’s deploy MusicGen for fun and profit!

    First, we’ll define a custom handler in handler.py. We can use the Inference Endpoints template and override the __init__ and __call__ methods with our custom inference code. __init__ will initialize the model and the processor, and __call__ will take the data and return the generated music. You can find the modified EndpointHandler class below. 👇

    from typing import Dict, List, Any
    from transformers import AutoProcessor, MusicgenForConditionalGeneration
    import torch
    
    class EndpointHandler:
        def __init__(self, path=""):
            
            self.processor = AutoProcessor.from_pretrained(path)
            self.model = MusicgenForConditionalGeneration.from_pretrained(path, torch_dtype=torch.float16).to("cuda")
    
        def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
            """
            Args:
                data (:dict:):
                    The payload with the text prompt and generation parameters.
            """
            
            inputs = data.pop("inputs", data)
            parameters = data.pop("parameters", None)
    
            
            inputs = self.processor(
                text=[inputs],
                padding=True,
                return_tensors="pt",).to("cuda")
    
            
            if parameters is not None:
                with torch.autocast("cuda"):
                    outputs = self.model.generate(**inputs, **parameters)
            else:
                with torch.autocast("cuda"):
                    outputs = self.model.generate(**inputs,)
    
            
            prediction = outputs[0].cpu().numpy().tolist()
    
            return [{"generated_audio": prediction}]
    

    To keep things simple, in this example we are only generating audio from text, and not conditioning it with a melody.
    Next, we will create a requirements.txt file containing all the dependencies we need to run our inference code:

    transformers==4.31.0
    accelerate>=0.20.3
    

    Uploading these two files to our repository will suffice to serve the model.

    inference-files

    We can now create the Inference Endpoint. Head to the Inference Endpoints page and click Deploy your first model. In the “Model repository” field, enter the identifier of your duplicated repository. Then select the hardware you want and create the endpoint. Any instance with a minimum of 16 GB RAM should work for musicgen-large.

    Create Endpoint

    After creating the endpoint, it will be automatically launched and ready to receive requests.

    Endpoint Running

    We can query the endpoint with the below snippet.

    curl URL_OF_ENDPOINT \
    -X POST \
    -d '{"inputs":"happy folk song, cheerful and lively"}' \
    -H "Authorization: {YOUR_TOKEN_HERE}" \
    -H "Content-Type: application/json"
    

    We can see the following waveform sequence as output.

    [{"generated_audio":[[-0.024490159,-0.03154691,-0.0079551935,-0.003828604, ...]]}]
    

    Here’s how it sounds like:

    Your browser does not support the audio element.

    You can also hit the endpoint with huggingface-hub Python library’s InferenceClient class.

    from huggingface_hub import InferenceClient
    
    client = InferenceClient(model = URL_OF_ENDPOINT)
    response = client.post(json={"inputs":"an alt rock song"})
    
    
    output = eval(response)[0]["generated_audio"]
    

    You can convert the generated sequence to audio however you want. You can use scipy in Python to write it to a .wav file.

    import scipy
    import numpy as np
    
    
    scipy.io.wavfile.write("musicgen_out.wav", rate=32000, data=np.array(output[0]))
    

    And voila!

    Play with the demo below to try the endpoint out.



    Conclusion

    In this blog post, we have shown how to deploy MusicGen using Inference Endpoints with a custom inference handler. The same technique can be used for any other model in the Hub that does not have an associated pipeline. All you have to do is override the Endpoint Handler class in handler.py, and add requirements.txt to reflect your project’s dependencies.



    Read More

    Deploy endpoints Inference MusicGen Time
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    admin
    • Website

    Related Posts

    Fine-tune Llama 2 with DPO

    November 13, 2026

    Run On-Device LLMs in Apple Devices

    November 13, 2026

    UniPipe: Unified Inference Serving and Fine-Tuning for Deadline-Constrained DNN Surrogate Workflows

    November 13, 2026
    Leave A Reply Cancel Reply

    Top Posts

    NVIDIA Is Investigating Reports That CEO Jensen Huang Was The Target Of Chinese Spy Efforts

    November 8, 202619 Views

    ADIndex: Layer-Adaptive Sparse Attention Indexing via Group-Level Distillation

    November 11, 20265 Views

    Anthropic releases new Mythos and Fable models amid RSI concerns

    November 9, 20265 Views

    Anthropic says AI is eroding Nvidia’s CUDA lead, cutting an AMD chip switch from months to three days

    November 11, 20264 Views
    Don't Miss
    Machine Learning

    [2512.10817] Extrapolation of Periodic Functions Using Binary Encoding of Continuous Numerical Values

    By adminNovember 13, 20260

    [Submitted on 11 Dec 2025 (v1), last revised 11 Nov 2026 (this version, v2)] View…

    Israel’s VCs see opportunity for challengers in Nvidia’s turmoil

    November 13, 2026

    Deploy MusicGen in no time with Inference Endpoints

    November 13, 2026

    4 Best Walking Pads for Small Spaces and Standing Desks (2026)

    November 13, 2026
    About Us

    Welcome to TechWave Dispatch

    At TechWave Dispatch, we bring you the latest news, insights, and developments from the ever-evolving world of technology. Our mission is to keep readers informed about the innovations shaping our digital future.

    From artificial intelligence and machine learning to cybersecurity, robotics, smart devices, and groundbreaking technologies, we cover the topics that matter in today's fast-moving tech landscape.

    Our Picks

    [2512.10817] Extrapolation of Periodic Functions Using Binary Encoding of Continuous Numerical Values

    November 13, 2026

    Israel’s VCs see opportunity for challengers in Nvidia’s turmoil

    November 13, 2026

    Deploy MusicGen in no time with Inference Endpoints

    November 13, 2026
    Most Popular

    Who Controls Scientific Discovery? – O’Reilly

    November 6, 20260 Views

    After blocking Muse, Amazon now lets users text Alexa to help them shop

    November 6, 20260 Views

    A controlled world for robot learning and evaluation

    November 6, 20260 Views
    Facebook X (Twitter) Instagram Pinterest
    • Terms and Conditions
    • Privacy Policy
    • Contact Us
    • About Us
    © 2026 TechWave Dispatch. All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.