Asterisk Speech-to-text detection

sachin
edited October 2023 in asterisk

Now, translation can be done externally in the programming language of one's choice using AEAP. Asterisk External Application Protocol (AEAP) to facilitate external speech to text translation in Asterisk 18.12.0+ and 19.4.0+.

Following links can be followed:

  1. Asterisk External Application Protocol (AEAP)
  2. Github : aeap-speech-to-text

With above, it sends the live stream to an AI Engine (with ability to handle the live streams)

Another way

Asterisk text-to-speech using Chat-GPT (or any AI engine with no support to transcribe live stream)

edit dialplan vi /etc/asterisk/extentions.conf

# extensions.conf
[INBOUND]
exten => _X.,1,NoOp(enter)
same => n,Set(DTMFIMPUT=${STRFTIME(${EPOCH},,%Y-%m-%d_%H:%M:%S)})
same => n,Answer()
same => n,Playback(/var/spool/asterisk/system/1698237084)
; some IVR file to play information. eg: say yes to continue
same => n,Record(${DTMFIMPUT}.wav,2,2,ky)
; https://docs.asterisk.org/Asterisk_18_Documentation/API_Documentation/Dialplan_Applications/Record/
; x - Ignore all terminator keys (DTMF) and keep recording until hangup.
; q - quiet (do not play a beep tone).
; y - Terminate recording if any DTMF digit is received.
same => n,NoOp(${CHANNEL(language)},"wav","${RECORDED_FILE}","${RECORD_STATUS}")
same => n,AGI(agi://127.0.0.1:14000/speech-to-text,"${DTMFIMPUT}")
same => n,Hangup()

[SUCCESS]
exten => _X.,1,NoOp(success)
same => n,Playback(hello-world)
same => n,Hangup()

[FAIL]
exten => _X.,1,NoOp(fail)
same => n,Hangup()

edit vi /etc/asterisk/pjsip.conf

[7000]
type=endpoint
context=INBOUND
disallow=all
allow=ulaw
auth=7000
aors=7000

[7000]
type=auth
auth_type=userpass
password=7000
username=7000

[7000]
type=aor
max_contacts=1

Create nodejs agi server

const fs = require('fs');
const axios = require("axios");
const Promise = require('promise');
const api_Key = "chat-gpt-api-key";


var callOpenAI = function (recording_obj) {
  return new Promise((resolve, reject) => {
    axios
    .post(
        "https://api.openai.com/v1/audio/translations",
        {
            file: fs.createReadStream(
              `${recording_obj.recording_path}`
            ),
          model: "whisper-1",
        },
        {
            headers: {
              Authorization: `Bearer ${api_Key}`,
              "Content-Type": "multipart/form-data",
            },
        }
    )
    .then((response) => {
        // console.log("wisper api", response.data);
        if (response.data) {
            resolve({ recording_obj: recording_obj, response: { message: response.data.text } });
        } else {
            reject({ recording_obj: recording_obj, response: { message: "Success Message Not found." } });
        }
    })
    .catch((error) => {
        // console.log("ERROR wisper api", error.response.data);
        if (error.response.data) {
            reject({ recording_obj: recording_obj, response: { message: error.response.data.error.message } });
        } else {
            reject({ recording_obj: recording_obj, response: { message: "Error message not found" } });
        }
    });
  });
};


var aio = require('asterisk.io'),
    agi = null;
 
agi = aio.agi(14000, '127.0.0.1');
 
agi.on('error', function(err){
    throw err;
});
 
agi.on('listening', function(){
    console.log('listening on port 14000');
});
 
agi.on('close', function(){
    console.log('close');
});
 
agi.on('connection', function(agiHandler){
    if(typeof agiHandler.agi_network_script != 'undefined'){
        if(agiHandler.agi_network_script == 'speech-to-text'){
            let recording_obj = { "recording_path": `/var/lib/asterisk/sounds/${agiHandler.agi_arg_1}.wav` };

            callOpenAI({ "recording_path": `/var/lib/asterisk/sounds/${agiHandler.agi_arg_1}.wav` })
            .then(function (result) {
                agiHandler.command('SET CONTEXT SACHIN', function(code, result, data){
                    agiHandler.command('SET PRIORITY 1', function(code, result, data){
                        agiHandler.close();
                    });
                });
            })
            .catch(function (error) {
                console.log("error--------------------", error);
            });
        }
    }

    agiHandler.on('hangup', function(){
        console.log('hangup');
    });
 
    agiHandler.on('error', function(err){
        throw err;
    });
 
    agiHandler.on('close', function(){
        console.log('close');
    });
 
});
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!