AI use Baidu quickly developed a "Q & A Robot" and access to small programs

Look at the results achieved:

 

Use Baidu UNIT preset intelligent questions and answers skills and micro-channel small program to achieve voice response robot. This introduces a small program to achieve functional development process, sharing subroutine modules main function, it is dry!

To learn the skills UNIT preset call, please refer to my previous post: "UNIT build a robot assistant"

https://ai.baidu.com/forum/topic/show/953021

For development of small micro-channel program, see my previous post: "UNIT access applet" https://ai.baidu.com/forum/topic/show/953022

1 System Framework

Technologies used are: Baidu speech recognition, speech synthesis, and micro-channel semantic parser UNIT applets. Applet through voice recognition, the user who submitted the question to Baidu UNIT, semantic parsing. Responses returned by speech synthesis, voice conversion, voice interaction with a user. All functions are in complete applet client, no server, suitable for individual developers.

2 small projects program

2.1 program creates

App.json increase in global configuration files in the root directory: "pages / contact / contact", automatically creates the relevant page of the file, structured as follows:

contact.js: functional logic module

contact.wxss: Page Style Files

contact.wxml: page layout file

contact.json: Page Profiling

2.2 applet recording function to achieve

Recording manager using micro-channel provided recorderManager achieve recording, the recording format aac. It should be noted that the micro-channel development tools and mobile phones recording the results of the files on the computer is inconsistent, format is set to 'aac', the computer end of the recording format is aac, m4a format is a recording mobile terminal. Since Baidu speech recognition speed version of the applet currently supports micro-channel recording m4a formats, so you do not upload the voice file transfer format, a lot easier!

// Get globally unique recording manager recorderManager

const recorderManager = wx.getRecorderManager();

// 录音时需要的参数, format设置为aac

const voiceOptions = {

  duration: 60000,

  sampleRate: 16000,

  numberOfChannels: 1,

  encodeBitRate: 48000,

  format: 'aac',

  frameSize: 50

}

// 按钮按下

  touchdown: function () {

     // 开始录音

    recorderManager.start(voiceOptions);

    this.setData({

      isSpeaking: true,

    })

    that.speaking.call();

    console.log("[Console log]:Touch down!Start recording!");

  },

  // 停止录音,会触发onStop事件

  touchup: function () {

    recorderManager.stop(voiceOptions)

    console.log("[Console log]:Touch up!Stop recording!");

    this.setData({

      isSpeaking: false,

      speakerUrl: '/res/image/speaker.png',

    })

    clearInterval(that.speakerInterval);//定时器停止

  },



// 添加录音停止触发事件,这段代码可以放到onLoad()里,页面加载的时候就添加上

    recorderManager.onStop((res) => {

      const { tempFilePath, fileSize } = res

  //录音完成调用语音识别API

      this.sendAsrRequest(res.tempFilePath, res.fileSize);  

    });

 

2.3 小程序语音播放功能实现

需要注意的是:小程序自身录音,用wx.playVoice()函数播放不了,要用到innerAudioContext。 

//微信语音播放,

  play: function (e) {

    const innerAudioContext = wx.createInnerAudioContext()

    innerAudioContext.autoplay = true

    innerAudioContext.src = filePath

    innerAudioContext.onPlay(() => {

      console.log('开始播放')

    })

    innerAudioContext.onError((res) => {

      console.log(res.errMsg)

      console.log(res.errCode)

    })

  },

3 调用语音识别极速版API

3.1 首先要在控制台创建应用,调用语音识别极速版API,“获取API Key/Secret Key”。

接口文档地址:https://ai.baidu.com/docs#/ASR-API-PRO/top

请求URL: https://vop.baidu.com/pro_api

3.2 语音识别功能实现

//发送语音识别请求,传入语音文件路径及长度,len为录音结束返回的字节长度:res.fileSize。

  ASRRequest: function (tempFilePath,len,arg) { // corpus是要发送的对话;arg是回调方法

    var that = this;

    // appkey

    var appkey = that.globalData.NLPAppkey;

    // appsecret

    var appSecret = that.globalData.NLPAppSecret;

    var api = "nli";

    var timestamp = new Date().getTime();

    var voice0 = fs.readFileSync(tempFilePath, "base64");

    console.log("[Console log]voice:" + voice0);

    console.log("[Console log]len:" + len);

    var rqJson = {

      'dev_pid': 80001,

      'format': 'm4a',

      'rate': 16000,

      'token': '填入获得的token ',

      'cuid': '填入cuid ',

      'channel': 1,

      'len': len,

      'speech': voice0

    };

    var rq = JSON.stringify(rqJson);

    console.log(rq);

    var ASRUrl = that.globalData.ASRUrl;

    // cusid是用来实现上下文的,可以自己随意定义内容,要够长够随机

    var cusid = that.globalData.NLPCusid;

    console.log("[Console log]:ASRRequest(),URL:" + ASRUrl);

    wx.request({

      url: ASRUrl,

      data: rq,

      header: { 'content-type': 'application/json' },

      method: 'POST',

      success: function (res) {

        var resData = res.data;

        console.log("[Console log]:ASTRequest() success...");

        console.log("[Console log]:Result:" + resData);

        var nli = JSON.stringify(resData);

      

        // 回调函数,解析数据

        typeof arg.success == "function" && arg.success(nli);

      },

      fail: function (res) {

        console.log("[Console log]:ASRRequest() failed...");

        console.error("[Console log]:Error Message:" + res.errMsg);

        typeof arg.fail == "function" && arg.fail();

      },

      complete: function () {

        console.log("[Console log]:ASRRequest() complete...");

        typeof arg.complete == "function" && arg.complete();

      }

    })

  },

4 调用UNIT接口,获得回答

4.1 首先要在控制台创建应用,调用UNIT接口,“获取API Key/Secret Key”。

接口文档地址:https://ai.baidu.com/docs#/UNIT-v2-API/top

请求URL: https://aip.baidubce.com/rpc/2.0/unit/bot/chat

4.2 程序实现

NLIRequest: function (corpus, arg) { // corpus是要发送的对话;arg是回调方法

    var that = this;

    // appkey

    var appkey = that.globalData.NLPAppkey;

      // appsecret

    var appSecret = that.globalData.NLPAppSecret;

    var api = "nli";

    var timestamp = new Date().getTime();

    var rqJson = {

      "bot_session": "",

      "log_id": "7758521",

      "request": {

        "bernard_level": 0,

        "client_session": "{\"client_results\":\"\", \"candidate_options\":[]}",

        "query": corpus,

        "query_info": {

          "asr_candidates": [],

          "source": "KEYBOARD",

          "type": "TEXT"

        },

        "updates": "",

        "user_id": "88888"

      },

      "bot_id": "64053",

      "version": "2.0"

      };

    var rq = JSON.stringify(rqJson);

    var nliUrl = that.globalData.NLPUrl;

    // cusid是用来实现上下文的,可以自己随意定义内容,要够长够随机

    var cusid = that.globalData.NLPCusid;

    console.log("[Console log]:NLIRequest(),URL:" + nliUrl);

    wx.request({

      url: nliUrl,

      data: rq,

      header: { 'content-type': 'application/x-www-form-urlencoded' },

      method: 'POST',

      success: function (res) {

        console.log("[Console log]:res:"+ res);

        var resData = res.data;

        console.log("[Console log]:NLIRequest() success...");

        console.log("[Console log]:Result:");

        console.log("[Console log]:resData:"+resData);

        var nli = JSON.stringify(resData);

        console.log("[Console log]:nli:" + nli);

      

        // 回调函数,解析数据

        typeof arg.success == "function" && arg.success(nli);

      },

      fail: function (res) {

        console.log("[Console log]:NLIRequest() failed...");

        console.error("[Console log]:Error Message:" + res.errMsg);

        typeof arg.fail == "function" && arg.fail();

      },

      complete: function () {

        console.log("[Console log]:NLIRequest() complete...");

        typeof arg.complete == "function" && arg.complete();

      }

    })

  },

5 调用语音合成API

5.1 首先要在控制台创建应用,调用语音合成API,“获取API Key/Secret Key”。

接口文档地址:https://ai.baidu.com/docs#/TTS-API/top

请求URL: https://tsn.baidu.com/text2audio

5.2 程序实现

// 语音合成

  tts: function (e) {

    console.log("[Console log]tts:" + e);

    var tex = encodeURI(e);//转换编码url_encode UTF8编码

    var tok = "填入获得的token";

    var cuid = app.globalData.NLPCusid;

    var ctp = 1;

    var lan = "zh";    // zh表示中文

    // 字符编码

    var spd = 5;  // 表示朗读的语速,9代表最快,1是最慢(撩妹请用2,绕口令请用9)

    var url = "https://tsn.baidu.com/text2audio?tex=" + tex + "&lan=" + lan + "&cuid=" + cuid + "&ctp=" + ctp + "&tok=" + tok + "&spd=" + spd



    wx.downloadFile({

      url: url,

      success: function (res) {

        console.log(res)

        filePath = res.tempFilePath;

        // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调

        if (res.statusCode === 200) {

          //小程序自身录音,用playVoice播放不了,要用innerAudioContext

        /*  wx.playVoice({

            filePath: res.tempFilePath

          })*/

          var filepath = res.tempFilePath;

          console.log(filepath);

          const innerAudioContext = wx.createInnerAudioContext();

          innerAudioContext.src = filepath;

          innerAudioContext.onPlay(() => {

            console.log('开始播放')

          });

          innerAudioContext.onError((res) => {

            console.log(res.errMsg)

            console.log(res.errCode)

          });

          innerAudioContext.play();

        }

      }

    })

  },

作者:wangwei8638

Guess you like

Origin www.cnblogs.com/AIBOOM/p/11725525.html