Remove the transcription as this can blocking.

Transcription still available through email queue this way it offloads the work off of FreeSWITCH.
This commit is contained in:
FusionPBX
2022-05-24 16:32:40 -06:00
committed by GitHub
parent 2326d79113
commit 46447d83c3

View File

@@ -54,338 +54,6 @@
:gsub('%?', '%%?'))
end
local function transcribe(file_path,settings,start_epoch)
--transcription variables
if (os.time() - start_epoch > 2) then
local transcribe_provider = settings:get('voicemail', 'transcribe_provider', 'text') or '';
transcribe_language = settings:get('voicemail', 'transcribe_language', 'text') or 'en-US';
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] transcribe_provider: " .. transcribe_provider .. "\n");
freeswitch.consoleLog("notice", "[voicemail] transcribe_language: " .. transcribe_language .. "\n");
end
if (transcribe_provider == "microsoft") then
local api_key1 = settings:get('voicemail', 'microsoft_key1', 'text') or '';
local api_key2 = settings:get('voicemail', 'microsoft_key2', 'text') or '';
if (api_key1 ~= '' and api_key2 ~= '') then
access_token_cmd = "curl -X POST \"https://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 access_token_result = handle:read("*a");
handle:close();
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] CMD: " .. access_token_cmd .. "\n");
freeswitch.consoleLog("notice", "[voicemail] RESULT: " .. access_token_result .. "\n");
end
--Access token request can fail
if (access_token_result == '') then
freeswitch.consoleLog("notice", "[voicemail] ACCESS TOKEN: (null) \n");
return ''
end
transcribe_cmd = "curl -X POST \"https://speech.platform.bing.com/recognize?scenarios=smd&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5&locale=" .. transcribe_language .. "&device.os=Freeswitch&version=3.0&format=json&instanceid=" .. gen_uuid() .. "&requestid=" .. gen_uuid() .. "\" -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 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 bing json\n");
end
return '';
end
end
if (debug["info"]) then
if (transcribe_json["results"][1]["name"] == nil) then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n");
else
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: " .. transcribe_json["results"][1]["name"] .. "\n");
end
if (transcribe_json["results"][1]["confidence"] == nil) then
freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: (null) \n");
else
freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: " .. transcribe_json["results"][1]["confidence"] .. "\n");
end
end
transcription = transcribe_json["results"][1]["name"];
transcription = transcription:gsub("<profanity>.*<%/profanity>","...");
confidence = transcribe_json["results"][1]["confidence"];
return transcription;
end
end
if (transcribe_provider == "azure") then
local api_key1 = settings:get('voicemail', 'azure_key1', 'text') or '';
local api_server_region = settings:get('voicemail', 'azure_server_region', 'text') or '';
if (api_server_region ~= '') then
api_server_region = api_server_region .. ".";
else
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] azure_server_region default setting must be set\n");
end
return '';
end
if (api_key1 ~= '') then
-- search in memcache first, azure documentation claims that the access token is valid for 10 minutes
local cache = require "resources.functions.cache";
local key = "app:voicemail:azure:access_token";
local access_token_result = cache.get(key)
if access_token_result then
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] Azure access_token recovered from memcached\n");
end
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.."\"";
local handle = io.popen(access_token_cmd);
access_token_result = handle:read("*a");
handle:close();
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] CMD: " .. access_token_cmd .. "\n");
freeswitch.consoleLog("notice", "[voicemail] ACCESS TOKEN: " .. access_token_result .. "\n");
end
--Access token request can fail
if (access_token_result == '') then
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] ACCESS TOKEN: (null) \n");
end
return ''
end
--Azure returns JSON when it has to report an error
if (string.sub(access_token_result, 1, 1) == '{') then
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] ERROR STRING: ".. access_token_result .. "\n");
end
return ''
end
cache.set(key, access_token_result, 120);
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] Azure access_token saved into memcached: " .. access_token_result .. "\n");
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
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 ''
end
local transcribe_json = JSON.decode(transcribe_result);
if (debug["info"]) then
if (transcribe_json["NBest"][1]["Display"] == nil) then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n");
else
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: " .. transcribe_json["NBest"][1]["Display"] .. "\n");
end
if (transcribe_json["NBest"][1]["Confidence"] == nil) then
freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: (null) \n");
else
freeswitch.consoleLog("notice", "[voicemail] CONFIDENCE: " .. transcribe_json["NBest"][1]["Confidence"] .. "\n");
end
end
transcription = transcribe_json["NBest"][1]["Display"];
confidence = transcribe_json["NBest"][1]["Confidence"];
return transcription;
end
end
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
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..[[" ]]
else
transcribe_cmd = [[ curl -X POST -u "apikey:]]..api_key..[[" --header "Content-type: audio/wav" --data-binary @]]..file_path..[[ "]]..transcription_server..[[" ]]
end
local handle = io.popen(transcribe_cmd);
local transcribe_result = handle:read("*a");
transcribe_result = transcribe_result:gsub('%%HESITATION ', '');
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 == "google") then
local api_key = settings:get('voicemail', 'google_key', 'text') or '';
local transcription_server = settings:get('voicemail', 'google_url', 'text') or '';
transcribe_alternate_language = settings:get('voicemail', 'transcribe_alternate_language', 'text') or 'es-US';
if (api_key ~= '') then
transcribe_cmd = [[sox ]]..file_path..[[ ]]..file_path..[[.flac trim 0 00:59 && echo "{ 'config': { 'languageCode': ']]..transcribe_language..[[', 'enableWordTimeOffsets': false , 'enableAutomaticPunctuation': true , 'alternativeLanguageCodes': ']]..transcribe_alternate_language..[[' }, 'audio': { 'content': '`base64 -w 0 ]]..file_path..[[.flac`' } }" | curl -X POST -H "Content-Type: application/json" -d @- "]]..transcription_server..[[:recognize?key=]]..api_key..[[" && rm -f ]]..file_path..[[.flac]]
end
local handle = io.popen(transcribe_cmd);
local transcribe_result = handle:read("*a");
transcribe_result = transcribe_result:gsub('%%HESITATION ', '');
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 google 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
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 '';
local json_enabled = settings:get('voicemail', 'json_enabled', 'boolean') or "false";
if (transcription_server ~= '') then
transcribe_cmd = "curl -X POST " .. transcription_server .. " -H 'Authorization: Bearer " .. api_key .. "' -F file=@"..file_path
local handle = io.popen(transcribe_cmd);
local transcribe_result = esc(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 ''
end
if (json_enabled == "true") then
local transcribe_json = JSON.decode(transcribe_result);
if (transcribe_json["message"] == nil) then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: " .. transcribe_result .. "\n");
transcribe_result = '';
end
if (transcribe_json["error"] ~= nil) then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: " .. transcribe_result .. "\n");
transcribe_result = '';
end
transcribe_result = transcribe_json["message"];
end
return transcribe_result;
end
end
else
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] message too short for transcription.\n");
end
end
return '';
end
--save the recording
function record_message()
@@ -396,14 +64,6 @@
local message_silence_threshold = settings:get('voicemail', 'message_silence_threshold', 'numeric') or 200;
local message_silence_seconds = settings:get('voicemail', 'message_silence_seconds', 'numeric') or 3;
local email_queue_enabled = settings:get('email_queue', 'enabled', 'boolean') or "false";
local transcribe_provider = settings:get('voicemail', 'transcribe_provider', 'text') or '';
transcribe_enabled = settings:get('voicemail', 'transcribe_enabled', 'boolean') or "false";
--debug information
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] transcribe_enabled: " .. transcribe_enabled .. "\n");
freeswitch.consoleLog("notice", "[voicemail] voicemail_transcription_enabled: " .. voicemail_transcription_enabled .. "\n");
end
--record your message at the tone press any key or stop talking to end the recording
if (skip_instructions == "true") then
@@ -527,11 +187,6 @@
--start epoch
start_epoch = os.time();
--if using the email queue disable inline transcription
if (email_queue_enabled == 'true') then
transcribe_enabled = 'false';
end
--save the recording
-- syntax is session:recordFile(file_name, max_len_secs, silence_threshold, silence_secs)
if (storage_path == "http_cache") then
@@ -546,13 +201,9 @@
elseif (shout_exists == "true" and transcribe_enabled == "true" and voicemail_transcription_enabled == "true" and transcribe_provider == "watson") then
--record in mp3 directly with mp3 transcription if watson selected
result = session:recordFile(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".mp3", message_max_length, message_silence_threshold, message_silence_seconds);
transcription = transcribe(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".mp3",settings,start_epoch);
else
--create initial wav recording
result = session:recordFile(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".wav", message_max_length, message_silence_threshold, message_silence_seconds);
if (transcribe_enabled == "true" and voicemail_transcription_enabled == "true") then
transcription = transcribe(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".wav",settings,start_epoch);
end
--use lame to encode, if available
if (file_exists("/usr/bin/lame")) then
freeswitch.consoleLog("notice", "using lame for mp3 encoding\n");
@@ -572,9 +223,6 @@
end
else
result = session:recordFile(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid.."."..vm_message_ext, message_max_length, message_silence_threshold, message_silence_seconds);
if (transcribe_enabled == "true" and voicemail_transcription_enabled == "true") then
transcription = transcribe(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid.."."..vm_message_ext,settings,start_epoch);
end
end
end