en-core-web-sm 模块错误 - 无服务器部署 AWS Lambda

en-core-web-sm module error - Serverless deployment AWS Lambda

我在 Python AWS Lambda 中使用 SpaCy 的 en-core-web-sm。我 运行 pip freeze > requirements.txt 获取 requirements.txt 文件中的所有依赖项。 en-core-web-sm==2.1.0 是文件中的一行。

当我尝试进行无服务器部署时,我得到 ERROR: Could not find a version that satisfies the requirement en-core-web-sm==2.1.0 (from versions: none) ERROR: No matching distribution found for en-core-web-sm==2.1.0 .

尽管我没有使用 Heroku,但我遵循 Heroku Deployment Error: No matching distribution found for en-core-web-sm 并在我的 requirements.txt 文件中添加了行 https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.1.0/en_core_web_sm-2.1.0.tar.gz#egg=en_core_web_sm==2.1.0 只是为了得到 Unzipped size must be smaller than 262144000 bytes (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: XxX-XxX)

如何将 en-web-core-sm 连接到我的 Lambda?

利用模型作为库的单独组件的优势,将模型上传到 S3 存储桶中。在初始化 spaCy 之前,我从 S3 下载模型。这是通过以下方法完成的。

def download_dir(dist, local, bucket):
    client = get_boto3_client('s3', lambda n: boto3.client('s3'))
    resource = get_boto3_client('s3r', lambda n: boto3.resource('s3'))

    paginator = client.get_paginator('list_objects')
    for result in paginator.paginate(Bucket=bucket, Delimiter='/', Prefix=dist):
        if result.get('CommonPrefixes') is not None:
            for subdir in result.get('CommonPrefixes'):
                download_dir(subdir.get('Prefix'), local, bucket)
        if result.get('Contents') is not None:
            for file in result.get('Contents'):

                if not os.path.exists(os.path.dirname(local + os.sep + file.get('Key'))):
                    os.makedirs(os.path.dirname(local + os.sep + file.get('Key')))
                dest_path = local + os.sep + file.get('Key')

                if not dest_path.endswith('/'):
                    resource.meta.client.download_file(bucket, file.get('Key'), dest_path)

使用 spaCy 的代码如下所示:

import spacy
if not os.path.isdir(f'/tmp/en_core_web_sm-2.0.0'):
       download_dir(lang, '/tmp', mapping_bucket)
spacy.util.set_data_path('/tmp')

nlp = spacy.load(f'/tmp/en_core_web_sm-2.0.0')
doc = nlp(spacy_input)
for token in doc:
    print(token.text, token.pos_, token.label_)