繁体   English   中英

谷歌云文本语音仅适用于列表中的第一个字符串

[英]Google cloud texttospeech only works on first string in a list

字符串大小的限制似乎是 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

一些字符串具有字符\\f 这些字符串在列表中碰巧是奇数编号的事实只是巧合。 通过删除\\f字符,我解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM