Google Cloud SDKを使用してText-To-Speech APIを呼びだすスクリプトを書いたのですが、以前のサーバー環境で動いていたものを移植したところエラーを吐かれるようになってしまいました
AttributeError: module 'google.cloud.texttospeech' has no attribute 'SsmlVoiceGender'
ソース
py
1from os import name 2import os 3import sys 4from discord.enums import SpeakingState 5sys.path.append('/root/google-cloud-sdk/bin/') 6import discord 7import info 8import func 9import re 10import random 11import datetime 12import html 13import time 14from discord.channel import VoiceChannel 15from discord.player import FFmpegPCMAudio 16from google.cloud import texttospeech 17from collections import defaultdict, deque 18 19... 20 21voiceChannel: VoiceChannel 22speechChannel: discord.channel 23connected: bool 24spQueue: str 25namemode: bool 26isalnum: bool 27queue_dict = defaultdict(deque) 28 29... 30 31 elif connected and message.channel == speechChannel: 32 if message.author.bot: 33 return 34 else: 35 if namemode==0: 36 make_voice(voiceChannel, message.content, '', message.guild) 37 elif namemode==1: 38 if message.author.nick: 39 nick = message.author.nick 40 else: 41 nick = message.author.name 42 make_voice(voiceChannel, message.content, nick, message.guild) 43 elif namemode==2: 44 make_voice(voiceChannel, message.content, message.author.name, message.guild) 45 46def text_to_ssml(text, author): 47 global isalnum 48 escaped_lines = html.escape(text) 49 spacing = " " 50 hyperlink = " Insersion of Hyper link " 51 isalnum = True 52 p = re.compile('[a-zA-Z0-9!?. ]+') 53 if escaped_lines == "": 54 spacing="" 55 elif not p.fullmatch(escaped_lines): 56 spacing = "。" 57 hyperlink = "ハイパーリンク" 58 isalnum = False 59 ssml = "{}".format( 60 re.sub(' ',spacing,(re.sub('[0-9]{14,}','',(re.sub('http[a-zA-Z_0-9./:?]{4,}',hyperlink,escaped_lines))))).replace('<:','').replace(':>','')+spacing+author 61 ) 62 return ssml 63 64def ssml_to_speech(ssml, file, language_code, gender): 65 ttsClient = texttospeech.TextToSpeechClient() 66 synthesis_input = texttospeech.SynthesisInput(text=ssml) 67 voice = texttospeech.VoiceSelectionParams( 68 language_code=language_code, ssml_gender=gender 69 ) 70 audio_config = texttospeech.AudioConfig( 71 audio_encoding=texttospeech.AudioEncoding.MP3 72 ) 73 response = ttsClient.synthesize_speech( 74 input=synthesis_input, voice=voice, audio_config=audio_config 75 ) 76 with open(file,"wb") as out: 77 out.write(response.audio_content) 78 return file 79 80def make_voice(voiceClient, text, author, guild): 81 ssml = text_to_ssml(text, author) 82 filename = time.time() 83 if isalnum: 84 file = ssml_to_speech(ssml, f"{filename}.mp3", "en-US", texttospeech.SsmlVoiceGender.FEMALE) 85 print("Text Chat Reading in en-US ->"+ssml) 86 else: 87 file = ssml_to_speech(ssml, f"{filename}.mp3", "ja-JP", texttospeech.SsmlVoiceGender.FEMALE) 88 print("Text Chat Reading in ja-JP ->"+ssml) 89 enqueue(voiceClient, guild, file) 90 91def enqueue(voiceClient, guild, source): 92 queue = queue_dict[guild.id] 93 queue.append(source) 94 if not voiceClient.is_playing(): 95 play(voiceClient, queue) 96 97def play(voiceClient, queue): 98 if not queue or voiceClient.is_playing(): 99 return 100 source = queue.popleft() 101 voiceClient.play(FFmpegPCMAudio(source, options="-loglevel panic"), after=lambda e:play(voiceClient, queue)) 102 time.sleep(1) 103 os.remove(source)
今までのサーバー上では正常に動作していたので多分ライブラリのインストール状況が原因だとは思うのですが、私では切り分けや特定ができないので質問させていただきました。
サーバー環境は
旧 | 新 | |
---|---|---|
OS | Ubuntu 20.04.3 LTS : GNU/Linux 5.11.0-43-generic x86_64 | Ubuntu 20.04.3 LTS : GNU/Linux 5.8.0-49-generic x86_64 |
仮想CPU | 6コア | 2コア |
仮想メモリ | 6GB | 2GB |
ストレージ | 50GB SSD | 25GB SSD |
Python バージョン | Python 3.8.10 | Python 3.8.10 |
gcloud バージョン | 367.0.0 | 367.0.0 |
回答1件
あなたの回答
tips
プレビュー