亚历山大之声服务的客户端
alexa-client的Python项目详细描述
Alexa语音服务客户端
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_id
、secret
和refresh_token
:
client kwarg | Notes |
---|---|
^{ | 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
下设置一些安全设置:
setting | value |
---|---|
Allowed Origins | http://localhost:9000 |
Allowed Return URLs | http://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 audio或recorded 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对话