Fix the indentation and white space.

This commit is contained in:
FusionPBX 2020-05-21 18:24:13 -06:00 committed by GitHub
parent 056ee9f10b
commit 8100f968c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 144 additions and 147 deletions

View File

@ -123,164 +123,161 @@
return transcription; return transcription;
end end
end end
if (transcribe_provider == "azure") then if (transcribe_provider == "azure") then
local api_key1 = settings:get('voicemail', 'azure_key1', 'text') or ''; local api_key1 = settings:get('voicemail', 'azure_key1', 'text') or '';
local api_server_region = settings:get('voicemail', 'azure_server_region', 'text') or ''; local api_server_region = settings:get('voicemail', 'azure_server_region', 'text') or '';
if (api_server_region ~= '') then if (api_server_region ~= '') then
api_server_region = api_server_region .. "."; api_server_region = api_server_region .. ".";
else else
if (debug["info"]) then if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] azure_server_region default setting must be set\n"); freeswitch.consoleLog("notice", "[voicemail] azure_server_region default setting must be set\n");
end end
return ''; return '';
end end
if (api_key1 ~= '') then if (api_key1 ~= '') then
-- search in memcache first, azure documentation claims that the access token is valid for 10 minutes -- search in memcache first, azure documentation claims that the access token is valid for 10 minutes
local cache = require "resources.functions.cache"; local cache = require "resources.functions.cache";
local key = "app:voicemail:azure:access_token"; local key = "app:voicemail:azure:access_token";
local access_token_result = cache.get(key) local access_token_result = cache.get(key)
if access_token_result then if access_token_result then
if (debug["info"]) then if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] Azure access_token recovered from memcached\n"); freeswitch.consoleLog("notice", "[voicemail] Azure access_token recovered from memcached\n");
end end
else else
access_token_cmd = "curl -X POST \"https://"..api_server_region.."api.cognitive.microsoft.com/sts/v1.0/issueToken\" -H \"Content-type: application/x-www-form-urlencoded\" -H \"Content-Length: 0\" -H \"Ocp-Apim-Subscription-Key: "..api_key1.."\""; access_token_cmd = "curl -X POST \"https://"..api_server_region.."api.cognitive.microsoft.com/sts/v1.0/issueToken\" -H \"Content-type: application/x-www-form-urlencoded\" -H \"Content-Length: 0\" -H \"Ocp-Apim-Subscription-Key: "..api_key1.."\"";
local handle = io.popen(access_token_cmd); local handle = io.popen(access_token_cmd);
access_token_result = handle:read("*a"); access_token_result = handle:read("*a");
handle:close(); handle:close();
if (debug["info"]) then if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] CMD: " .. access_token_cmd .. "\n"); freeswitch.consoleLog("notice", "[voicemail] CMD: " .. access_token_cmd .. "\n");
freeswitch.consoleLog("notice", "[voicemail] ACCESS TOKEN: " .. access_token_result .. "\n"); freeswitch.consoleLog("notice", "[voicemail] ACCESS TOKEN: " .. access_token_result .. "\n");
end end
--Access token request can fail --Access token request can fail
if (access_token_result == '') then if (access_token_result == '') then
if (debug["info"]) then if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] ACCESS TOKEN: (null) \n"); freeswitch.consoleLog("notice", "[voicemail] ACCESS TOKEN: (null) \n");
end end
return '' return ''
end end
--Azure returns JSON when it has to report an error --Azure returns JSON when it has to report an error
if (string.sub(access_token_result, 1, 1) == '{') then if (string.sub(access_token_result, 1, 1) == '{') then
if (debug["info"]) then if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] ERROR STRING: ".. access_token_result .. "\n"); freeswitch.consoleLog("notice", "[voicemail] ERROR STRING: ".. access_token_result .. "\n");
end end
return '' return ''
end end
cache.set(key, access_token_result, 120); cache.set(key, access_token_result, 120);
if (debug["info"]) then if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] Azure access_token saved into memcached: " .. access_token_result .. "\n"); freeswitch.consoleLog("notice", "[voicemail] Azure access_token saved into memcached: " .. access_token_result .. "\n");
end end
end end
transcribe_cmd = "curl -X POST \"https://"..api_server_region.."stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=".. transcribe_language .."&format=detailed\" -H 'Authorization: Bearer " .. access_token_result .. "' -H 'Content-type: audio/wav; codec=\"audio/pcm\"; samplerate=8000; trustsourcerate=false' --data-binary @"..file_path transcribe_cmd = "curl -X POST \"https://"..api_server_region.."stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=".. transcribe_language .."&format=detailed\" -H 'Authorization: Bearer " .. access_token_result .. "' -H 'Content-type: audio/wav; codec=\"audio/pcm\"; samplerate=8000; trustsourcerate=false' --data-binary @"..file_path
local handle = io.popen(transcribe_cmd); local handle = io.popen(transcribe_cmd);
local transcribe_result = handle:read("*a"); local transcribe_result = handle:read("*a");
handle:close(); handle:close();
if (debug["info"]) then if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] CMD: " .. transcribe_cmd .. "\n"); freeswitch.consoleLog("notice", "[voicemail] CMD: " .. transcribe_cmd .. "\n");
freeswitch.consoleLog("notice", "[voicemail] RESULT: " .. transcribe_result .. "\n"); freeswitch.consoleLog("notice", "[voicemail] RESULT: " .. transcribe_result .. "\n");
end end
--Trancribe request can fail --Trancribe request can fail
if (transcribe_result == '') then if (transcribe_result == '') then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n"); freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n");
return '' return ''
end end
local transcribe_json = JSON.decode(transcribe_result); local transcribe_json = JSON.decode(transcribe_result);
if (debug["info"]) then if (debug["info"]) then
if (transcribe_json["NBest"][1]["Display"] == nil) then if (transcribe_json["NBest"][1]["Display"] == nil) then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n"); freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n");
else else
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: " .. transcribe_json["NBest"][1]["Display"] .. "\n"); freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: " .. transcribe_json["NBest"][1]["Display"] .. "\n");
end end
if (transcribe_json["NBest"][1]["Confidence"] == nil) then if (transcribe_json["NBest"][1]["Confidence"] == nil) then
freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: (null) \n"); freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: (null) \n");
else else
freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: " .. transcribe_json["NBest"][1]["Confidence"] .. "\n"); freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: " .. transcribe_json["NBest"][1]["Confidence"] .. "\n");
end end
end end
transcription = transcribe_json["NBest"][1]["Display"]; transcription = transcribe_json["NBest"][1]["Display"];
confidence = transcribe_json["NBest"][1]["Confidence"]; confidence = transcribe_json["NBest"][1]["Confidence"];
return transcription; return transcription;
end end
end end
--Watson --Watson
if (transcribe_provider == "watson") then if (transcribe_provider == "watson") then
local api_key = settings:get('voicemail', 'watson_key', 'text') or ''; local api_key = settings:get('voicemail', 'watson_key', 'text') or '';
local transcription_server = settings:get('voicemail', 'watson_url', 'text') or ''; local transcription_server = settings:get('voicemail', 'watson_url', 'text') or '';
if (api_key ~= '') then if (api_key ~= '') then
if (vm_message_ext == "mp3") then if (vm_message_ext == "mp3") then
transcribe_cmd = [[ curl -X POST -u "apikey:]]..api_key..[[" --header "Content-type: audio/mp3" --data-binary @]]..file_path..[[ "]]..transcription_server..[[" ]] transcribe_cmd = [[ curl -X POST -u "apikey:]]..api_key..[[" --header "Content-type: audio/mp3" --data-binary @]]..file_path..[[ "]]..transcription_server..[[" ]]
else else
transcribe_cmd = [[ curl -X POST -u "apikey:]]..api_key..[[" --header "Content-type: audio/wav" --data-binary @]]..file_path..[[ "]]..transcription_server..[[" ]] transcribe_cmd = [[ curl -X POST -u "apikey:]]..api_key..[[" --header "Content-type: audio/wav" --data-binary @]]..file_path..[[ "]]..transcription_server..[[" ]]
end end
local handle = io.popen(transcribe_cmd); local handle = io.popen(transcribe_cmd);
local transcribe_result = handle:read("*a"); local transcribe_result = handle:read("*a");
transcribe_result = transcribe_result:gsub('*HESITATION ', ''); transcribe_result = transcribe_result:gsub('*HESITATION ', '');
handle:close(); handle:close();
if (debug["info"]) then if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] CMD: " .. transcribe_cmd .. "\n"); freeswitch.consoleLog("notice", "[voicemail] CMD: " .. transcribe_cmd .. "\n");
freeswitch.consoleLog("notice", "[voicemail] RESULT: " .. transcribe_result .. "\n"); freeswitch.consoleLog("notice", "[voicemail] RESULT: " .. transcribe_result .. "\n");
end end
--Trancribe request can fail --Trancribe request can fail
if (transcribe_result == '') then if (transcribe_result == '') then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n"); freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n");
return '' return ''
else else
status, transcribe_json = pcall(JSON.decode, transcribe_result); 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 not status then if (transcribe_json["results"] ~= nil) then
if (debug["info"]) then --Transcription
freeswitch.consoleLog("notice", "[voicemail] error decoding watson json\n"); if (transcribe_json["results"][1]["alternatives"][1]["transcript"] ~= nil) then
end transcription = '';
return ''; for key, row in pairs(transcribe_json["results"]) do
end transcription = transcription .. row["alternatives"][1]["transcript"];
end end
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: " .. transcription .. "\n");
if (transcribe_json["results"] ~= nil) then end
--Transcription else
if (transcribe_json["results"][1]["alternatives"][1]["transcript"] ~= nil) then if (debug["info"]) then
transcription = ''; freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n");
for key, row in pairs(transcribe_json["results"]) do end
transcription = transcription .. row["alternatives"][1]["transcript"]; return '';
end end
if (debug["info"]) then --Confidence
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: " .. transcription .. "\n"); if (transcribe_json["results"][1]["alternatives"][1]["confidence"]) then
end if (debug["info"]) then
else freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: " .. transcribe_json["results"][1]["alternatives"][1]["confidence"] .. "\n");
if (debug["info"]) then end
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n"); confidence = transcribe_json["results"][1]["alternatives"][1]["confidence"];
end else
return ''; if (debug["info"]) then
end freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: (null) \n");
--Confidence end
if (transcribe_json["results"][1]["alternatives"][1]["confidence"]) then end
if (debug["info"]) then return transcription;
freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: " .. transcribe_json["results"][1]["alternatives"][1]["confidence"] .. "\n"); else
end if (debug["info"]) then
confidence = transcribe_json["results"][1]["alternatives"][1]["confidence"]; freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: json error \n");
else end
if (debug["info"]) then return '';
freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: (null) \n"); end
end end
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 if (transcribe_provider == "custom") then
local transcription_server = settings:get('voicemail', 'transcription_server', 'text') or ''; local transcription_server = settings:get('voicemail', 'transcription_server', 'text') or '';