[voicemail transcription] fix google v2 (#6946)

* v2 doesn't require to have api_key set
* allow setting setting a path to the application credentials
* check only ones if the file path exists
This commit is contained in:
Ahron Greenberg (agree) 2024-04-15 11:26:44 -04:00 committed by GitHub
parent 0646bcb93e
commit 4435cf5bd0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 54 additions and 62 deletions

View File

@ -2,6 +2,11 @@
if (!function_exists('transcribe')) { if (!function_exists('transcribe')) {
function transcribe ($file_path, $file_name, $file_extension) { function transcribe ($file_path, $file_name, $file_extension) {
//check if the file exists
if (!file_exists($file_path.'/'.$file_name)) {
echo "file not found ".$file_path.'/'.$file_name;
exit;
}
//get the email queue settings //get the email queue settings
$setting = new settings(['category' => 'voicemail']); $setting = new settings(['category' => 'voicemail']);
@ -23,13 +28,6 @@ if (!function_exists('transcribe')) {
} }
if (isset($api_key) && $api_key != '') { if (isset($api_key) && $api_key != '') {
//check if the file exists
if (!file_exists($file_path.'/'.$file_name)) {
echo "file not found ".$file_path.'/'.$file_name;
exit;
}
//start output buffer //start output buffer
ob_start(); ob_start();
$out = fopen('php://output', 'w'); $out = fopen('php://output', 'w');
@ -122,6 +120,7 @@ if (!function_exists('transcribe')) {
if ($transcribe_provider == 'google') { if ($transcribe_provider == 'google') {
$api_key = $setting->get('voicemail', 'google_key'); $api_key = $setting->get('voicemail', 'google_key');
$api_url = $setting->get('voicemail', 'google_url'); $api_url = $setting->get('voicemail', 'google_url');
$application_credentials = $setting->get('voicemail', 'google_application_credentials');
$transcribe_language = $setting->get('voicemail', 'transcribe_language'); $transcribe_language = $setting->get('voicemail', 'transcribe_language');
$transcribe_alternate_language = $setting->get('voicemail', 'transcribe_alternate_language'); $transcribe_alternate_language = $setting->get('voicemail', 'transcribe_alternate_language');
@ -138,21 +137,21 @@ if (!function_exists('transcribe')) {
$content_type = 'audio/wav'; $content_type = 'audio/wav';
} }
if (isset($api_key) && $api_key != '') {
//$command = "curl -X POST -silent -u \"apikey:".$api_key."\" --header \"Content-type: ".$content_type."\" --data-binary @".$file_path."/".$file_name." \"".$api_url."\"";
//echo "command: ".$command."\n";
//version 1 //version 1
if (substr($api_url, 0, 32) == 'https://speech.googleapis.com/v1') { if (substr($api_url, 0, 32) == 'https://speech.googleapis.com/v1') {
if (isset($api_key) && $api_key != '') {
$command = "sox ".$file_path."/".$file_name." ".$file_path."/".$file_name.".flac trim 0 00:59 "; $command = "sox ".$file_path."/".$file_name." ".$file_path."/".$file_name.".flac trim 0 00:59 ";
$command .= "&& echo \"{ 'config': { 'languageCode': '".$transcribe_language."', 'enableWordTimeOffsets': false , 'enableAutomaticPunctuation': true , 'alternativeLanguageCodes': '".$transcribe_alternate_language."' }, 'audio': { 'content': '`base64 -w 0 ".$file_path."/".$file_name.".flac`' } }\" "; $command .= "&& echo \"{ 'config': { 'languageCode': '".$transcribe_language."', 'enableWordTimeOffsets': false , 'enableAutomaticPunctuation': true , 'alternativeLanguageCodes': '".$transcribe_alternate_language."' }, 'audio': { 'content': '`base64 -w 0 ".$file_path."/".$file_name.".flac`' } }\" ";
$command .= "| curl -X POST -H \"Content-Type: application/json\" -d @- ".$api_url.":recognize?key=".$api_key." "; $command .= "| curl -X POST -H \"Content-Type: application/json\" -d @- ".$api_url.":recognize?key=".$api_key." ";
$command .= "&& rm -f ".$file_path."/".$file_name.".flac"; $command .= "&& rm -f ".$file_path."/".$file_name.".flac";
echo $command."\n"; echo $command."\n";
} }
}
//version 2 //version 2
if (substr($api_url, 0, 32) == 'https://speech.googleapis.com/v2') { elseif (substr($api_url, 0, 32) == 'https://speech.googleapis.com/v2') {
if (!empty(($application_credentials))) {
putenv("GOOGLE_APPLICATION_CREDENTIALS=".$application_credentials);
}
$command = "echo \"{ 'config': { 'auto_decoding_config': {}, 'language_codes': ['".$transcribe_language."'], 'model': 'long' }, 'content': '`base64 -w 0 ".$file_path."/".$file_name."`' } \" "; $command = "echo \"{ 'config': { 'auto_decoding_config': {}, 'language_codes': ['".$transcribe_language."'], 'model': 'long' }, 'content': '`base64 -w 0 ".$file_path."/".$file_name."`' } \" ";
$command .= "| curl -X POST -H \"Content-Type: application/json\" -H \"Authorization: Bearer \$(gcloud auth application-default print-access-token)\" -d @- ".$api_url; $command .= "| curl -X POST -H \"Content-Type: application/json\" -H \"Authorization: Bearer \$(gcloud auth application-default print-access-token)\" -d @- ".$api_url;
echo $command."\n"; echo $command."\n";
@ -193,7 +192,6 @@ if (!function_exists('transcribe')) {
return $array; return $array;
} }
}
//transcribe - azure //transcribe - azure
if ($transcribe_provider == 'azure') { if ($transcribe_provider == 'azure') {
@ -340,12 +338,6 @@ if (!function_exists('transcribe')) {
$full_file_name = $file_path.'/'.$file_name ; $full_file_name = $file_path.'/'.$file_name ;
//check if the file exists
if (!file_exists($full_file_name)) {
echo "file not found ".$full_file_name;
exit;
}
//start output buffer //start output buffer
ob_start(); ob_start();
$out = fopen('php://output', 'w'); $out = fopen('php://output', 'w');