音频(实验性质)#

学习如何使用 Xinference 将音频转换为文本或将文本转换为音频。

介绍#

Audio API提供了三种与音频交互的方法:

  • 转录终端将音频转录为输入语言。

  • 翻译端点将音频转换为英文。

  • 转录终端将音频转录为输入语言。

API 端点

OpenAI 兼容端点

Transcription API

/v1/audio/transcriptions

Translation API

/v1/audio/translations

Speech API

/v1/audio/speech

支持的模型列表#

在Xinference中,以下模型支持音频API:

语音转文本#

  • whisper-tiny

  • whisper-tiny.en

  • whisper-base

  • whisper-base.en

  • whisper-medium

  • whisper-medium.en

  • whisper-large-v3

  • whisper-large-v3-turbo

  • Belle-distilwhisper-large-v2-zh

  • Belle-whisper-large-v2-zh

  • Belle-whisper-large-v3-zh

  • SenseVoiceSmall

文本转语音#

  • ChatTTS

  • CosyVoice

  • FishSpeech-1.5

  • F5-TTS

快速入门#

转录#

Transcription API 模仿了 OpenAI 的 create transcriptions API。你可以通过 cURL、OpenAI Client 或者 Xinference 的 Python 客户端来尝试 Transcription API:

curl -X 'POST' \
  'http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1/audio/transcriptions' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "<MODEL_UID>",
    "file": "<audio bytes>",
  }'

翻译#

Translation API 模仿了 OpenAI 的 create translations API。你可以通过 cURL、OpenAI Client 或 Xinference 的 Python 客户端来尝试使用 Translation API:

curl -X 'POST' \
  'http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1/audio/translations' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "<MODEL_UID>",
    "file": "<audio bytes>",
  }'

语音#

Transcription API 模仿了 OpenAI 的 create speech API。你可以通过 cURL、OpenAI Client 或者 Xinference 的 Python 客户端来尝试 Speech API:

Speech API 默认使用非流式

  1. ChatTTS 的流式输出不如非流式的效果好,参考:2noise/ChatTTS#564

  2. 流式要求 ffmpeg<7:https://pytorch.org/audio/stable/installation.html#optional-dependencies

curl -X 'POST' \
  'http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1/audio/speech' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "<MODEL_UID>",
    "input": "<The text to generate audio for>",
    "voice": "echo",
    "stream": True,
  }'

ChatTTS 使用#

基本使用,参考 语音使用章节

固定音色。我们可以使用由 6drf21e/ChatTTS_Speaker 提供的固定音色,下载 evaluation_result.csv ,以 seed_2155 音色作为例子,我们使用 emb_data 列的数据。

import pandas as pd

df = pd.read_csv("evaluation_results.csv")
emb_data_2155 = df[df['seed_id'] == 'seed_2155'].iloc[0]["emb_data"]

使用 seed_2155 固定音色来创建语音。

from xinference.client import Client

client = Client("http://<XINFERENCE_HOST>:<XINFERENCE_PORT>")

model = client.get_model("<MODEL_UID>")
resp_bytes = model.speech(
    voice=emb_data_2155,
    input=<The text to generate audio for>
)

CosyVoice 模型使用#

基本使用,加载模型 CosyVoice-300M-SFT

curl -X 'POST' \
  'http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1/audio/speech' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "<MODEL_UID>",
    "input": "<The text to generate audio for>",
    # ['中文女', '中文男', '日语男', '粤语女', '英文女', '英文男', '韩语女']
    "voice": "中文女"
  }'

克隆声音,加载模型 CosyVoice-300M

from xinference.client import Client

client = Client("http://<XINFERENCE_HOST>:<XINFERENCE_PORT>")

model = client.get_model("<MODEL_UID>")

zero_shot_prompt_text = ""
# The zero shot prompt file is the voice file
# the words said in the file should be identical to zero_shot_prompt_text
with open(zero_shot_prompt_file, "rb") as f:
    zero_shot_prompt = f.read()

speech_bytes = model.speech(
    "<The text to generate audio for>",
    prompt_text=zero_shot_prompt_text,
    prompt_speech=zero_shot_prompt,
)

跨语言使用,加载模型 CosyVoice-300M

from xinference.client import Client

client = Client("http://<XINFERENCE_HOST>:<XINFERENCE_PORT>")

model = client.get_model("<MODEL_UID>")

# the file that reads in some language
with open(cross_lingual_prompt_file, "rb") as f:
    cross_lingual_prompt = f.read()

speech_bytes = model.speech(
    "<The text to generate audio for>",  # text could be another language
    prompt_speech=cross_lingual_prompt,
)

基于指令的声音合成,加载模型 CosyVoice-300M-Instruct

from xinference.client import Client

client = Client("http://<XINFERENCE_HOST>:<XINFERENCE_PORT>")

model = client.get_model("<MODEL_UID>")

response = model.speech(
    "在面对挑战时,他展现了非凡的<strong>勇气</strong>与<strong>智慧</strong>。",
    voice="中文男",
    instruct_text="Theo 'Crimson', is a fiery, passionate rebel leader. "
    "Fights with fervor for justice, but struggles with impulsiveness.",
)

更多指令和例子,可以参考 https://fun-audio-llm.github.io/

FishSpeech 模型使用#

基本使用,参考 语音使用章节

克隆语音,启动模型 FishSpeech-1.5。请使用 prompt_speech`而不是 `reference_audio 以及 prompt_text 而不是 reference_text 来为 FishSpeech 模型提供参考音频。这个参数和 CosyVoice 的语音克隆保持一致。

from xinference.client import Client

client = Client("http://<XINFERENCE_HOST>:<XINFERENCE_PORT>")

model = client.get_model("<MODEL_UID>")

# The reference audio file is the voice file
# the words said in the file should be identical to reference_text
with open(reference_audio_file, "rb") as f:
    reference_audio = f.read()
reference_text = ""  # text in the audio

speech_bytes = model.speech(
    "<The text to generate audio for>",
    prompt_speech=reference_audio,
    prompt_text=reference_text
)

SenseVoiceSmall 离线使用#

现在 SenseVoiceSmall 使用一个小的 VAD 模型 fsmn-vad,因此它需要网络来下载。

对于离线环境,你可以提前下载这个 VAD 模型。

huggingface 或者 modelscope 下载。假设下载到 /path/to/fsmn-vad

然后当用 Web UI 加载 SenseVoiceSmall 时,添加额外选项,key 是 vad_model,值是之前的下载路径 /path/to/fsmn-vad。用命令行加载时,增加选项 --vad_model /path/to/fsmn-vad

Kokoro 模型使用#

Kokoro模型支持多语言,默认是英文。如果你想使用非默认语言,例如中文,则需要安装额外依赖包并且在模型启动时增加对应参数。

  1. pip install misaki[zh]

  2. 使用 lang_code=’z’ 参数初始化模型,可以参考 kokoro source code 查看所有支持的 lang_code。如果你是通过 Web UI启动的模型,则需要添加额外参数,key是 lang_code,value是 z。如果你是通过 xinference client启动的模型,则可以参考如下代码传递参数:

    model_uid = client.launch_model(
        model_name="Kokoro-82M",
        model_type="audio",
        compile=False,
        download_hub="huggingface",
        lang_code="z",
    )
    
  3. 当推理时,需要使用 ‘z’ 开头的 voice,例如:zf_xiaoyi。目前支持的 voices 可以参考 https://huggingface.co/hexgrad/Kokoro-82M/tree/main/voices。使用方法如下:

    input_string = "重新启动即可更新"
    response = model.speech(input_string, voice="zf_xiaoyi")