【发布时间】:2019-08-31 20:56:14
【问题描述】:
字符串大小的限制似乎是 5000 个字符。我知道这一点是因为我尝试了一个更大的字符串,但我收到了错误消息,说大小限制为 5000 个字符。当您尝试将整本书转换为音频时,这会使事情变得非常耗时。所以我把这本书分成了少于 5000 个字符集。列表中的第一个字符串有效,但第二个字符串无效。这是错误信息
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.
文本文件是取自网站的未完成书,因此共享它没有问题。文本文件可以在这里下载:
https://drive.google.com/file/d/1PWZYka1RbIb7eIHcGp7_03pEw72oTNoC/view?usp=sharing
这是我正在使用的代码:
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
【问题讨论】:
-
想知道第二个文本是否有问题或只是进行顺序调用。您可以尝试仅手动发送第二个文本,看看是否有问题?
-
我对此表示严重怀疑。第二个字符串与第一个字符串没有任何不同。此外,我意识到这很难相信,但它适用于列表中具有偶数索引的字符串,但一次运行只能工作不超过两次。因此,如果您从 4 开始,则字符串 4 和 6 将起作用,但如果您从 0 开始,则不会起作用。此外,我已经发布了整个代码和文本文件。而字符串 1、3 和 5 永远不起作用。
标签: python google-cloud-platform google-text-to-speech