首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Google Cloud SDK在本地运行的Google Speech-to-Text JupyterLab笔记本脚本

使用Google Cloud SDK在本地运行的Google Speech-to-Text JupyterLab笔记本脚本
EN

Stack Overflow用户
提问于 2020-10-10 05:01:34
回答 2查看 377关注 0票数 0

我有以下Python脚本,它在谷歌JupyterLab笔记本上运行良好,但不能在本地使用Google Cloud SDK:

代码语言:javascript
复制
from google.cloud import speech_v1p1beta1

def speech_to_text(audio_file):

    client = speech_v1p1beta1.SpeechClient()

    enable_word_time_offsets = True
    enable_word_confidence = True
    enable_automatic_punctuation = True

    language_code = 'en-US'
    config = {
        'enable_word_confidence': enable_word_confidence,
        'enable_word_time_offsets': enable_word_time_offsets,
        'enable_automatic_punctuation': enable_automatic_punctuation,
        'language_code': language_code
    }
    audio = {'uri': audio_file}
    operation = client.long_running_recognize (config, audio)
    response = client.recognize(config, audio)
    result = response.results[0]
    alternative = result.alternatives[0]

    print(alternative)
    
speech_to_text('gs://my-bucket/my-folder/my-subfolder/my-audio-file.flac')

但是,当我尝试使用Google Cloud SDK在虚拟环境中本地运行这个脚本(WIN10,Python3.8)时,我得到了以下错误消息:

代码语言:javascript
复制
Traceback (most recent call last):
  File "my-speech-to-text-script.py", line 32, in <module>
    speech_to_text('gs://my-bucket/my-folder/my-subfolder/my-audio-file.flac')
  File "my-speech-to-text-script.py", line 25, in speech_to_text
    operation = client.long_running_recognize (config, audio)
TypeError: long_running_recognize() takes from 1 to 2 positional arguments but 3 were given

我按照本教程设置了虚拟环境https://cloud.google.com/python/setup#windows,然后运行了pip install google-cloud-speech

我做错了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-12 18:52:16

我通过更新我的代码弄清楚了这一点,我的代码和您的代码一样,可能是基于语音到文本库的较旧版本。

重要的变化:

代码语言:javascript
复制
operation = client.long_running_recognize(request={"config":config, "audio":audio})
票数 1
EN

Stack Overflow用户

发布于 2020-10-12 23:06:40

这解决了我的问题,非常感谢。下面是正在运行的代码:

代码语言:javascript
复制
from google.cloud import speech_v1p1beta1

def speech_to_text(audio_file):

    client = speech_v1p1beta1.SpeechClient()

    enable_word_time_offsets = True
    enable_word_confidence = True
    enable_automatic_punctuation = True

    language_code = "en-US"
    config = {
        "enable_word_confidence": enable_word_confidence,
        "enable_word_time_offsets": enable_word_time_offsets,
        "enable_automatic_punctuation": enable_automatic_punctuation,
        "language_code": language_code
    }
    audio = {"uri": audio_file}
    operation = client.long_running_recognize(request={"config":config, "audio":audio})
    response = client.recognize(request={"config":config, "audio":audio})
    result = response.results[0]
    alternative = result.alternatives[0]

    print(alternative)
    
speech_to_text('gs://my-bucket/my-folder/my-subfolder/my-audio-file.flac')
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64287266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档