Skip to content

文本转音频

pipeline pipeline

文本转音频管道将文本生成音频。

示例

以下展示了一个使用此管道的简单示例。

from txtai.pipeline import TextToAudio

# 创建并运行管道
tta = TextToAudio()
tta("在此描述要生成的音频")

配置驱动示例

管道可以通过Python或配置运行。管道可以通过配置中使用管道的类名的小写形式来实例化。配置驱动的管道可以通过工作流API运行。

config.yml

# 使用类名的小写形式创建管道
texttoaudio:

# 使用工作流运行管道
workflow:
  tta:
    tasks:
      - action: texttoaudio

使用工作流运行

from txtai import Application

# 使用工作流创建并运行管道
app = Application("config.yml")
list(app.workflow("tta", ["在此描述要生成的音频"]))

使用API运行

CONFIG=config.yml uvicorn "txtai.api:app" &

curl \
  -X POST "http://localhost:8000/workflow" \
  -H "Content-Type: application/json" \
  -d '{"name":"tta", "elements":["在此描述要生成的音频"]}'

方法

管道的Python文档。

__init__(path=None, quantize=False, gpu=True, model=None, rate=None, **kwargs)

Source code in txtai/pipeline/audio/texttoaudio.py
14
15
16
17
18
19
20
21
22
def __init__(self, path=None, quantize=False, gpu=True, model=None, rate=None, **kwargs):
    if not SCIPY:
        raise ImportError('TextToAudio pipeline is not available - install "pipeline" extra to enable.')

    # Call parent constructor
    super().__init__("text-to-audio", path, quantize, gpu, model, **kwargs)

    # Target sample rate, defaults to model sample rate
    self.rate = rate

__call__(text, maxlength=512)

Generates audio from text.

This method supports text as a string or a list. If the input is a string, the return type is a single audio output. If text is a list, the return type is a list.

Parameters:

Name Type Description Default
text

text|list

required
maxlength

maximum audio length to generate

512

Returns:

Type Description

list of (audio, sample rate)

Source code in txtai/pipeline/audio/texttoaudio.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def __call__(self, text, maxlength=512):
    """
    Generates audio from text.

    This method supports text as a string or a list. If the input is a string,
    the return type is a single audio output. If text is a list, the return type is a list.

    Args:
        text: text|list
        maxlength: maximum audio length to generate

    Returns:
        list of (audio, sample rate)
    """

    # Format inputs
    texts = [text] if isinstance(text, str) else text

    # Run pipeline
    results = [self.convert(x) for x in self.pipeline(texts, forward_params={"max_new_tokens": maxlength})]

    # Extract results
    return results[0] if isinstance(text, str) else results