update custom transcription provider for async transcriptions and retry logic (#6583)
Co-authored-by: Finn <finn@acceleratenetworks.com>
This commit is contained in:
parent
5e23824940
commit
8cdcd86ba9
|
|
@ -223,9 +223,8 @@ if (!function_exists('transcribe')) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//transcribe - custom
|
// transcribe - custom
|
||||||
//Works with Mozilla DeepSpeech or Coqui with https://github.com/AccelerateNetworks/DeepSpeech_Frontend
|
// Works with self-hostable transcription service at https://github.com/AccelerateNetworks/an-transcriptions
|
||||||
//or Vosk with https://git.callpipe.com/fusionpbx/vosk_frontend
|
|
||||||
if ($transcribe_provider == 'custom') {
|
if ($transcribe_provider == 'custom') {
|
||||||
$api_key = $_SESSION['voicemail']['api_key']['text'];
|
$api_key = $_SESSION['voicemail']['api_key']['text'];
|
||||||
$api_url = $_SESSION['voicemail']['transcription_server']['text'];
|
$api_url = $_SESSION['voicemail']['transcription_server']['text'];
|
||||||
|
|
@ -241,21 +240,63 @@ if (!function_exists('transcribe')) {
|
||||||
$content_type = 'audio/wav';
|
$content_type = 'audio/wav';
|
||||||
}
|
}
|
||||||
|
|
||||||
$file_path = $file_path.'/'.$file_name;
|
$message = null;
|
||||||
$command = "curl -X POST ".$api_url." -H 'Authorization: Bearer ".$api_key."' -F file=@".$file_path;
|
for($retries = 5; $retries > 0; $retries--) {
|
||||||
echo $command."\n";
|
echo "sending voicemail recording to ".$api_url." for transcription";
|
||||||
$http_response = shell_exec($command);
|
|
||||||
$array = json_decode($http_response, true);
|
// submit the file for transcribing
|
||||||
if ($array === null) {
|
$file_path = $file_path.'/'.$file_name;
|
||||||
|
$command = "curl -sX POST ".$api_url."/enqueue -H 'Authorization: Bearer ".$api_key."' -F file=@".$file_path;
|
||||||
|
$stdout = shell_exec($command);
|
||||||
|
$resp = json_decode($stdout, true);
|
||||||
|
if ($resp === null) {
|
||||||
|
echo "unexpected error: ".$stdout;
|
||||||
|
// json not parsable, try again
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$transcription_id = $resp['id'];
|
||||||
|
|
||||||
|
// wait for transcription to complete
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
echo "checking ".$api_url." for completion of job ".$transcription_id;
|
||||||
|
$command = "curl -s ".$api_url."/j/".$transcription_id." -H 'Authorization: Bearer ".$api_key."'";
|
||||||
|
$resp = json_decode(shell_exec($command), true);
|
||||||
|
if ($resp === null) {
|
||||||
|
// json not parsable, try again
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($resp['status'] == "failed") {
|
||||||
|
echo "transcription failed, retrying";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($resp['status'] == "finished") {
|
||||||
|
echo "transcription succeeded";
|
||||||
|
$message = $resp['result'];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// transcription is queued or in progress, check again in 1 second
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if($message !== null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if($message == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
$message = $array['message'];
|
|
||||||
}
|
|
||||||
$array['provider'] = $transcribe_provider;
|
$array['provider'] = $transcribe_provider;
|
||||||
$array['language'] = $transcribe_language;
|
$array['language'] = $transcribe_language;
|
||||||
$array['api_key'] = $api_key;
|
$array['api_key'] = $api_key;
|
||||||
$array['command'] = $command;
|
// $array['command'] = $command
|
||||||
$array['message'] = $message;
|
$array['message'] = $message;
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue