Python Google cloud texttospeech仅适用于列表中的第一个字符串
字符串大小的限制似乎是5000个字符。我知道这一点,因为我尝试了一个更大的字符串,但我得到了错误消息,说大小限制是5000个字符。当你试图将整本书转换成音频时,这会让事情变得非常耗时。于是我把这本书分成了不到5000个字符集。列表中的第一个字符串有效,但第二个字符串无效。这是错误消息Python Google cloud texttospeech仅适用于列表中的第一个字符串,python,google-cloud-platform,google-text-to-speech,Python,Google Cloud Platform,Google Text To Speech,字符串大小的限制似乎是5000个字符。我知道这一点,因为我尝试了一个更大的字符串,但我得到了错误消息,说大小限制是5000个字符。当你试图将整本书转换成音频时,这会让事情变得非常耗时。于是我把这本书分成了不到5000个字符集。列表中的第一个字符串有效,但第二个字符串无效。这是错误消息 Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 57, in error_remapped_callable
return callable_(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/grpc/_channel.py", line 533, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/grpc/_channel.py", line 467, in _end_unary_response_blocking
raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.INVALID_ARGUMENT
details = "Request contains an invalid argument."
debug_error_string = "{"created":"@1567284127.093145000","description":"Error received from peer","file":"src/core/lib/surface/call.cc","file_line":1017,"grpc_message":"Request contains an invalid argument.","grpc_status":3}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1741, in <module>
main()
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1735, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1135, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/kylefoley/PycharmProjects/inference_engine2/inference2/proofs/hieroglyphs/begin.py", line 64, in <module>
add_english.prepare_txt2audio()
File "/Users/kylefoley/PycharmProjects/inference_engine2/inference2/proofs/hieroglyphs/add_english.py", line 271, in prepare_txt2audio
text2audio(client, str1, e)
File "/Users/kylefoley/PycharmProjects/inference_engine2/inference2/proofs/hieroglyphs/add_english.py", line 303, in text2audio
response = client.synthesize_speech(input_text, voice, audio_config)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/cloud/texttospeech_v1/gapic/text_to_speech_client.py", line 322, in synthesize_speech
request, retry=retry, timeout=timeout, metadata=metadata
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/api_core/gapic_v1/method.py", line 143, in __call__
return wrapped_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/api_core/retry.py", line 273, in retry_wrapped_func
on_error=on_error,
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/api_core/retry.py", line 182, in retry_target
return target()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/api_core/timeout.py", line 214, in func_with_timeout
return func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 2, in raise_from
google.api_core.exceptions.InvalidArgument: 400 Request contains an invalid argument.
回溯(最近一次呼叫最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/api\u core/grpc\u helpers.py”,第57行,错误为\u重新映射\u可调用
返回可调用函数(*args,**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/grpc/_channel.py”,第533行,在调用中__
返回\u结束\u一元\u响应\u阻塞(状态、调用、错误、无)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/grpc/_channel.py”,第467行,在一元响应中
提升集合点(状态,无,无,截止日期)
grpc._通道_会合点:
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev.py”,第1741行,在
main()
文件“/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev.py”,第1735行,主目录
globals=debugger.run(setup['file'],None,None,is_模块)
文件“/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev.py”,第1135行,运行中
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py”,execfile中第18行
exec(编译(内容+“\n”,文件,'exec'),全局,loc)
文件“/Users/kylefoley/PycharmProjects/inference_engine2/inference2/proof/higeroglyphs/begin.py”,第64行,在
添加英语。准备
文件“/Users/kylefoley/PycharmProjects/inference_engine2/inference2/proof/higeroglyphs/add_english.py”,第271行,在prepare_txt2audio中
text2audio(客户端、str1、e)
text2audio中的文件“/Users/kylefoley/PycharmProjects/inference_engine2/inference2/proof/higeroglyphs/add_english.py”,第303行
响应=客户端。合成语音(输入文本、语音、音频配置)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/cloud/texttospeech_v1/gapic/text_to_speech_client.py”,第322行,在syntheu speech中
请求,重试=重试,超时=超时,元数据=元数据
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/api_core/gapic_v1/method.py”,第143行,在调用中__
返回包装函数(*args,**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/api\u core/retry.py”,第273行,在retry\u wrapped\u func中
on_错误=on_错误,
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/api_core/retry.py”,第182行,在retry_目标中
返回目标()
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/api_-core/timeout.py”,第214行,在func_中,带有超时
返回函数(*args,**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/api\u core/grpc\u helpers.py”,第59行,错误为\u重新映射\u可调用
六、从(例外情况。从grpc错误(exc),exc)
文件“”,第2行,从
google.api_core.exceptions.InvalidArgument:400请求包含无效参数。
文本文件是从网站上获取的未完成的书籍,因此共享它没有问题。可在此处下载文本文件:
下面是我使用的代码:
en = enumerate
p = print
def prepare_txt2audio():
str1 = "my_id.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = str1
client = texttospeech.TextToSpeechClient()
text1 = from_txt2lst('atkins1.txt')
txt1 = ' '.join(text1)
b = len(txt1)
c = b // 3000
lsts = divide_lst(text1, c)
for e, x in enumerate(lsts):
y = ' '.join(x)
assert len(y) < 5000
for e, lst in en(lsts):
p (e)
str1 = ' '.join(lst)
text2audio(client, str1, e)
# time.sleep(10)
def text2audio(client, txt1, idx):
input_text = texttospeech.types.SynthesisInput(text=txt1)
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
name='en-US-Wavenet-C',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
response = client.synthesize_speech(input_text, voice, audio_config)
with open(f'atkins_book/atkins{idx}.mp3', 'wb') as out:
out.write(response.audio_content)
def from_txt2lst(file):
if not file.endswith('txt'):
file += '.txt'
try:
lst = [line[:-1] for line in open(file, 'r')]
except:
lst = [line[:-1] for line in open(file, 'r+', encoding="latin-1")]
return del_last_empty_rw(lst)
def del_last_empty_rw(lst):
while type(lst[-1] == str) and not reg(r'\S', lst[-1]):
del lst[-1]
return lst
def divide_lst(lst, divisions):
lst1 = []
for i in range(divisions):
start1, stop1 = divide_range(divisions, len(lst), i)
lst1.append(lst[start1:stop1])
return lst1
def divide_range(divisions: int, total: int, idx: int):
sec = total // divisions
start = idx * sec
if total % divisions != 0 and idx + 1 == divisions:
stop = total
else:
stop = start + sec
return start, stop
en=枚举
p=打印
def prepare_txt2audio():
str1=“my_id.json”
os.environ['GOOGLE\u应用程序\u凭据]]=str1
client=texttospeech.TextToSpeechClient()
text1=来自_txt2lst('atkins1.txt')
txt1=''.join(text1)
b=len(txt1)
c=b//3000
lsts=除法(text1,c)
对于枚举中的e,x(lsts):
y=''。连接(x)
断言长度(y)<5000
对于e,en中的lst(lst):
p(e)
str1=''.join(lst)
text2audio(客户端、str1、e)
#时间。睡眠(10)
def text2audio(客户端、txt1、idx):
input_text=texttospeech.types.SynthesisInput(text=txt1)
voice=texttospeech.types.voiceselection参数(
语言代码为'en-US',
name='en-US-Wavenet-C',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
audio_config=texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
响应=客户端。合成语音(输入文本、语音、音频配置)
打开(f'atkins_book/atkins{idx}.mp3,'wb')作为输出:
输出.写入(响应.音频内容)
来自_txt2lst的def(文件):
如果不是file.endswith('txt'):
文件+='.txt'
尝试:
lst=[line[:-1]表示打开的行(文件'r')]
除:
lst=[line[:-1]表示打开的行(文件'r+',encoding=“latin-1”)]
返回del_last_empty_rw(lst)
def del_last_empty_rw(lst):
而类型(lst[-1]==str)和非reg(r'\S',lst[-1]):
德尔一世[-1]
返回lst
def分区(lst,分区):
lst1=[]
对于范围内的i(分区):
start1,stop1=分割范围(分割,长度(lst),i)
lst1.append(lst[start1:stop1])
返回lst1
def divide_范围(分段:整数、总计:整数、idx:整数):
秒=总//分
开始=idx*秒
如果总分区数为%d!=0和idx+1==分区:
停止=总计
其他:
停止=开始+秒
返回开始,停止
某些字符串具有字符\f
。事实上,这些字符串恰好是列表中的奇数,这只是巧合。通过删除\f
字符,我解决了这个问题。想知道