113 views
【0】連載内容
(134)【Google Cloud TTS #1】子どもの英会話学習教材を作りたい!
(135)【Google Cloud TTS #2】Google Cloud側の準備作業
(136)【Google Cloud TTS #3】自前サーバー側の準備作業(Ubuntu24) ←今回はココ
(137)【Google Cloud TTS #4】WEBブラウザから実行
(138)【Google Cloud TTS #5】話す速度をゆっくりに
(139)【Google Cloud TTS #6】声の大きさ、声の高さを変える。
(140)【Google Cloud TTS #7】Webブラウザ上で話者(Voice)を指定可能に
(141)【Google Cloud TTS #8】二人以上の会話を入力可能に
(142)【Google Cloud TTS #9】英会話教材を作る。(一先ず完結)

【1】やりたいこと
前回の投稿 (135)【Google Cloud TTS #2】Google Cloud側の準備作業 で Google Cloud側の準備作業が完了している。
今回は、Webブラウザからのリクエストを受けて TTSリクエストを発行する Pythonスクリプトの実行環境を構築する。
【2】環境構築の方針
実行環境一式は、/opt/webtts に設置する。
これは、Ubuntu24上の HTTPサーバ(Apache2.4)の実行ユーザー(www-data)から Pythonプログラムにアクセスできるようにするためだ。
同時に、開発中は $USER でコマンドラインから実行可能であること。
【3】やってみる
(1) プロジェクトのディレクトリを作成
# プロジェクトルートの作成 sudo mkdir -p /opt/webtts # 秘密鍵ファイルの置き場所を作成(仮に...) sudo mkdir -p /opt/webtts/auth # Pythonプログラムと音声出力用のディレクトリ作成 sudo mkdir -p /opt/webtts/src sudo mkdir -p /opt/webtts/output
(2) Python仮想環境を作成
# プロジェクトルートに移動する。 cd /opt/webtts # Python-venvをインストールする。 sudo apt install python3-venv # 仮想環境を作成 sudo python3 -m venv myenv # 仮想環境内でライブラリをインストール sudo ./myenv/bin/pip install --upgrade google-cloud-texttospeech
(3) 秘密鍵の配置と権限設定
前回の投稿 (135)【Google Cloud TTS #2】Google Cloud側の準備作業 で作成した Google Cloud TTS APIを使うための秘密鍵ファイルを
/opt/webts/auth
に配置した後、以下のコマンドを実行する。
# 所有者を $USER、グループを www-data に変更 sudo chown -R $USER:www-data /opt/webtts # 秘密鍵の権限:グループ(www-data)のみ読み取り許可 (440) sudo chmod 440 /opt/webtts/auth/key.json # 出力先:www-data に書き込みを許可 (770) sudo chmod 770 /opt/webtts/output # ディレクトリに SGID を設定 (chmod の 2000 番台) # これにより、この中で作成された新しいファイルは常に www-data グループを継承する。 sudo find /opt/webtts -type d -exec chmod 2770 {} + # ファイルにはグループ書き込み権限を付与 sudo find /opt/webtts -type f -exec chmod 660 {} + # 仮想環境の実行ファイルには実行権限を付与 sudo chmod +x /opt/webtts/myenv/bin/python
【4】動作確認
前回の投稿で Google Cloud側の設定が出来た。
今回の投稿で Ubuntu24側の設定が出来た。
それでは、動作確認用プログラムを実行してみよう!
# まずは、【3】(2)で作成した Python仮想環境に入る。 source /opt/webtts/myenv/bin/activate # プログラムソースファイルを置くために作ったディレクトリに移動する。 cd /opt/webtts/src # Pythonを起動する。 python
Python Shellが起動したら、以下のプログラムを一行ずつ入力しながら動作確認してみる。
import os
import sys
from google.cloud import texttospeech
#-----------------------------------------------------------------------
# 鍵ファイルの絶対パスを指定
KEY_PATH = "/opt/webtts/auth/key.json"
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = KEY_PATH
#-----------------------------------------------------------------------
# 英文と 出力MPファイル名を指定
text = "Can you read this book for me?"
mp3file = "sound.mp3"
#-----------------------------------------------------------------------
# TTS実行
client = texttospeech.TextToSpeechClient()
synthesis_input = texttospeech.SynthesisInput(text=text)
voice = texttospeech.VoiceSelectionParams(language_code="en-US", name="en-US-Chirp3-HD-Rasalgethi")
audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)
response = client.synthesize_speech(input=synthesis_input, voice=voice, audio_config=audio_config)
#-----------------------------------------------------------------------
# TTSで生成された音声をMP3ファイルに出力
output_file = os.path.join("/opt/webtts/output", mp3file)
with open(output_file, "wb") as out:
out.write(response.audio_content)
成功!
音声ファイルが出力された。
Can you read this book for me?
と、かなりの早口で再生された。
子どもの学習用には速度調整が必要だな…
$ ls -l /opt/webtts/output/ total 8 -rw-rw-r-- 1 user www-data 5952 3月 25 12:33 sound.mp3