Selfhosted Transcription Service (#3753)
* Added a simple selfhosted voicemail transcription API client * Minor logging cleanup * Add some string escaping to handle transcription providers * Add JSON handling (optional) to selfhosted API
This commit is contained in:
parent
c52e2d64a9
commit
c452b9417f
|
|
@ -38,6 +38,22 @@
|
|||
end)
|
||||
end
|
||||
|
||||
--define escape function (prevents lua injection attacks)
|
||||
local function esc(x)
|
||||
return (x:gsub('%%', '%%%%')
|
||||
:gsub('^%^', '%%^')
|
||||
:gsub('%$$', '%%$')
|
||||
:gsub('%(', '%%(')
|
||||
:gsub('%)', '%%)')
|
||||
:gsub('%.', '%%.')
|
||||
:gsub('%[', '%%[')
|
||||
:gsub('%]', '%%]')
|
||||
:gsub('%*', '%%*')
|
||||
:gsub('%+', '%%+')
|
||||
:gsub('%-', '%%-')
|
||||
:gsub('%?', '%%?'))
|
||||
end
|
||||
|
||||
local function transcribe(file_path,settings,start_epoch)
|
||||
--transcription variables
|
||||
if (os.time() - start_epoch > 2) then
|
||||
|
|
@ -105,6 +121,40 @@
|
|||
return transcription;
|
||||
end
|
||||
end
|
||||
if (transcribe_provider == "selfhosted") 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");
|
||||
|
|
|
|||
Loading…
Reference in New Issue