用于Amazon转录流的异步Python SDK
amazon-transcribe的Python项目详细描述
亚马逊转录流式SDK
Amazon转录流式SDK允许用户直接与 亚马逊转录流媒体服务和他们的Python程序。目标 该项目是为了让用户能够直接与亚马逊转录集成 不需要任何东西,只需要一个音频字节流和一个基本的处理程序。在
该项目仍处于早期alpha阶段,因此界面仍可能发生变化 可能会出现快速迭代。强烈建议使用严格的依赖关系 如果在本地测试之外使用这个。在
安装
从pip安装:
python -m pip install amazon-transcribe
要从Github安装:
^{pr2}$要从Python应用程序中使用,请在requirements.txt
文件中添加amazon-transcribe
作为依赖项。在
注意:此SDK构建在 AWS Common Runtime (CRT),一个集合 我们通过绑定与C库交互。CRT在PyPI上可用 (awscrt)作为通用平台的预编译轮 (Linux、macOS、Windows)。非标准操作系统可能需要编译这些 图书馆本身。在
使用
先决条件
如果您还没有为您的AWS帐户设置本地凭据,可以按照下面的步骤操作 这个guide 用于使用AWS CLI进行配置。在
快速启动
此SDK的安装将需要实时或预录制的音频。全部细节 关于音频输入的要求可以在Amazon Transcribe Streaming documentation中找到。在
下面是一个应用程序示例:
importasyncio# This example uses aiofile for asynchronous file reads.# It's not a dependency of the project but can be installed# with `pip install aiofile`.importaiofilefromamazon_transcribe.clientimportTranscribeStreamingClientfromamazon_transcribe.handlersimportTranscriptResultStreamHandlerfromamazon_transcribe.modelimportTranscriptEvent"""Here's an example of a custom event handler you can extend toprocess the returned transcription results as needed. Thishandler will simply print the text out to your interpreter."""classMyEventHandler(TranscriptResultStreamHandler):asyncdefhandle_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.resultsforresultinresults:foraltinresult.alternatives:print(alt.transcript)asyncdefbasic_transcribe():# Setup up our client with our chosen AWS regionclient=TranscribeStreamingClient(region="us-west-2")# Start transcription to generate our async streamstream=awaitclient.start_stream_transcription(language_code="en-US",media_sample_rate_hz=16000,media_encoding="pcm",)asyncdefwrite_chunks():# An example file can be found at tests/integration/assets/test.wav# 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.asyncwithaiofile.AIOFile('tests/integration/assets/test.wav','rb')asafp:reader=aiofile.Reader(afp,chunk_size=1024*16)asyncforchunkinreader:awaitstream.input_stream.send_audio_event(audio_chunk=chunk)awaitstream.input_stream.end_stream()# Instantiate our handler and start processing eventshandler=MyEventHandler(stream.output_stream)awaitasyncio.gather(write_chunks(),handler.handle_events())loop=asyncio.get_event_loop()loop.run_until_complete(basic_transcribe())loop.close()
安全
有关详细信息,请参见CONTRIBUTING。在
许可证
这个项目是在Apache-2.0许可下授权的。在
- 项目
标签: