From 3a0a7b48f1a9d08e9ee566986543cd7d129cd01c Mon Sep 17 00:00:00 2001 From: Andrew Alexander Date: Thu, 14 Dec 2023 19:39:11 -0600 Subject: [PATCH] openai transcription support (#6850) * openai transcription support * Update transcribe.php * Update transcribe.php --- .../resources/functions/transcribe.php | 86 ++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/app/email_queue/resources/functions/transcribe.php b/app/email_queue/resources/functions/transcribe.php index ad4bddfc75..721201f21c 100644 --- a/app/email_queue/resources/functions/transcribe.php +++ b/app/email_queue/resources/functions/transcribe.php @@ -318,7 +318,91 @@ if (!function_exists('transcribe')) { return $array; } + //transcribe - openai + // settings: + // openai_key (required) + // openai_url + // openai_model + if ($transcribe_provider == 'openai') { + $api_key = $setting->get('voicemail', 'openai_key'); + $api_url = $setting->get('voicemail', 'openai_url'); + $api_voice_model = $setting->get('voicemail', 'openai_model'); + + if (empty($api_url)) { + $api_url = "https://api.openai.com/v1/audio/transcriptions"; + } + + if (empty($api_voice_model)) { + $api_voice_model = "whisper-1"; + } + + if (isset($api_key) && $api_key != '') { + + $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 + ob_start(); + $out = fopen('php://output', 'w'); + + //create the curl resource + $ch = curl_init(); + + $post_data = array( + 'model'=>$api_voice_model, + 'file'=>curl_file_create($full_file_name) + ); + + //set the curl options + curl_setopt_array($ch, array( + CURLOPT_URL =>$api_url, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_SSL_VERIFYPEER => TRUE, + CURLOPT_HTTPHEADER => array('Authorization: Bearer '.$api_key), + CURLOPT_POSTFIELDS => $post_data, + + )); + + // //add verbose for debugging + // curl_setopt($ch, CURLOPT_VERBOSE, true); + curl_setopt($ch, CURLOPT_STDERR, $out); + + //execute the curl with the options + $http_content = curl_exec($ch); + + //return the error + if (curl_errno($ch)) { + echo 'Error:' . curl_error($ch); + } + + //close the curl resource + curl_close($ch); + + //show the debug information + fclose($out); + $debug = ob_get_clean(); + echo $debug; + + + $ob = json_decode($http_content, true); + + $message = $ob['text']; + return array( + 'provider' => $transcribe_provider, + 'message' => $message + ); + } + + } + // todo: add error checking + // return array('message' => "Missing valid transcribe_provider"; + } } - + ?>