亚历山大之声服务的客户端

alexa-client的Python项目详细描述


Alexa语音服务客户端

code-climate-imagecircle-ci-imagecodecov-image

python client for alexa语音服务(avs)


安装

pip install alexa_client

或者如果您想运行演示:

pip install alexa_client[demo]

用法

文件音频

fromalexa_clientimportAlexaClientclient=AlexaClient(client_id='my-client-id',secret='my-secret',refresh_token='my-refresh-token',)client.connect()# authenticate and other handshaking stepswithopen('./tests/resources/alexa_what_time_is_it.wav','rb')asf:fori,directiveinenumerate(client.send_audio_file(f)):ifdirective.namein['Speak','Play']:withopen(f'./output_{i}.mp3','wb')asf:f.write(directive.audio_attachment)

现在听output_0.wav亚历克斯应该告诉你时间

麦克风音频

importiofromalexa_clientimportAlexaClientimportpyaudiodefcallback(in_data,frame_count,time_info,status):buffer.write(in_data)return(in_data,pyaudio.paContinue)p=pyaudio.PyAudio()stream=p.open(format=pyaudio.paInt16,channels=1,rate=16000,input=True,stream_callback=callback,)client=AlexaClient(client_id='my-client-id',secret='my-secret',refresh_token='my-refresh-token',)buffer=io.BytesIO()try:stream.start_stream()print('listening. Press CTRL + C to exit.')client.connect()fori,directiveinenumerate(client.send_audio_file(buffer)):ifdirective.namein['Speak','Play']:withopen(f'./output_{i}.mp3','wb')asf:f.write(directive.audio_attachment)finally:stream.stop_stream()stream.close()p.terminate()

多步骤请求

alexa命令可能与以前的命令有关,例如,

[你]“亚历克斯,回答20个问题” [亚历山大]“是动物、矿物还是蔬菜?” [你]“矿物” “有价值吗” [你]“不” [Alexa]“是吗……”

这可以通过将相同的对话框请求id传递给多个send_audio_file调用来实现:

fromalexa_client.alexa_clientimporthelpersdialog_request_id=helpers.generate_unique_id()directives_one=client.send_audio_file(audio_one,dialog_request_id=dialog_request_id)directives_two=client.send_audio_file(audio_two,dialog_request_id=dialog_request_id)directives_three=client.send_audio_file(audio_three,dialog_request_id=dialog_request_id)

运行流媒体麦克风音频演示以使用此功能:

pip install alexa_client[demo]
python -m alexa_client.demo.streaming_microphone \
    --client-id="{enter-client-id-here}"\
    --client-secret="{enter-client-secret-here"}\
    --refresh-token="{enter-refresh-token-here}"

ASR配置文件

自动语音识别(ASR)配置文件,针对不同距离的用户语音进行优化默认情况下,使用close_talk,但可以指定:

from alexa_client import constants

client.send_audio_file(
    audio_file=audio_file,
    distance_profile=constants.NEAR_FIELD,  # or constants.FAR_FIELD
)

音频格式

默认情况下,假定为pcm音频格式,但可以指定opus:

from alexa_client import constants

client.send_audio_file(
    audio_file=audio_file,
    audio_format=constants.OPUS,
)

当指定了pcm格式时,音频应为16位线性pcm(lpcm16)、16khz采样率、单通道和小尾数。

当指定opus forat时,音频应为16位opus、16 kHz采样率、32 k比特率和小尾数。

身份验证

要使用avs,首先必须有一个developer account。然后注册产品here在“您的产品是应用程序还是设备”下选择“应用程序”?

客户需要您的client_idsecretrefresh_token

client kwargNotes
^{}Retrieve by clicking on the your product listed here
^{}Retrieve by clicking on the your product listed here
^{}You must generate this. See below

刷新令牌

您需要通过网络浏览器登录到amazon以获取刷新令牌。

若要启用此功能,请转到here,然后单击产品在Security Profile下设置一些安全设置:

settingvalue
Allowed Originshttp://localhost:9000
Allowed Return URLshttp://localhost:9000/callback/

请注意在“产品信息”下为“产品ID”输入的内容,因为这将用作设备类型ID(区分大小写!)

然后运行:

python -m alexa_client.refreshtoken.serve \
    --device-type-id="{enter-device-type-id-here}"\
    --client-id="{enter-client-id-here}"\
    --client-secret="{enter-client-secret-here}"

按照网页浏览器中http://localhost:9000所示的屏幕说明进行操作。 完成后,亚马逊将返回您的refresh_token-您需要send audiorecorded voice

将音频传送到AVS

AlexaClient.send_audio_file流媒体将类似文件的对象上载到avs,延迟时间很长。类文件对象可以是文件系统中的实际文件、包含麦克风音频的内存bytesio缓冲区,甚至是来自your browser over a websocket in real-time的音频流。

永久AVS连接

调用AlexaClient.connect将创建到AVS的持久连接在没有向AVS发出请求4分钟后,运行一个ping AVS的线程这可以防止由于不活动而强制关闭连接。

单元测试

要运行单元测试,请调用以下命令:

git clone git@github.com:richtier/alexa-voice-service-client.git
make test_requirements
make test

其他项目

这个库由alexa-browser-client使用,它允许您从浏览器与Alexa对话

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java从Struts操作的钩子更改表单值(Liferay 6.2)   java如何改变Spring MVC处理url“点”字符的行为   Clojure中使用Java接口的问题   junit如何模拟comosDB azure java集成客户端?   运行函数时发生java错误   netbeans Java。jar文件和依赖项   Eclipse中文本字段中的java输入类型   在JAVA中,将集合变量从实时批处理运行时传递到调用批处理文件   java通过Bean验证API中的自定义消息管理异常   XML到json的转换,在Java中,同时保留数据类型   根据JAVA中的列名从Excel文件中获取单元格值   在AsyncTask的doInBackground中未调用java FirebaseMessagingService   java如何在数据库中插入一行?