From 7868073b73829909277f04f8349f62370e12dcf9 Mon Sep 17 00:00:00 2001 From: FusionPBX Date: Fri, 22 Mar 2024 23:14:12 -0600 Subject: [PATCH] Add ai classe and interfaces to the core --- core/ai/resources/classes/ai.php | 110 ++++++++++++ core/ai/resources/classes/ai_openai.php | 160 ++++++++++++++++++ core/ai/resources/interfaces/ai_speech.php | 15 ++ .../ai/resources/interfaces/ai_transcribe.php | 12 ++ 4 files changed, 297 insertions(+) create mode 100644 core/ai/resources/classes/ai.php create mode 100644 core/ai/resources/classes/ai_openai.php create mode 100644 core/ai/resources/interfaces/ai_speech.php create mode 100644 core/ai/resources/interfaces/ai_transcribe.php diff --git a/core/ai/resources/classes/ai.php b/core/ai/resources/classes/ai.php new file mode 100644 index 0000000000..6e068f73c1 --- /dev/null +++ b/core/ai/resources/classes/ai.php @@ -0,0 +1,110 @@ +setting = $setting; + + //build the setting object and get the recording path + $this->transcribe_key = $setting->get('audio', 'transcribe_key'); + $this->transcribe_engine = $setting->get('audio', 'transcribe_engine'); + $this->speech_key = $setting->get('audio', 'speech_key'); + $this->speech_engine = $setting->get('audio', 'speech_engine'); + } + + /** + * speech - text to speech + */ + public function speech() { + if (!empty($this->speech_engine)) { + //set the class interface to use the _template suffix + $classname = 'audio_'.$this->speech_engine; + + //load the class + //require_once $classname . '.php'; + + //create the object + $object = new $classname($this->setting); + + //ensure the class has implemented the audio_interface interface + if ($object instanceof audio_interface) { + $object->set_path($this->audio_path); + $object->set_filename($this->audio_filename); + $object->set_format($this->audio_format); + $object->set_voice($this->audio_voice); + $object->set_message($this->audio_message); + $object->speech(); + } + else { + return false; + } + } + } + + /** + * transcribe - speech to text + */ + public function transcribe() : string { + + if (!empty($this->transcribe_engine)) { + //set the class interface to use the _template suffix + $classname = 'audio_'.$this->transcribe_engine; + + //load the class + //require_once $classname . '.php'; + + //create the object + $object = new $classname($this->setting); + //ensure the class has implemented the audio_interface interface + if ($object instanceof audio_interface) { + $object->set_path($this->audio_path); + $object->set_filename($this->audio_filename); + return $object->transcribe(); + } + else { + return ''; + } + } + + } + + } +} + +?> \ No newline at end of file diff --git a/core/ai/resources/classes/ai_openai.php b/core/ai/resources/classes/ai_openai.php new file mode 100644 index 0000000000..4b56eb74eb --- /dev/null +++ b/core/ai/resources/classes/ai_openai.php @@ -0,0 +1,160 @@ +transcribe_key = $setting->get('audio', 'transcribe_key'); + $this->speech_key = $setting->get('audio', 'speech_key'); + + } + + public function set_path(string $audio_path) { + $this->path = $audio_path; + } + + public function set_filename(string $audio_filename) { + $this->filename = $audio_filename; + } + + public function set_format(string $audio_format) { + $this->format = $audio_format; + } + + public function set_voice(string $audio_voice) { + $this->voice = $audio_voice; + } + + public function set_message(string $audio_message) { + $this->message = $audio_message; + } + + /** + * speech - text to speech + */ + public function speech() : bool { + + // set the request URL + $url = 'https://api.openai.com/v1/audio/speech'; + + // set the request headers + $headers = [ + 'Authorization: Bearer ' . $this->speech_key, + 'Content-Type: application/json' + ]; + + // Set the request data format, wav, mp3, opus + $data = [ + 'model' => 'tts-1-hd', + 'input' => $this->message, + 'voice' => $this->voice, + 'response_format' => 'wav' + ]; + + // initialize curl handle + $ch = curl_init($url); + + // set the curl options + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); + + // run the curl request and get the response + $response = curl_exec($ch); + + // close the handle + curl_close($ch); + + // check for errors + if ($response === false) { + return false; + } + else { + // save the audio file + file_put_contents($this->path.'/'.$this->filename, $response); + return true; + } + + } + + /** + * transcribe - speech to text + */ + public function transcribe() : string { + // initialize a curl handle + $ch = curl_init(); + + // set the URL for the request + curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/audio/transcriptions'); + + // set the request method to POST + curl_setopt($ch, CURLOPT_POST, true); + + // set the request headers + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Authorization: Bearer '.$this->transcribe_key, + 'Content-Type: multipart/form-data' + )); + + // set the POST data + $post_data = array( + 'file' => new CURLFile($this->path.'/'.$this->filename), + 'model' => 'whisper-1', + 'response_format' => 'text' + ); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); + + // return the response as a string instead of outputting it directly + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + // run the curl request and transcription message + $this->message = curl_exec($ch); + + // check for errors + if (curl_errno($ch)) { + echo 'Error: ' . curl_error($ch); + exit; + } + + // close the handle + curl_close($ch); + + // return the transcription + if (empty($this->message)) { + return ''; + } + else { + return trim($this->message); + } + } + + } +} + +?> \ No newline at end of file diff --git a/core/ai/resources/interfaces/ai_speech.php b/core/ai/resources/interfaces/ai_speech.php new file mode 100644 index 0000000000..d3c65257cb --- /dev/null +++ b/core/ai/resources/interfaces/ai_speech.php @@ -0,0 +1,15 @@ + \ No newline at end of file diff --git a/core/ai/resources/interfaces/ai_transcribe.php b/core/ai/resources/interfaces/ai_transcribe.php new file mode 100644 index 0000000000..ef16c4c99e --- /dev/null +++ b/core/ai/resources/interfaces/ai_transcribe.php @@ -0,0 +1,12 @@ + \ No newline at end of file