Skip to main content

用于 Alexa 语音服务 (AVS) 的 Python 客户端

项目描述

Alexa语音服务客户端

代码气候图像 圆-ci-图像 编解码器图像

用于 Alexa 语音服务 (AVS) 的 Python 客户端


安装

pip install alexa_client

或者如果你想运行演示:

pip install alexa_client[demo]

用法

文件音频

from alexa_client import AlexaClient

client = AlexaClient(
    client_id='my-client-id',
    secret='my-secret',
    refresh_token='my-refresh-token',
)
client.connect()  # authenticate and other handshaking steps
with open('./tests/resources/alexa_what_time_is_it.wav', 'rb') as f:
    for i, directive in enumerate(client.send_audio_file(f)):
        if directive.name in ['Speak', 'Play']:
            with open(f'./output_{i}.mp3', 'wb') as f:
                f.write(directive.audio_attachment)

现在听output_0.wav,Alexa 应该会告诉你时间。

麦克风音频

import io

from alexa_client import AlexaClient
import pyaudio


def callback(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()
    for i, directive in enumerate(client.send_audio_file(buffer)):
        if directive.name in ['Speak', 'Play']:
            with open(f'./output_{i}.mp3', 'wb') as f:
                f.write(directive.audio_attachment)
finally:
    stream.stop_stream()
    stream.close()
    p.terminate()

多步骤请求

Alexa 命令可能与先前的命令有关,例如,

[你]“Alexa,玩二十个问题”[Alexa]“它是动物、矿物还是植物?” [你]“矿物”[Alexa]“它有价值吗”[你]“不”[Alexa]“它是……”

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

from alexa_client.alexa_client import helpers

dialog_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 格式时,音频应为 16 位 Opus、16kHz 采样率、32k 比特率和小端。

基本网址

base_url可以设置以改善延迟。选择离您所在位置最近的地区。

from alexa_client.alexa_client import constants

client = AlexaClient(
    client_id='my-client-id',
    secret='my-secret',
    refresh_token='my-refresh-token',
    base_url=constants.BASE_URL_ASIA
)

默认的基本 URL 是欧洲。可用的常量是 BASE_URL_EUROPE、BASE_URL_ASIA 和 BASE_URL_NORTH_AMERICA,但如果需要,您可以传递任何字符串。

阅读更多

验证

要使用 AVS,您必须首先拥有一个开发者帐户然后在这里注册您的产品。在“您的产品是应用程序还是设备”下选择“应用程序”?

客户需要您的client_id,secretrefresh_token:

客户夸克 笔记
client_id 通过单击此处列出的产品进行检索
secret 通过单击此处列出的产品进行检索
refresh_token 你必须生成这个。见下文

刷新令牌

您需要通过 Web 浏览器登录 Amazon 以获取刷新令牌。

要启用此功能,请先转到此处并单击您的产品以在以下位置设置一些安全设置Security Profile

环境 价值
允许的来源 http://localhost:9000
允许的返回 URL 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:9000Web 浏览器中显示的屏幕说明进行操作。完成后,亚马逊将退回您的refresh_token- 您需要发送音频录制的语音

将音频流式传输到 AVS

AlexaClient.send_audio_file流式传输将类似文件的对象上传到 AVS 以获得很大的延迟。类似文件的对象可以是文件系统上的实际文件、包含来自麦克风的音频的内存中 BytesIo 缓冲区,甚至可以是来自浏览器的实时音频流。

持久的 AVS 连接

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

单元测试

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

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

其他的项目

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

项目详情


下载文件

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

内置分布

alexa_client-1.5.1-py3-none-any.whl (16.0 kB 查看哈希

已上传 py3