Install TangoFlux
mainInstall the TangoFlux package directly from the GitHub repository using pip:
pip install git+https://github.com/declare-lab/TangoFluxrepository·main·Indexed 21 days ago
https://github.com/declare-lab/tangofluxTangoFlux is a high-speed text-to-audio generation model producing 44.1kHz stereo audio up to 30 seconds long using Diffusion Transformers (DiT/MMDiT) and rectified flow trajectories. It features a Python API, CLI, and a web interface, as well as custom nodes for ComfyUI including TangoFluxLoader, TangoFluxSampler, and TangoFluxVAEDecodeAndPlay. The library supports multi-GPU training via Hugging Face accelerate and Clap-Ranked Preference Optimization (CRPO).
Install the TangoFlux package directly from the GitHub repository using pip:
pip install git+https://github.com/declare-lab/TangoFluxThe CRPO (Clap-Ranked Preference Optimization) dataset can be generated using two scripts:
tangoflux/generate_crpo.py: Generates the initial dataset using a prompt bank and model weights. You can specify sample size and samples per prompt.tangoflux/label_crpo.py: Labels the generated audio and constructs preference pairs, producing a train.json file suitable for train_dpo.py.You can iterate this process by repeating the steps with updated model weights.
TangoFlux uses Hugging Face's accelerate for multi-GPU training.
accelerate config to set up your environment.configs/tangoflux_config.yaml.accelerate launch.Standard Training Command:
CUDA_VISIBLE_DEVICES=0,1 accelerate launch --config_file='configs/accelerator_config.yaml' tangoflux/train.py --checkpointing_steps="best" --save_every=5 --config='configs/tangoflux_config.yaml'DPO (Preference Optimization) Training:
To perform DPO training, ensure your training data contains chosen, reject, caption, and duration fields. Use tangoflux/train_dpo.py instead:
CUDA_VISIBLE_DEVICES=0,1 accelerate launch --config_file='configs/accelerator_config.yaml' tangoflux/train_dpo.py --checkpointing_steps="best" --save_every=5 --config='configs/tangoflux_config.yaml'TangoFlux nodes are located in the "TangoFlux" category within the ComfyUI node menu. The core nodes are:
TangoFluxLoader: Loads the model weights and configuration.TangoFluxSampler: Performs the audio generation process.TangoFluxVAEDecodeAndPlay: Decodes the latent representation into playable audio.You can speed up TangoFlux by up to 2x using TeaCache without significant audio quality degradation. This is a training-free method. On an A800 GPU, inference latency can be reduced from ~4.08s to ~1.95s using TeaCache (0.4).
TangoFlux models can be acquired in three ways:
TangoFluxLoader node will automatically download all required models upon its first use.install.py script to download the models.models/tangoflux.flan-t5-large from HuggingFace into models/text_encoders/google-flan-t5-large.To use TangoFlux within ComfyUI, you must install the custom nodes into your custom_nodes directory and install the necessary Python dependencies.
custom_nodes directory.ComfyUI-TangoFlux.install.py script located in the comfyui subdirectory to install requirements.Alternatively, you can install the nodes using the ComfyUI Manager.
cd ComfyUI/custom_nodes
git clone https://github.com/declare-lab/TangoFlux ComfyUI-TangoFlux
cd ComfyUI-TangoFlux/comfyui
python install.pyTangoFlux can be used as a custom node within ComfyUI for advanced graph-based audio generation pipelines.
Refer to the ComfyUI-TangoFlux repository to install the necessary custom nodes.
To start a local web-based demonstration interface, run the following command:
tangoflux-demoTo install TangoFlux directly from the GitHub repository, use the following command:
!pip install git+https://github.com/declare-lab/TangoFlux.gitUse the TangoFluxInference class to generate audio from text prompts.
Important: You must specify a duration between 1 and 30 seconds when calling model.generate.
import torchaudio
from tangoflux import TangoFluxInference
model = TangoFluxInference(name='declare-lab/TangoFlux')
# duration must be between 1 and 30
audio = model.generate('Hammer slowly hitting the wooden table', steps=50, duration=10)
torchaudio.save('output.wav', audio, 44100)To save the generated audio using torchaudio, ensure the tensor is in the correct 2D format [channels, samples]. If the output is a 1D tensor, use .unsqueeze(0) to add the channel dimension.
import torchaudio
# Ensure audio is in the correct format (2D Tensor: [channels, samples])
if len(audio.shape) == 1:
audio_tensor = audio.unsqueeze(0)
elif len(audio.shape) == 2:
audio_tensor = audio
else:
raise ValueError(f"Unexpected audio tensor shape: {audio.shape}")
# Save the audio as a .wav file
torchaudio.save('generated_audio.wav', audio_tensor, sample_rate=44100)Use the tangoflux command to generate audio files from text prompts via the terminal. Use the --duration flag to specify length in seconds and --steps to control generation quality.
tangoflux "Hammer slowly hitting the wooden table" output.wav --duration 10 --steps 50