openai transcription support (#6850)

* openai transcription support

* Update transcribe.php

* Update transcribe.php
This commit is contained in:
Andrew Alexander
2023-12-14 19:39:11 -06:00
committed by GitHub
parent 7a0aa3c4e0
commit 3a0a7b48f1

View File

@@ -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";
}
}
?>