问题描述
我不知道音频文件的语言。因此,我需要为IdentifyLanguage
使用start_transcription_job()
。
LanguageCode
将为空白,因为我不知道音频文件的语言。
环境
使用
Python 3.8运行时,
boto3版本1.16.5
,
botocore版本:1.19.5
,
没有Lambda层。
这是我进行转录工作的代码:
mediaFileUri = 's3://'+ bucket_name+'/'+prefixKey
transcribe_client = boto3.client('transcribe')
response = transcribe_client.start_transcription_job(
TranscriptionJobName="abc",IdentifyLanguage=True,Media={
'MediaFileUri':mediaFileUri
},)
然后我收到此错误:
{
"errorMessage": "Parameter validation Failed:\nMissing required parameter in input: \"LanguageCode\"\nUnkNown parameter in input: \"IdentifyLanguage\",must be one of: TranscriptionJobName,LanguageCode,MediaSampleRateHertz,MediaFormat,Media,OutputBucketName,OutputEncryptionKMSKeyId,Settings,ModelSettings,JobExecutionSettings,ContentRedaction","errorType": "ParamValidationError","stackTrace": [
" File \"/var/task/app.py\",line 27,in TranscribeSoundToWordHandler\n response = response = transcribe_client.start_transcription_job(\n"," File \"/var/runtime/botocore/client.py\",line 316,in _api_call\n return self._make_api_call(operation_name,kwargs)\n",line 607,in _make_api_call\n request_dict = self._convert_to_request_dict(\n",line 655,in _convert_to_request_dict\n request_dict = self._serializer.serialize_to_request(\n"," File \"/var/runtime/botocore/validate.py\",line 297,in serialize_to_request\n raise ParamValidationError(report=report.generate_report())\n"
]
}
出现此错误,意味着我必须指定LanguageCode
,而IdentifyLanguage
是无效的参数。
100%确保S3中存在音频文件。但是如果没有LanguageCode
,它将无法正常工作,并且IdentifyLanguage
参数是未知参数
我使用SAM应用程序通过以下命令在本地进行测试:
sam local invoke MyHandler -e lambda\TheDirectory\event.json
然后我cdk deploy
,并同时签入Aws Lambda Console,对其进行了相同的测试events.json
,但仍然收到相同的错误
我认为这是Lambda执行环境,我没有使用任何Lambda层。
我看过Aws Transcribe的这份文档:
https://docs.aws.amazon.com/transcribe/latest/dg/API_StartTranscriptionJob.html
和boto3
的这份文档:
明确声明不需要LanguageCode
,并且IdentifyLanguage
是有效参数。
那我错过了什么?有什么想法吗?我该怎么办?
更新:
我一直在搜索并在线问夫妻,我认为我应该构建功能容器1st,以便SAM将boto3打包到容器中。
cdk synth --no-staging > template.yaml
然后:
sam build --use-container
sam local invoke MyHandler78A95900 -e lambda\TheDirectory\event.json
但是,我仍然遇到相同的错误,但是还要发布堆栈跟踪
[ERROR] ParamValidationError: Parameter validation Failed:
Missing required parameter in input: "LanguageCode"
UnkNown parameter in input: "IdentifyLanguage",ContentRedaction
Traceback (most recent call last):
File "/var/task/app.py",in TranscribeSoundToWordHandler
response = response = transcribe_client.start_transcription_job(
File "/var/runtime/botocore/client.py",in _api_call
return self._make_api_call(operation_name,kwargs)
File "/var/runtime/botocore/client.py",in _make_api_call
request_dict = self._convert_to_request_dict(
File "/var/runtime/botocore/client.py",in _convert_to_request_dict
request_dict = self._serializer.serialize_to_request(
File "/var/runtime/botocore/validate.py",in serialize_to_request
raise ParamValidationError(report=report.generate_report())
真的不知道我在这里做错了什么。我也报告了一个github issue here,但似乎无法重现该问题。
主要问题/问题:
无法start_transription_job
-
没有
LanguageCode
-
带有
IdentifyLanguage=True
可能的原因是什么,如何解决此问题(不知道音频文件的语言,我想在没有给出LanguageCode的情况下识别音频文件的语言)?
解决方法
检查您是否正在使用最新的boto3版本。
boto3.__version__
'1.16.5'
我尝试过了,而且行得通。
import boto3
transcribe = boto3.client('transcribe')
response = transcribe.start_transcription_job(TranscriptionJobName='Test-20201-27',IdentifyLanguage=True,Media={'MediaFileUri':'s3://BucketName/DemoData/Object.mp4'})
print(response)
{
"TranscriptionJob": {
"TranscriptionJobName": "Test-20201-27","TranscriptionJobStatus": "IN_PROGRESS","Media": {
"MediaFileUri": "s3://BucketName/DemoData/Object.mp4"
},"StartTime": "datetime.datetime(2020,10,27,15,41,2,599000,tzinfo=tzlocal())","CreationTime": "datetime.datetime(2020,565000,"IdentifyLanguage": "True"
},"ResponseMetadata": {
"RequestId": "9e4f94a4-20e4-4ca0-9c6e-e21a8934084b","HTTPStatusCode": 200,"HTTPHeaders": {
"content-type": "application/x-amz-json-1.1","date": "Tue,27 Oct 2020 14:41:02 GMT","x-amzn-requestid": "9e4f94a4-20e4-4ca0-9c6e-e21a8934084b","content-length": "268","connection": "keep-alive"
},"RetryAttempts": 0
}
}
,
最后,我注意到这是因为打包的lambda函数由于某种原因未上传。在几个人的帮助下,这是我解决的方法。
首先修改CDK堆栈,以定义我的lambda函数,如下所示:
from aws_cdk import (
aws_lambda as lambda_,core
)
from aws_cdk.aws_lambda_python import PythonFunction
class MyCdkStack(core.Stack):
def __init__(self,scope: core.Construct,id: str,**kwargs) -> None:
super().__init__(scope,id,**kwargs)
# define lambda
my_lambda = PythonFunction(
self,'MyHandler',entry='lambda/MyHandler',index='app.py',runtime=lambda_.Runtime.PYTHON_3_8,handler='MyHandler',timeout=core.Duration.seconds(10)
)
这将使用aws-lambda-python module,它将处理将所有必需的模块安装到docker中的过程。
接下来,cdk合成一个模板文件
cdk synth --no-staging > template.yaml
这时,它将捆绑entry
路径中PythonFunction
路径内的所有内容,并在该requirements.txt
路径内安装entry
中定义的所有必要依赖项。
下一步,构建Docker容器
$ sam build --use-container
确保template.yaml
文件位于根目录中。这将构建一个docker容器,并且工件将构建在我的根目录的.aws-sam/build
目录内。
最后一步,使用sam调用函数:
sam local invoke MyHandler78A95900 -e path\to\event.json
现在终于如我上面的问题所述,成功调用了start_transcription_job
,没有任何错误。
结论:
- 一开始我只
pip install boto3
,这只会 在我的本地系统中安装boto3
。 - 然后,我
sam local invoke
没有sam build --use-container
首先构建容器 - 最后,我最后有
sam build
,但在那一点上,我没有 将requirements.txt
中定义的内容捆绑到.aws-sam/build
,因此需要如上所述使用aws-lambda-python module。