python - 使用 Google Cloud SDK 在本地运行的 Google Speech-to-Text JupyterLab 笔记本脚本
问题描述
我有以下 Python 脚本,可以在 Google JupyterLab 笔记本上正常运行,但不能在本地使用 Google Cloud SDK:
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、Python 3.8)时,我收到以下错误消息:
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
我究竟做错了什么?
解决方案
这解决了我的问题,非常感谢。这是现在正在运行的代码:
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')
推荐阅读
- c - 如何将多个 .txt 文件读入单个缓冲区?
- apache-kafka - 开发群聊应用程序
- react-native - Xcode 无法加载弹出和发布的 expo 应用程序
- sql - 这是查看 SQL 准备语句的正确方法吗?
- c# - SerializedField 中的 GameObject - OverRide - 全部应用:Prefab 不更新
- javascript - 但是,尝试响应式更新图表数据时,图表并未反映更改。Vue.js
- java - 运行时异常 LibGDX 错误,但为什么它抱怨 TextureRegion?
- php - PHP SESSION 项目不跨页携带
- uitextfield - 在保持缩进的同时难以移除 UITextView 边框
- vba - “emacsclientw -c” 作为 Outlook 宏运行速度非常慢,而在 Window Powershell 中运行速度非常快