piper-without-espeakをDocker環境でローカルで動かす

初めに

TTSライブラリの一つであるpiperがあります。こちらはespeakpiper-phonemizerに依存しているため、依存しないようにしたライブライであるpiper-without-espeakを動かしていきます。Windows環境ではインストールできないため、Docker環境でlinux上で動かします

以下で記事のリポジトリを公開しています

github.com

開発環境

実行

今回はlinux向けにバイナリがリリースされているのでこちらを使用していきます。

[64-bit desktop Linux] https://github.com/rhasspy/piper/releases/download/v1.2.0/piper_amd64.tar.gz

ソースコードからビルドすることもできます。

If you want to build from source, see the Makefile and C++ source. You must download and extract piper-phonemize to lib/Linux-$(uname -m)/piper_phonemize before building. For example, lib/Linux-x86_64/piper_phonemize/lib/libpiper_phonemize.so should exist for AMD/Intel machines (as well as everything else from libpiper_phonemize-amd64.tar.gz).

以下がDockerファイルです

# Base Image: Ubuntu
FROM ubuntu:22.04

# Install system dependencies: Python3/pip, ONNX Runtime (CPU), sox, ALSA utils, and curl
RUN apt-get update && apt-get install -y \
    python3 python3-pip \
    sox alsa-utils \
    curl ca-certificates && \
    rm -rf /var/lib/apt/lists/*

# Download and extract Piper pre-built binary
ENV PIPER_VERSION=1.2.0
RUN curl -L "https://github.com/rhasspy/piper/releases/download/v${PIPER_VERSION}/piper_amd64.tar.gz" \
    | tar -xz -C /usr/local && \
    # Move binary to PATH
    cp /usr/local/piper/piper /usr/local/bin/piper && \
    chmod +x /usr/local/bin/piper

# Install Piper via pip (includes onnxruntime and phonemizer)
RUN pip3 install --no-cache-dir piper-tts

# (Optional) For GPU support, uncomment the next line to install onnxruntime-gpu
# RUN pip3 install --no-cache-dir onnxruntime-gpu

# Create a directory for Piper data (voices) and designate it as a volume
RUN mkdir -p /data
VOLUME ["/data"]

# Set the entrypoint to Piper. It will read from STDIN by default.
# Include default --data-dir and --download-dir pointing to /data for persistence.
ENTRYPOINT ["piper", "--data-dir", "/data", "--download-dir", "/data"]

# By default, show help (can be overridden by supplying commands)
CMD ["--help"]

また 以下で実行できます。(Windowspowershellです)

echo \"This is a test\" | docker run -i --rm -v ${PWD}/piper_data:/data piper-tts --model en_US-lessac-medium --output_file /data/output.wav

実際に生成した音声は以下になります

soundcloud.com

onnx-gpuで動かす場合

onnxのgpuで動かす場合は以下のようにDockerfileを定義します

# ベースイメージ: CUDA 12.4 Runtime + Ubuntu 22.04
FROM nvidia/cuda:12.4.0-runtime-ubuntu22.04

# 環境変数を設定し、tzdataの入力を回避
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Tokyo

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        software-properties-common \
        wget \
        libsndfile1 \
        tzdata && \
    ln -fs /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && \
    dpkg-reconfigure -f noninteractive tzdata && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# 必要なパッケージのインストール(Python3.11, pip)
RUN add-apt-repository ppa:deadsnakes/ppa -y && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
        python3.11 \
        python3.11-distutils && \
    wget -qO /tmp/get-pip.py https://bootstrap.pypa.io/get-pip.py && \
    python3.11 /tmp/get-pip.py && \
    rm /tmp/get-pip.py && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# Pythonパッケージのインストール(onnxruntime-gpuとpiper-tts)
RUN pip install --no-cache-dir onnxruntime-gpu piper-tts

# 作業ディレクトリとデータディレクトリの作成
WORKDIR /app
RUN mkdir -p /data

# エントリーポイント設定:
# GPU(CUDA)を使用し、モデルの保存先を/dataに指定してpiperを起動
ENTRYPOINT ["piper", "--cuda", "--download-dir", "/data"]

以下でビルドをします

docker build -t piper-tts-gpu . 

実行コマンドは以下です

echo "Hello, this is a test." | docker run --rm -i --gpus all -v ${PWD}/data:/data piper-tts-gpu:latest --model /data/en_US-lessac-medium.onnx --config /data/en_US-lessac-medium.onnx.json --output_file /data/test.wav