跳到主要内容

文本转音频

pipeline pipeline

文本转音频流水线从文本生成音频。

示例

以下展示了使用此流水线的一个简单示例。

from txtai.pipeline import TextToAudio

# Create and run pipeline
tta = TextToAudio()
tta("Describe the audio to generate here")

有关更详细的示例,请参阅下方链接。

笔记本 描述
生成式音频 使用生成式音频工作流讲故事 Open In Colab

配置驱动的示例

流水线可以使用 Python 或配置运行。流水线可以在 配置 中使用流水线的全小写名称实例化。配置驱动的流水线可以使用 工作流API 运行。

config.yml

# Create pipeline using lower case class name
texttoaudio:

# Run pipeline with workflow
workflow:
  tta:
    tasks:
      - action: texttoaudio

使用工作流运行

from txtai import Application

# Create and run pipeline with workflow
app = Application("config.yml")
list(app.workflow("tta", ["Describe the audio to generate here"]))

使用 API 运行

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

curl \
  -X POST "http://localhost:8000/workflow" \
  -H "Content-Type: application/json" \
  -d '{"name":"tta", "elements":["Describe the audio to generate here"]}'

方法

流水线的 Python 文档。

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

源代码位于 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)

从文本生成音频。

此方法支持文本为字符串或列表。如果输入是字符串,则返回类型为单个音频输出。如果文本是列表,则返回类型是列表。

参数

名称 类型 描述 默认值
text

text|list

必需
maxlength

生成音频的最大长度

512

返回值

类型 描述

列表 (音频, 采样率)

源代码位于 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