<返回更多

CentOS 7 Freeswitch1.10.7 对接百度MrcpServer实现 TTS和ASR

2022-09-28  今日头条  IT界螺丝钉
加入收藏

通过百度MrcpServer实现简单的呼叫对话机器人。

一、参考百度AI接入指南,创建应用,获取AppID、API Key、Secret Key,用于后续配置使用

https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjgn3

二、下载MrcpServer安装包
sdk_asr_MRCPServer-20200609.tar.gz

https://ai.baidu.com/ai-doc/SPEECH/7kaxz0h2z

https://ai.baidu.com/download?sdkId=111

wget https://platform.bj.bcebos.com/sdk%2FasMRCPServer-20200609.tar.gz

 

 

三、解压
sdk_asr_MRCPServer-20200609.tar.gz文件。会生成个MRCPServer文件夹。

tar -xvf sdk%2Fasr%2FMRCPServer-20200609.tar.gz

四、.安装百度MrcpServer

cd MRCPServer/

sh ./bootstrap.sh

ll /opt/compiler/

total 4

drwxrwxr-x 3 3308 3308 4096 Jun 9 2020 gcc-8.2

五、配置Mrcp

1>配置ASR,为FreeSWITCH提供语音识别服务。

mrcp-server/conf/mrcp-asr.conf

更改AUTH_APPID和AUTH_APPKEY为从百度官方获取的APPID和API Key的值

 

 

 

2>配置TTS,为FreeSWITCH提供语音合成服务。
配置文件为:
mrcp-server/conf/mrcp-proxy.conf

更改AUTH_APPID和AUTH_APPKEY为从百度官方获取的APPID和API Key的值

 

3>配置MrcpServer的SIP服务端,为FreeSWITCH的MrcpClient提供SIP服务。

mrcp-server/conf/unimrcpserver.xml

 

 

4>启动MrcpServer,无报错即正常。

cd mrcp-server/

./bin/unimrcpserver -r . &

 

 

5>FreeSWITCH使用百度的MrcpServer需要用到grammar.xml这个语法文件。

cp /MRCPServer/mrcp-server/data/grammar.xml /etc/freeswitch/grammar/baidu.gram

六、FreeSWITCH配置

vim /etc/freeswitch/conf/mrcp_profiles/baiduserver.xml

<include>
  <!-- 后面我们使用该配置文件,均使用 name 作为唯一标识,而不是文件名 -->
  <profile name="baiduserver" version="2">
    <!-- Mrcp Server 地址 -->
    <!-- Mrcp Server 地址和FreeSWITCH如果不在同一个服务器,请酌情修改 -->
    <param name="server-ip" value="127.0.0.1"/>
    <!-- Mrcp Server 端口号 -->
    <param name="server-port" value="6060"/>
    <param name="resource-location" value=""/>

    <!-- FreeSWITCH的Mrcp Clent的ip/port/transfer/rtp-ip/rtp-port等信息 -->
    <!-- Mrcp Server 地址和FreeSWITCH如果不在同一个服务器,请酌情修改client-ip/rtp-ip,以便能连接到Mrcp Server -->
    <param name="client-ip" value="127.0.0.1" />
    <param name="client-port" value="6666"/>
    <param name="sip-transport" value="udp"/>
	<param name="rtp-ip" value="127.0.0.1"/>
    <param name="rtp-port-min" value="21000"/>
    <param name="rtp-port-max" value="22000"/>
    
    <param name="speechsynth" value="speechsynthesizer"/>
    <param name="speechrecog" value="speechrecognizer"/>
    <param name="codecs" value="PCMU PCMA L16/96/8000"/>

    <!-- Add any default MRCP params for SPEAK requests here -->
    <synthparams>
    </synthparams>

    <!-- Add any default MRCP params for RECOGNIZE requests here -->
    <recogparams>
      <!--param name="start-input-timers" value="false"/-->
    </recogparams>
  </profile>
</include>

 

vim /etc/freeswitch/conf/autoload_configs/unimrcp.conf.xml

<configuration name="unimrcp.conf" description="UniMRCP Client">
  <settings>
    <param name="default-tts-profile" value="baiduserver"/>
    <param name="default-asr-profile" value="baiduserver"/>
    <param name="log-level" value="DEBUG"/>
    <param name="enable-profile-events" value="false"/>

    <param name="max-connection-count" value="100"/>
    <param name="offer-new-connection" value="1"/>
    <param name="request-timeout" value="3000"/>
  </settings>

  <profiles>
    <X-PRE-PROCESS cmd="include" data="../mrcp_profiles/*.xml"/>
  </profiles>

</configuration>

七、测试

 

 

   <!-- 百度 unimrcp server asr 测试 Python/ target=_blank class=infotextkey>Python版 -->
   <!-- play_and_detect_speech -->
   <extension name="asr-play-and-detect-speech">
        <condition field="destination_number" expression="^88994$">
            <action application="sleep" data="1000"/>
            <action application="answer" /> 
            <action application="python" data="test"/>
        </condition>
   </extension>
import os
import freeswitch
from freeswitch import *
import sys
import json
import tempfile
# import requests
import xml.etree.ElementTree as ET
import freeswitch as fs
from freeswitch import *
from xml.dom.minidom import parseString
import random
import requests

###########百度UNIT3.0聊天###########
client_id = "ADIB9GGUGzYNpL**************"
client_key = "eOu8qYRU5sogDKMKVkMYheXV**********"
user_id = "88888"  # 默认user_id都为88888 

def handler(session, args):
    session.answer()

    session.sleep(1000)
    session.execute("speak", "unimrcp:baiduserver||" + "我是你的智能小卓子,有什么可以帮助你的?") 
    tryagain = 0
    while session.ready(): 
        session.execute("play_and_detect_speech",
                        "silence_stream://1000 detect:unimrcp:baiduserver {start-input-timers=false,no-input-timeout=60000,input-timeout=60000,recognition-timeout=60000}builtin:grammar/baidu")
        
        asr_result = session.getVariable('detect_speech_result')
        
        if asr_result is None:
            tryagain = 0
        else:
            try:
                freeswitch.consoleLog("CRIT", "n====================n'" + asr_result + "'====================n")
                #语音转文字
                guest_text = asr2text(asr_result)
                # freeswitch.consoleLog("CRIT", "----------------结果识别-------------------:'" + text + "'n") 
                tryagain = 1 
                #对接机器人
                bot_text=bot_chat(guest_text, user_id, client_id, client_key)
                session.execute("speak", "unimrcp:baiduserver||" + bot_text)
            except Exception as e:
                fs.consoleLog('CRIT', '>>> ASR result parse failed n%s' % e)
                continue
        session.sleep(1000)
    session.hangup()


def asr2text(text): 
     xml_dom = parseString(text)  
     collection = xml_dom.documentElement
     returnInfo = collection.getElementsByTagName("input") 
     guest_text =returnInfo[0].firstChild.data 
     return guest_text
def bot_chat(chat_input, user_id, client_id, client_secret):
    # 设置默认回复
    chat_reply = "不好意思,我正在学习中,随后回复你"
    # 固定的url格式
    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s" % (
    client_id, client_secret)
    res = requests.post(url)
    access_token = json.loads(res.text)["access_token"] 
    unit_chatbot_url = "https://aip.baidubce.com/rpc/2.0/unit/service/v3/chat?access_token=" + access_token
    # 拼装聊天接口对应请求    
    post_data = {
        "version": "3.0",
        "service_id": "S75157",
        "session_id": "",
        "log_id": str(random.random()),
        "skill_ids":["1237888","1237895"],
        "request":
            {
                "terminal_id": user_id,
                "query": chat_input
            }
    }  
    # 将聊天接口对应请求数据转为json数据
    res = requests.post(url=unit_chatbot_url, json=post_data)

    # 获取聊天接口返回数据
    unit_chat_obj = json.loads(res.text) 
    if unit_chat_obj["error_code"] != 0:
        return chat_reply+'('+unit_chat_obj["error_msg"]+')' 
    unit_chat_response_reply = unit_chat_obj['result']['responses'][0]['actions'][0]['say']
    return unit_chat_response_reply 
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>