跳转到主要内容

用于Amazon Transcribe流式传输的异步Python SDK

项目描述

Amazon Transcribe Streaming SDK

Amazon Transcribe Streaming SDK允许用户直接通过Python程序与Amazon Transcribe Streaming服务接口。项目的目标是使用户能够直接与Amazon Transcribe集成,无需除音频字节流和基本处理程序之外的其他任何东西。

该项目仍处于早期alpha阶段,因此接口可能仍会发生变化,并且可能经历快速迭代。如果在使用此软件包时遇到问题,建议您在本地测试之外的地方使用时严格锁定依赖项。请注意,awscrt是与botocore(AWS CLI和boto3的核心模块)共享的依赖项。如果在同一环境中安装,您可能需要保持amazon-transcribe为最新版本。

安装

从pip安装

python -m pip install amazon-transcribe

从Github安装

git clone https://github.com/awslabs/amazon-transcribe-streaming-sdk.git
cd amazon-transcribe-streaming-sdk
python -m pip install .

要将其用于Python应用程序,请在您的requirements.txt文件中将amazon-transcribe添加为依赖项。

注意:此SDK是在AWS通用运行时(CRT)的基础上构建的,这是一个C库集合,我们通过绑定与之交互。CRT作为预编译的轮子可在PyPI(awscrt)上找到,适用于常见的平台(Linux、macOS、Windows)。对于非标准操作系统,可能需要自行编译这些库。

用法

先决条件

如果您还没有为您的AWS账户设置本地凭据,可以按照此指南使用AWS CLI进行配置。

本质上,您需要设置以下认证配置之一,以便SDK成功解析您的API密钥

  1. 设置环境变量AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY和可选的AWS_SESSION_TOKEN
  2. 设置AWS_PROFILE指向您的AWS配置文件目录
  3. ~/.aws/credentials中配置[default]配置文件

有关AWS共享配置文件和凭据提供程序的更多信息,请参阅以下开发者指南

快速入门

为此SDK的设置需要实时或预录制的音频。有关音频输入要求的完整信息,请参阅Amazon Transcribe Streaming文档

以下是一个示例应用程序,以开始使用

import asyncio

# This example uses aiofile for asynchronous file reads.
# It's not a dependency of the project but can be installed
# with `pip install aiofile`.
import aiofile

from amazon_transcribe.client import TranscribeStreamingClient
from amazon_transcribe.handlers import TranscriptResultStreamHandler
from amazon_transcribe.model import TranscriptEvent
from amazon_transcribe.utils import apply_realtime_delay

"""
Here's an example of a custom event handler you can extend to
process the returned transcription results as needed. This
handler will simply print the text out to your interpreter.
"""


SAMPLE_RATE = 16000
BYTES_PER_SAMPLE = 2
CHANNEL_NUMS = 1

# An example file can be found at tests/integration/assets/test.wav
AUDIO_PATH = "tests/integration/assets/test.wav"
CHUNK_SIZE = 1024 * 8
REGION = "us-west-2"


class MyEventHandler(TranscriptResultStreamHandler):
    async def handle_transcript_event(self, transcript_event: TranscriptEvent):
        # This handler can be implemented to handle transcriptions as needed.
        # Here's an example to get started.
        results = transcript_event.transcript.results
        for result in results:
            for alt in result.alternatives:
                print(alt.transcript)


async def basic_transcribe():
    # Setup up our client with our chosen AWS region
    client = TranscribeStreamingClient(region=REGION)

    # Start transcription to generate our async stream
    stream = await client.start_stream_transcription(
        language_code="en-US",
        media_sample_rate_hz=SAMPLE_RATE,
        media_encoding="pcm",
    )

    async def write_chunks():
        # NOTE: For pre-recorded files longer than 5 minutes, the sent audio
        # chunks should be rate limited to match the realtime bitrate of the
        # audio stream to avoid signing issues.
        async with aiofile.AIOFile(AUDIO_PATH, "rb") as afp:
            reader = aiofile.Reader(afp, chunk_size=CHUNK_SIZE)
            await apply_realtime_delay(
                stream, reader, BYTES_PER_SAMPLE, SAMPLE_RATE, CHANNEL_NUMS
            )
        await stream.input_stream.end_stream()

    # Instantiate our handler and start processing events
    handler = MyEventHandler(stream.output_stream)
    await asyncio.gather(write_chunks(), handler.handle_events())


loop = asyncio.get_event_loop()
loop.run_until_complete(basic_transcribe())
loop.close()

安全

有关更多信息,请参阅CONTRIBUTING

许可证

此项目采用Apache-2.0许可证。

项目详情


下载文件

下载适用于您平台的自定义文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

amazon-transcribe-0.6.2.tar.gz (31.0 kB 查看哈希值)

上传时间

构建分布

amazon_transcribe-0.6.2-py3-none-any.whl (38.8 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面