Kimi-Audio

repository·master·Indexed 26 days ago

https://github.com/moonshotai/kimi-audio

An open-source audio foundation model and inference library designed for universal audio tasks, including Automatic Speech Recognition (ASR), audio question answering, captioning, and end-to-end speech conversation. The library supports loading pretrained models like Kimi-Audio-7B-Instruct, performing Supervised Fine-Tuning (SFT) for audio understanding, and generating both text and audio waveforms.

Tokens
3.5K
Snippets
6
Records
12
Agent score
39%

What's inside kimi-audio

  1. Use the Kimi-Audio Evaluation Toolkit

    master

    The Kimi-Audio-Evalkit is designed to address challenges in evaluating audio foundation models (inconsistent metrics, varying configurations, etc.).

    Key Features:

    • Integration: Supports Kimi-Audio and other recent audio LLMs.
    • Standardized Metrics: Implements standardized metric calculation and integrates LLMs for intelligent judging (e.g., for Audio Question Answering/AQA).
    • Unified Comparison: Provides a platform for side-by-side comparisons using shareable inference 'recipes'.
    • Speech Conversation Benchmarking: Includes a benchmark for evaluating speech conversation abilities such as control, empathy, and style.

    Access the toolkit here: Kimi-Audio-Evalkit.

  2. Evaluate Kimi-Audio using the Generation Testset

    master
    To benchmark and evaluate the conversational capabilities of audio-based dialogue models, use the Kimi-Audio-Generation-Testset available on Hugging Face. This dataset contains audio files with various instructions and conversational prompts in Chinese, designed to assess a model's ability to generate relevant and appropriately styled audio responses.
  3. Finetune Kimi-Audio

    master

    Follow these four steps to finetune the Kimi-Audio model:

    1. Download Pretrained Model: Download moonshotai/Kimi-Audio-7B and save it to output/pretrained_hf.
    2. Preprocess Data: Extract semantic tokens from your .jsonl data file.
    3. Run Finetuning: Execute the finetuning script using the processed data.
    4. Export for Inference: Convert the resulting checkpoints into a format suitable for inference.

    Note: For tasks other than ASR (e.g., speech conversation or text-to-speech), you may need to modify the tokenize_message function in finetune_codes/datasets.py and tune hyperparameters in finetune_codes/finetune_ds.sh.

    # 1. Download pretrained model
    CUDA_VISIBLE_DEVICES=0 python -m finetune_codes.model --model_name "moonshotai/Kimi-Audio-7B" --output_dir "output/pretrained_hf"
    
    # 2. Preprocess data and extract semantic tokens
    CUDA_VISIBLE_DEVICES=0 python -m finetune_codes.extract_semantic_codes --input_file "finetune_codes/demo_data/audio_understanding/data.jsonl" --output_file "finetune_codes/demo_data/audio_understanding/data_with_semantic_codes.jsonl"
    
    # 3. Finetune the model
    bash finetune_codes/finetune_ds.sh \
        --model_path "output/pretrained_hf" \
        --data "finetune_codes/demo_data/audio_understanding/data_with_semantic_codes.jsonl"
    
    # 4. Convert finetuned model for inference
    CUDA_VISIBLE_DEVICES=0 python -m finetune_codes.model --model_name "moonshotai/Kimi-Audio-7B" \
    --action "export_model" \
    --input_dir "output/kimiaudio_ckpts" \
    --output_dir "output/finetuned_hf_for_inference"
  4. Prepare data for Audio Understanding SFT

    master

    Supervised Fine-Tuning (SFT) for Audio Understanding tasks requires data in a .jsonl format where each line is a JSON object. For an ASR (Automatic Speech Recognition) task, the structure must include a task_type of understanding and a conversation array containing user text instructions, user audio paths, and assistant text transcripts.

    You can use the provided script to prepare Librispeech ASR data or prepare your own data following this schema:

    {
        "task_type": "understanding",
        "conversation": [
            {
                "role": "user",
                "message_type": "text",
                "content": "Please transcribe the spoken content into written text."
            },
            {
                "role": "user",
                "message_type": "audio",
                "content": # Audio Path
            },
            {
                "role": "assistant",
                "message_type": "text",
                "content": # Transcript
            }
        ]
    }

    To prepare Librispeech ASR data using the demo script:

    python finetune_codes/demo_data/audio_understanding/prepare_librispeech_asrtask.py --output_dir "output/data/librispeech"
  5. Use KimiAudio for Audio-to-Text (ASR) and Conversational tasks

    master

    The KimiAudio class from kimia_infer.api.kimia allows you to perform Automatic Speech Recognition (ASR) and multi-modal conversations.

    Loading the Model

    Initialize the model with a model path (e.g., moonshotai/Kimi-Audio-7B-Instruct) and set load_detokenizer=True to enable audio generation.

    Sampling Parameters

    You can control generation quality using a dictionary of parameters including audio_temperature, audio_top_k, text_temperature, text_top_k, and repetition penalties for both audio and text.

    Generating Output

    Use the .generate() method with a list of messages. Each message is a dictionary with role, message_type, and content.

    • message_type can be text, audio (file path), or audio-text (for assistant turns in multi-turn conversations).
    • Use output_type="text" to get only text output.
    • Use output_type="both" to get both audio waveforms and text output.
    import soundfile as sf
    from kimia_infer.api.kimia import KimiAudio
    
    # --- 1. Load Model ---
    model_path = "moonshotai/Kimi-Audio-7B-Instruct" 
    model = KimiAudio(model_path=model_path, load_detokenizer=True)
    
    # --- 2. Define Sampling Parameters ---
    sampling_params = {
        "audio_temperature": 0.8,
        "audio_top_k": 10,
        "text_temperature": 0.0,
        "text_top_k": 5,
        "audio_repetition_penalty": 1.0,
        "audio_repetition_window_size": 64,
        "text_repetition_penalty": 1.0,
        "text_repetition_window_size": 16,
    }
    
    # --- 3. Example 1: Audio-to-Text (ASR) ---
    messages_asr = [
        {"role": "user", "message_type": "text", "content": "Please transcribe the following audio:"},
        {"role": "user", "message_type": "audio", "content": "test_audios/asr_example.wav"}
    ]
    
    # Generate only text output
    _, text_output = model.generate(messages_asr, **sampling_params, output_type="text")
    print(">>> ASR Output Text: ", text_output)
    
    # --- 4. Example 2: Audio-to-Audio/Text Conversation ---
    messages_conversation = [
        {"role": "user", "message_type": "audio", "content": "test_audios/qa_example.wav"}
    ]
    
    # Generate both audio and text output
    wav_output, text_output = model.generate(messages_conversation, **sampling_params, output_type="both")
    
    # Save the generated audio
    output_audio_path = "output_audio.wav"
    sf.write(output_audio_path, wav_output.detach().cpu().view(-1).numpy(), 24000)
    
    # --- 5. Example 3: Multi-turn Conversation ---
    messages = [
        {"role": "user", "message_type": "audio", "content": "test_audios/multiturn/case2/multiturn_q1.wav"},
        {"role": "assistant", "message_type": "audio-text", "content": ["test_audios/multiturn/case2/multiturn_a1.wav", "当然可以,这很简单。一二三四五六七八九十。"]},
        {"role": "user", "message_type": "audio", "content": "test_audios/multiturn/case2/multiturn_q2.wav"}
    ]
    wav, text = model.generate(messages, **sampling_params, output_type="both")
  6. Install Kimi-Audio

    master

    You can install Kimi-Audio by cloning the repository and installing requirements, or directly via pip.

    Option 1: Clone the repository

    git clone https://github.com/MoonshotAI/Kimi-Audio.git
    cd Kimi-Audio
    git submodule update --init --recursive
    pip install -r requirements.txt

    Option 2: Install via pip

    pip install torch
    pip install git+https://github.com/MoonshotAI/Kimi-Audio.git
    git clone https://github.com/MoonshotAI/Kimi-Audio.git
    cd Kimi-Audio
    git submodule update --init --recursive
    pip install -r requirements.txt
  7. Reference: KimiAudio.generate parameters

    master

    The generate method accepts the following key arguments:

    • messages: A list of message dictionaries.
      • role: e.g., user, assistant.
      • message_type: text, audio, or audio-text.
      • content: String (for text), file path (for audio), or a list [audio_path, text] (for audio-text).
    • output_type: Determines the return format. Options are text or both.
    • **sampling_params: Keyword arguments for controlling generation (e.g., audio_temperature, text_top_k, etc.).
  8. Cite Kimi-Audio Technical Report

    master

    If using Kimi-Audio in research or applications, please cite the technical report using the following BibTeX entry:

    @misc{kimiteam2025kimiaudiotechnicalreport,
          title={Kimi-Audio Technical Report}, 
          author={KimiTeam and Ding Ding and Zeqian Ju and Yichong Leng and Songxiang Liu and Tong Liu and Zeyu Shang and Kai Shen and Wei Song and Xu Tan and Heyi Tang and Zhengtao Wang and Chu Wei and Yifei Xin and Xinran Xu and Jianwei Yu and Yutao Zhang and Xinyu Zhou and Y. Charles and Jun Chen and Yanru Chen and Yulun Du and Weiran He and Zhenxing Hu and Guokun Lai and Qingcheng Li and Yangyang Liu and Weidong Sun and Jianzhou Wang and Yuzhi Wang and Yuefeng Wu and Yuxin Wu and Dongchao Yang and Hao Yang, Ying Yang, and Zhilin Yang, and Aoxiong Yin, and Ruibin Yuan, and Yutong Zhang, and Zaida Zhou},
          year={2025},
          eprint={2504.18425},
          archivePrefix={arXiv},
          primaryClass={eess.AS},
          url={https://arxiv.org/abs/2504.18425}, 
    }