亚马逊AWS官方博客

利用 Amazon Bedrock Claude 3 生成视频内容摘要

前言

随着人工智能技术的不断发展,大语言模型(Large Language Model)持续热度不减。层出不穷的高智能/性能模型,为各领域的创新应用带来了新的助力。LLM 的快速发展,也给音视频领域带来了很多新的机遇,“视频摘要”就是其中比较突出的需求。“视频摘要”利用 LLM 的理解能力,对视频内容进行文字的摘要和描述,通常用于视频会议摘要,视频培训摘要等等。比如,目前 Bilibili 即推出了“AI 视频总结”功能,目前处于公测阶段:

本文将以新闻视频为例,介绍一种利用 Claude3 自动化生成新闻内容摘要的方法。

Bedrock & Transcribe 简介

Amazon Bedrock 是亚马逊云科技在 2023 年推出的基础模型托管服务,您可以使用 Amazon Bedrock,通过单个 API 提供来自 AI21 Labs、Anthropic、Cohere、Meta、Stability AI 和 Amazon 等领先人工智能公司的高性能基础模型。以及通过安全性、隐私性和负责任的 AI 构建生成式人工智能应用程序所需的一系列广泛功能。使用 Amazon Bedrock,您可以轻松试验和评估适合您的使用案例的热门 FM,通过微调和检索增强生成(RAG)等技术利用您的数据对其进行私人定制,并构建使用您的企业系统和数据来源执行任务的代理。

Claude 模型由 Anthropic 公司推出,目前已迭代至 3.0 版本,分为 Haiku,Sonnet,Opus 三个版本。它们的定位以及使用场景请见下图:

其主要特点,您可以参考 Anthropic 官网的相关介绍:https://www.anthropic.com/claude。目前您已经可以在 Amazon Bedrock 服务上直接以 API 的形式调用 Claude3。

Amazon Transcribe 是一项人工智能服务,使您能够轻松实现语音转文本功能。采用自动语音识别(ASR)技术,您可以将 Amazon Transcribe 用于各种业务应用程序,包括转录基于语音的客户服务通话、生成音频/视频内容字幕,以及对音频/视频内容执行(基于文本的)内容分析。

方案设计

在本次 Demo 中,我们希望对上传至 S3 存储桶的英文新闻视频,自动生成文字的新闻摘要/新闻稿。该需求可以考虑从如下几个方向实现:

  • 利用 Amazon Media Convert 对原始视频文件进行抽帧,直接调用 Claude3 对图像进行摘要输出。但该方案可能会产生如下问题:
    • 图像帧由于每帧之间有时间间隔,产生的图像帧组合会造成新闻内容的缺失,导致 Claude3 对新闻内容理解缺失,致使生成的摘要内容缺失或不准确;
    • 视频帧输入到 Claude3 会占用较多输入 token,生产环境下会造成较大成本。Claude3 对于图像 Token 是按照图片的尺寸决定的,具体的计算公式为:tokens = (width px * height px)/750。更多详细内容,请参考 Claude 官方文档:https://docs.anthropic.com/claude/docs/vision
  • 利用 Amazon Media Convert 对原始视频文件进行字幕抽取,然后将字幕作为提示词输入给 Claude3 进行字幕内容摘要。但由于视频来源的多样化,无法确保所有原始视频都包含字幕,所以该方案不具备普适性;
  • 本文中方案采用 Amazon Transcribe 首先对原始视频生成字幕文件,然后将字幕内容作为提示词输入给 Claude3 进行字幕内容摘要。利用 Lambda 将整个工作流自动化,实现文件上传后,直接可获取摘要文档。

工作流如下:

  • 视频文件上传到 S3 桶,触发 create-transcribe-job lambda 函数;
  • create-transcribe-job lambda 函数调用 transcribe,开启转录任务。转录后的字幕文件,上传到 S3;
  • 字幕文件上传后,自动触发 create-bedrock-job lambda 函数,将字幕文件内容作为提示词,输出给 Bedrock Claude3;
  • Claude3 将字幕内容进行摘要,生成新闻稿并将文件上传到 S3。

环境准备

本 Demo 采用的模型为 Claude3 Sonnet,Region 为 us-east-1,Claude3 模型权限开通请参考 Bedrock 用户指南:https://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-access.html

上传视频素材

在终端工具上安装最新版本 cli,配置相关凭证。安装与配置文档文档请见:https://docs.aws.amazon.com/zh_cn/cli/latest/userguide/getting-started-install.html

创建 s3 bucket,用于存储视频新闻和 Transcribe 解析的字幕文件:

s3 mb s3://news-summary-demo

上传原始视频文件到 news-summary-demo 存储桶中的 video-input 路径下:

aws s3 cp videoplayback.mp4 s3://news-summary-demo/video-input/videoplayback.mp4

新增 IAM Role

本 Demo 后续会创建两个 Lambda 函数,create-transcribe-job 用于创建 transcribe 任务,create-bedrock-job 用于调用 bedrock 的 claude3 基础模型。我们可以提前创建好 Lambda 函数后续所需要的 IAM Role。

创建 create-transcribe-job IAM Role

#创建 IAM Policy:
aws iam create-policy \
  --policy-name create-transcribe-job-policy \
  --policy-document '{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::news-summary-demo",
                "arn:aws:s3:::news-summary-demo/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": "transcribe:*",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": "logs:CreateLogGroup",
            "Resource": "arn:aws:logs:us-east-1:<your account id>:*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": [
                "arn:aws:logs:us-east-1:<your account id>:log-group:/aws/lambda/create-transcribe-job:*"
            ]
        }
        
    ]
}'

#创建 IAM Role 并关联上述策略:
aws iam create-role \
  --role-name create-transcribe-job \
  --assume-role-policy-document '{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "lambda.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}'

aws iam attach-role-policy \
  --role-name create-transcribe-job \
  --policy-arn arn:aws:iam::<your account id>:policy/create-transcribe-job-policy

创建 create-bedrock-job IAM Role

#创建 IAM Policy:
aws iam create-policy \
  --policy-name create-bedrock-job-policy \
  --policy-document '{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::news-summary-demo",
                "arn:aws:s3:::news-summary-demo/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": "bedrock:*",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": "logs:CreateLogGroup",
            "Resource": "arn:aws:logs:us-east-1:<your account id>:*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": [
                "arn:aws:logs:us-east-1:<your account id>:log-group:/aws/lambda/create-bedrock-job:*"
            ]
        }
    ]
}'

#创建 IAM Role 并关联上述策略:
aws iam create-role \
  --role-name create-bedrock-job \
  --assume-role-policy-document '{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "lambda.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}'

aws iam attach-role-policy \
  --role-name create-bedrock-job \
  --policy-arn arn:aws:iam::<your account id>:policy/create-bedrock-job-policy

创建 Lambda 函数

创建 create-transcribe-job 函数,zip 包请在此下载:https://d38s7u1hm58wso.cloudfront.net/create-transcribe-job.zip

#创建 create-transcribe-job 函数
aws lambda create-function \
  --function-name create-transcribe-job \
  --runtime python3.9 \
  --role arn:aws:iam::<your account id>:role/create-transcribe-job \
  --timeout 900 \
  --handler lambda_function.lambda_handler \
  --publish \
  --zip-file fileb://create-transcribe-job.zip

创建 create-bedrock-job 函数,zip 包请在此下载:https://d38s7u1hm58wso.cloudfront.net/create-bedrock-job.zip

#创建 create-bedrock-job 函数
aws lambda create-function \
  --function-name create-bedrock-job \
  --runtime python3.9 \
  --role arn:aws:iam::<your account id>:role/create-bedrock-job \
  --timeout 900 \
  --handler lambda_function.lambda_handler \
  --publish \
  --zip-file fileb://create-bedrock-job.zip

创建 S3 Event Trigger,用于触发以上两个 Lambda 函数:

#添加 S3 Invoke Lambda 权限:
aws lambda add-permission \
    --function-name create-transcribe-job \
    --statement-id s3-event-permission-1 \
    --action "lambda:InvokeFunction" \
    --principal s3.amazonaws.com \
    --source-arn arn:aws:s3:::news-summary-demo \
    --source-account <your account id>
    
aws lambda add-permission \
    --function-name create-bedrock-job \
    --statement-id s3-event-permission-2 \
    --action "lambda:InvokeFunction" \
    --principal s3.amazonaws.com \
    --source-arn arn:aws:s3:::news-summary-demo \
    --source-account <your account id>

#添加 S3 Event Trigger:
aws s3api put-bucket-notification-configuration \
    --bucket news-summary-demo \
    --notification-configuration '{"LambdaFunctionConfigurations":[
        {"LambdaFunctionArn":"arn:aws:lambda:us-east-1:<your account id>:function:create-transcribe-job","Events":["s3:ObjectCreated:*"],"Filter":{"Key":{"FilterRules":[{"Name":"prefix","Value":"video-input/"}]}}},
        {"LambdaFunctionArn":"arn:aws:lambda:us-east-1:<your account id>:function:create-bedrock-job","Events":["s3:ObjectCreated:*"],"Filter":{"Key":{"FilterRules":[{"Name":"prefix","Value":"subtitle-output/"},{"Name":"suffix","Value":".srt"}]}}}
    ]}'

此处 Claude3 的提示词如下:

The following content is taken from the subtitle file of a video. Please help me generate press release based on the content of these subtitles, with the number of words within 100 words. Please output the press release directly without including other content: \n\n{srt_content}

效果展示

视频文件上传至 S3 桶 news-summary-demo 下的 video-input 路径后,一般 2 分钟以内(视视频时长而定)即可在 summary output 路径下生成对应的文字新闻稿。本视频生成的 100 字新闻稿摘要如下:

On International Women’s Day, a group of resilient women in Maui are spearheading recovery efforts and advocating for the island’s cultural preservation after the devastating wildfires. These women are raising awareness about potential environmental hazards posed by a temporary debris dump site, while the Lahaina Land Trust, created by local women, aims to protect the island’s essence by enabling generational families to retain their land through leasing arrangements, preventing outside developers from exploiting the post-fire situation. Their efforts exemplify the strength and commitment of Maui’s women in safeguarding their community’s well-being and cherished heritage.

参考链接

视频来源:https://www.youtube.com/watch?v=1KyzjZwIFU4

本篇作者

孟祥智

亚马逊云科技解决方案架构师