使用百度的语音识别,实现AI对话的案例

  1. 注册并获取百度API Key

在百度智能云控制台注册并获取语音识别的API Key和Secret Key。同时还需要注册并获取语音合成的API Key和Secret Key。这两个Key需要分别用于语音识别和语音合成。

  1. 安装mod_vbr、mod_dptools、mod_curl和mod_flite

我们需要在FreeSWITCH中安装一些其他模块,以便实现语音识别、语音合成和互动。

可以通过以下命令进行安装:

sudo apt-get install freeswitch-mod-vbr freeswitch-mod-dptools freeswitch-mod-curl freeswitch-mod-flite
  1. 创建语音流程

现在,我们需要创建一个语音流程,使用户可以与机器人进行交互。

我们选择使用dialplan中的ivr(Interactive Voice Response,即自动语音应答)模块来实现这个流程。

以下是一个简单的IVR流程示例:

<include>
  <context name="public">
    <extension name="ivr_demo">
      <condition field="destination_number" expression="1000">
        <!-- 播放欢迎语音 -->
        <action application="play_and_detect_speech" data="welcome/zh 3 detect:*"/>
        <!-- 记录语音识别结果 -->
        <action application="set" data="result=${detect_speech_result}"/>
        <!-- 判断语音识别结果并响应 -->
        <action application="fifo" data="ivr_demo ${result}"/>
        <action application="bridge" data="user/${result}@speak"/>
      </condition>
    </extension>
  </context>
  <context name="speak">
    <extension name="speak">
      <condition field="destination_number" expression="(.*)">
        <!-- 语音合成 -->
        <action application="synthesize_speech" data="text:${destination_number},voice:zh,engine:baidu"/>
      </condition>
    </extension>
  </context>
</include>

在这个dialplan中,当被叫号码为1000时,机器人将首先播放欢迎语音。播放完成后,用detect_speech应用程序记录用户的语音输入结果。接着,使用fifo应用程序将结果存入ivr_demo队列,然后使用bridge应用程序将用户连接到speak context。speakcontext中,将使用synthesize_speech应用程序将用户的语音输入结果转换为语音响应,并发送回给用户。synthesize_speech将使用Baidu TTS API进行语音合成。

  1. 配置mod_curl

使用Baidu提供的RESTful API请求语音识别和语音合成服务。需要在/etc/freeswitch/autoload_configs/curl.conf.xml中添加百度API的配置,如下所示:

<configuration name="curl.conf" description="Load curl XML directory config">
  <bindings>
    <binding name="baidu-asr">
      <param name="gateway-url" value="https://vop.baidu.com/server_api"/>
      <param name="header" value="Content-Type:audio/wav;rate=16000"/>
      <param name="get-data" value=""/>
      <param name="post-data" value="{\"format\":\"wav\",\"rate\":16000,\"channel\":1}" nodata="true"/>
      <param name="post-url" value="https://vop.baidu.com/server_api"/>
      <param name="post-query" value="?cuid=70:ee:50:79:33:bb00&amp;token=baidu_token&amp;lan=zh"/>
      <param name="post-xml" value=""/>
      <param name="upload-file" value="${recordings_dir}/${uuid}.wav"/>
      <param name="download-file" value="${recordings_dir}/${uuid}.txt"/>
      <param name="content-type" value="application/json"/>
      <param name="extension" value=".txt"/>
      <param name="retry" value="400"/>
    </binding>
    <binding name="baidu-tts">
      <param name="gateway-url" value="https://tsn.baidu.com/text2audio"/>
      <param name="header" value=""/>
      <param name="get-data" value=""/>
      <param name="post-data" value="{&quot;tex&quot;: &quot;${text}&quot;,&quot;tok&quot;: &quot;${baidu_token}&quot;,&quot;ctp&quot;: &quot;1&quot;,&quot;cuid&quot;: &quot;70:ee:50:79:33:bb00&quot;,&quot;lan&quot;: &quot;zh&quot;,&quot;spd&quot;: 6,&quot;pit&quot;: 6,&quot;vol&quot;: 5,&quot;per&quot;: 1}" nodata="false"/>
      <param name="post-url" value="https://tsn.baidu.com/text2audio"/>
      <param name="post-query" value=""/>
      <param name="post-xml" value=""/>
      <param name="upload-file" value=""/>
      <param name="download-file" value="${recordings_dir}/${uuid}.mp3"/>
      <param name="content-type" value="application/json"/>
      <param name="extension" value=".mp3"/>
      <param name="retry" value="400"/>
      <param name="command" value="newest"/>
    </binding>
  </bindings>
</configuration>

在这个配置中,我们定义了两个binding,一个用于语音识别(baidu-asr),另一个用于语音合成(baidu-tts)。其中,我们需要将${baidi_token}替换为你在控制台中获取的百度API token。

  1. 启动FreeSWITCH

设置好这些配置后,现在可以启动FreeSWITCH来测试这个语音机器人了。您可以使用以下命令启动FreeSWITCH:

sudo service freeswitch start

尝试拨打号码1000,看看语音机器人是否可以理解您说的话,并正确响应。

请注意,本示例是一个简单的框架,您需要进一步使用API来定制和增强您的机器人。同时,我们也建议您参考百度API的文档进行使用和开发。

posted @ 2023-06-17 10:19  孙同海  阅读(378)  评论(0编辑  收藏  举报