只能在 Google Shell 中使用路径设置命令“exports...”。不要在代码中和我的主平台 Python Local 上使用“PATH”在 shell 中工作
在一个涉及 Google 语音转文本 API(特别是较新的 v2 版本)的项目中,我面临着从 PHP 过渡到 Python 的令人困惑的问题。我之前使用 API 版本 1 在 PHP 中成功实现了一个项目。然而,在迁移到 Python 以更好地与 v2 API 和 Google 文档保持一致时,我遇到了权限问题。
-核心问题:
在 PHP 中,一切都顺利进行,但在 Python 中,我遇到了与peer.recognizers.recognize 权限相关的 PermissionDenied 错误。这很令人困惑,因为相同的项目和服务帐户在 PHP 中运行良好。
-代码片段(Python):
import os
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech
credential_path = "C:\\Users\\Joffrey-PTMG\\Desktop\\U23\\OFC-SUBM2.0\\authgooglejson\\hidekey.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
def transcribe_file_v2(
project_id: str,
audio_file: str,
) -> cloud_speech.RecognizeResponse:
print(project_id)
# Instantiates a client
client = SpeechClient()
# Reads a file as bytes
with open(audio_file, "rb") as f:
content = f.read()
config = cloud_speech.RecognitionConfig(
auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
language_codes=["fr-FR"],
model="long",
)
request = cloud_speech.RecognizeRequest(
recognizer=f"projects/{project_id}/locations/global/recognizers/_",
config=config,
content=content,
)
# Transcribes the audio into text
response = client.recognize(request=request)
for result in response.results:
print(f"Transcript: {result.alternatives[0].transcript}")
return response
project_idvar = 'eighth-server-267708'
audio_filevar = 'testtt.mp3'
transcribe_file_v2(project_idvar, audio_filevar)
-Google Cloud Shell 错误:
Welcome to Cloud Shell! Type "help" to get started.
Your Cloud Platform project in this session is set to eighth-server-267708.
Use “gcloud config set project [PROJECT_ID]” to change to a different project.
vedlem_mail@cloudshell:~ (eighth-server-267708)$ python projshell11.py
eighth-server-267708
Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/google/api_core/grpc_helpers.py", line 75, in error_remapped_callable
return callable_(*args, **kwargs)
File "/usr/local/lib/python3.9/dist-packages/grpc/_channel.py", line 1161, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/usr/local/lib/python3.9/dist-packages/grpc/_channel.py", line 1004, in _end_unary_response_blocking
raise _InactiveRpcError(state) # pytype: disable=not-instantiable
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.PERMISSION_DENIED
details = "Permission 'speech.recognizers.recognize' denied on resource (or it may not exist)."
debug_error_string = "UNKNOWN:Error received from peer ipv4:74.125.140.95:443 {grpc_message:"Permission \'speech.recognizers.recognize\' denied on resource (or it may not exist).", grpc_status:7, created_time:"2023-12-04T07:29:41.163760308+00:00"}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/vedlem_mail/projshell11.py", line 44, in <module>
transcribe_file_v2(project_idvar, audio_filevar)
File "/home/vedlem_mail/projshell11.py", line 34, in transcribe_file_v2
response = client.recognize(request=request)
File "/home/vedlem_mail/.local/lib/python3.9/site-packages/google/cloud/speech_v2/services/speech/client.py", line 1482, in recognize
response = rpc(
File "/usr/local/lib/python3.9/dist-packages/google/api_core/gapic_v1/method.py", line 131, in __call__
return wrapped_func(*args, **kwargs)
File "/usr/local/lib/python3.9/dist-packages/google/api_core/grpc_helpers.py", line 77, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.PermissionDenied: 403 Permission 'speech.recognizers.recognize' denied on resource (or it may not exist). [reason: "IAM_PERMISSION_DENIED"
domain: "iam.googleapis.com"
metadata {
key: "permission"
value: "speech.recognizers.recognize"
}
]
vedlem_mail@cloudshell:~ (eighth-server-267708)$
-本地Python错误:
C:\Users\Joffrey-PTMG\Desktop\U23\OFC-SUBM2.0>python proj1.py
eighth-server-267708
Traceback (most recent call last):
File "C:\pythonCore\Lib\site-packages\google\api_core\grpc_helpers.py", line 75, in error_remapped_callable
return callable_(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\pythonCore\Lib\site-packages\grpc\_channel.py", line 1161, in __call__
return _end_unary_response_blocking(state, call, False, None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\pythonCore\Lib\site-packages\grpc\_channel.py", line 1004, in _end_unary_response_blocking
raise _InactiveRpcError(state) # pytype: disable=not-instantiable
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.PERMISSION_DENIED
details = "Permission 'speech.recognizers.recognize' denied on resource (or it may not exist)."
debug_error_string = "UNKNOWN:Error received from peer ipv6:%5B2a00:1450:4007:81a::200a%5D:443 {grpc_message:"Permission \'speech.recognizers.recognize\' denied on resource (or it may not exist).", grpc_status:7, created_time:"2023-12-04T07:30:15.8905988+00:00"}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Joffrey-PTMG\Desktop\U23\OFC-SUBM2.0\proj1.py", line 44, in <module>
transcribe_file_v2(project_idvar, audio_filevar)
File "C:\Users\Joffrey-PTMG\Desktop\U23\OFC-SUBM2.0\proj1.py", line 34, in transcribe_file_v2
response = client.recognize(request=request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\pythonCore\Lib\site-packages\google\cloud\speech_v2\services\speech\client.py", line 1482, in recognize
response = rpc(
^^^^
File "C:\pythonCore\Lib\site-packages\google\api_core\gapic_v1\method.py", line 131, in __call__
return wrapped_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\pythonCore\Lib\site-packages\google\api_core\grpc_helpers.py", line 77, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.PermissionDenied: 403 Permission 'speech.recognizers.recognize' denied on resource (or it may not exist). [reason: "IAM_PERMISSION_DENIED"
domain: "iam.googleapis.com"
metadata {
key: "permission"
value: "speech.recognizers.recognize"
}
]
我一直在尝试直接通过 Google Cloud Shell 运行我的 Python 脚本。有趣的是,环境变量 PATH 的工作方式并不像我的 PHP 项目那样无缝。因此,我在 Python 脚本中添加了以下几行来设置凭据路径:
credential_path = "C:\\Users\\Joffrey-PTMG\\Desktop\\U23\\OFC-SUBM2.0\\authgooglejson\\hidekey.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
这是为了让它工作,但即使删除这些行并尝试使用以下方法通过 Cloud Shell 设置环境变量:
export GOOGLE_APPLICATION_CREDENTIALS='/home/vedlem_mail/jsonss/hidekey.json'
我遇到了同样的错误。所以在我看来,这些行并不是问题的根源,但我们现在不知道为什么..
没有定义路径的行但执行了导出行的版本仅适用于 google shell(可能要感谢 shell 的重新启动)
我的目标是简单地在 .mp3 文件上使用语音转文本。我正在尝试克服看似虚假的错误,因为我在角色设置中被指定为“所有者”。这个设置在 PHP 中完美运行,所以我不明白为什么它在 Python 中不起作用。
帮助我🙏😢谢谢
我遇到了同样的问题,发现语音 v2 需要服务代理。 https://cloud.google.com/iam/docs/service-agents 搜索语音,然后使用 GCP 命令创建它。
gcloud beta services identity create --service=speech.googleapis.com --project=<Your project name here>
project_number=$(gcloud projects list --filter=<Your project name here> --format="value(PROJECT_NUMBER)")
gcloud projects add-iam-policy-binding neos-stt-0 --member serviceAccount:service-${project_number?}@gcp-sa-speech.iam.gserviceaccount.com --role roles/speech.serviceAgent