From a4e340f517dbe4941650fbd219d1deabaf8c2537 Mon Sep 17 00:00:00 2001 From: konradSC Date: Mon, 19 Aug 2019 11:17:03 -0400 Subject: [PATCH] Watson Transcription (#4420) The following PR is for voicemail transcription using IBM Watson. https://cloud.ibm.com/catalog/services/speech-to-text The following values need to be configured in Default Settings: Category: Voicemail Subcategory: watson_key type: text Value: [Your Watson api key ] Category: Voicemail Subcategory: json_enabled type: boolean Value: true Category: Voicemail Subcategory: transcibe_language type: text Value: en-US Category: Voicemail Subcategory: transcribe_provider type: text Value: watson Category: Voicemail Subcategory: watson_url type: text Value: https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?model=en-US_NarrowbandModel Category: Voicemail Subcategory: transcribe_enabled type: boolean Value: true Reload Default Settings. I Flushed Cache and Reloaded XML just for good measure. Also make sure transcription is set to TRUE in your voicemail box. --- .../resources/functions/record_message.lua | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/resources/install/scripts/app/voicemail/resources/functions/record_message.lua b/resources/install/scripts/app/voicemail/resources/functions/record_message.lua index 98910b52d4..efc402b4ae 100644 --- a/resources/install/scripts/app/voicemail/resources/functions/record_message.lua +++ b/resources/install/scripts/app/voicemail/resources/functions/record_message.lua @@ -209,6 +209,74 @@ end end + --Watson + if (transcribe_provider == "watson") then + local api_key = settings:get('voicemail', 'watson_key', 'text') or ''; + local transcription_server = settings:get('voicemail', 'watson_url', 'text') or ''; + if (api_key ~= '') then + transcribe_cmd = [[ curl -X POST -u "apikey:]]..api_key..[[" --header "Content-type: audio/wav" --data-binary @]]..file_path..[[ "]]..transcription_server..[[" ]] + local handle = io.popen(transcribe_cmd); + local transcribe_result = handle:read("*a"); + handle:close(); + if (debug["info"]) then + freeswitch.consoleLog("notice", "[voicemail] CMD: " .. transcribe_cmd .. "\n"); + freeswitch.consoleLog("notice", "[voicemail] RESULT: " .. transcribe_result .. "\n"); + end + + --Trancribe request can fail + if (transcribe_result == '') then + freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n"); + return '' + else + status, transcribe_json = pcall(JSON.decode, transcribe_result); + + if not status then + if (debug["info"]) then + freeswitch.consoleLog("notice", "[voicemail] error decoding watson json\n"); + end + return ''; + end + end + + + if (transcribe_json["results"] ~= nil) then + --Transcription + if (transcribe_json["results"][1]["alternatives"][1]["transcript"] ~= nil) then + transcription = ''; + for key, row in pairs(transcribe_json["results"]) do + transcription = transcription .. row["alternatives"][1]["transcript"]; + end + if (debug["info"]) then + freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: " .. transcription .. "\n"); + end + else + if (debug["info"]) then + freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n"); + end + return ''; + end + --Confidence + if (transcribe_json["results"][1]["alternatives"][1]["confidence"]) then + if (debug["info"]) then + freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: " .. transcribe_json["results"][1]["alternatives"][1]["confidence"] .. "\n"); + end + confidence = transcribe_json["results"][1]["alternatives"][1]["confidence"]; + else + if (debug["info"]) then + freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: (null) \n"); + end + end + + return transcription; + else + if (debug["info"]) then + freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: json error \n"); + end + return ''; + end + end + end + if (transcribe_provider == "custom") then local transcription_server = settings:get('voicemail', 'transcription_server', 'text') or ''; local api_key = settings:get('voicemail', 'api_key', 'text') or '';