Преглед изворни кода

Update the new ai class
- Add voices and languages
- Change classname audio_ to ai_

FusionPBX пре 1 година
родитељ
комит
6c8b94b879
2 измењених фајлова са 194 додато и 36 уклоњено
  1. 80 18
      core/ai/resources/classes/ai.php
  2. 114 18
      core/ai/resources/classes/ai_openai.php

+ 80 - 18
core/ai/resources/classes/ai.php

@@ -22,30 +22,92 @@ if (!class_exists('ai')) {
 		private $transcribe_object;
 		private $speech_object;
 
-		private $setting;
+		private $settings;
 
 		public $audio_path;
 		public $audio_filename;
 		public $audio_format;
 		public $audio_voice;
+		public $audio_language;
 		public $audio_message;
 
 		/**
 		 * called when the object is created
 		 */
-		public function __construct(settings $setting = null) {
+		public function __construct(settings $settings = null) {
 			//make the setting object
-			if ($setting === null) {
-				$setting = new settings();
+			if ($settings === null) {
+				$settings = new settings();
 			}
 
-			$this->setting = $setting;
+			//add the settings object to the class
+			$this->settings = $settings;
 
 			//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');
+			$this->transcribe_key = $settings->get('ai', 'transcribe_key');
+			$this->transcribe_engine = $settings->get('ai', 'transcribe_engine');
+			$this->speech_key = $settings->get('ai', 'speech_key');
+			$this->speech_engine = $settings->get('ai', 'speech_engine');
+		}
+
+		/**
+		 * get_voices - get the list voices
+		 */
+		public function get_voices() : array {
+
+			//set the class interface to use the _template suffix
+			$classname = 'ai_'.$this->speech_engine;
+
+			//create the object
+			$object = new $classname($this->settings);
+
+			//return the voices array
+			return $object->get_voices();
+		}
+
+		/**
+		 * get_translate_enabled - get whether the engine can do translations
+		 */
+		public function get_translate_enabled() : bool {
+
+			//set the class interface to use the _template suffix
+			$classname = 'ai_'.$this->speech_engine;
+
+			//create the object
+			$object = new $classname($this->settings);
+
+			//return the translate_enabled
+			return $object->get_translate_enabled();
+		}
+
+		/**
+		 * get_language_enabled - get whether the engine allows to set the language
+		 */
+		public function get_language_enabled() : bool {
+
+			//set the class interface to use the _template suffix
+			$classname = 'ai_'.$this->speech_engine;
+
+			//create the object
+			$object = new $classname($this->settings);
+
+			//return the language_enabled
+			return $object->get_language_enabled();
+		}
+
+		/**
+		 * get_languages - get the list languages
+		 */
+		public function get_languages() : array {
+
+			//set the class interface to use the _template suffix
+			$classname = 'ai_'.$this->speech_engine;
+
+			//create the object
+			$object = new $classname($this->settings);
+
+			//return the languages array
+			return $object->get_languages();
 		}
 
 		/**
@@ -54,20 +116,19 @@ if (!class_exists('ai')) {
 		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';
+				$classname = 'ai_'.$this->speech_engine;
 
 				//create the object
-				$object = new $classname($this->setting);
+				$object = new $classname($this->settings);
 
 				//ensure the class has implemented the audio_interface interface
-				if ($object instanceof audio_interface) {
+				if ($object instanceof ai_speech) {
 					$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_language($this->audio_language);
+					//$object->set_translate($this->audio_translate);
 					$object->set_message($this->audio_message);
 					$object->speech();
 				}
@@ -84,15 +145,16 @@ if (!class_exists('ai')) {
 
 			if (!empty($this->transcribe_engine)) {
 				//set the class interface to use the _template suffix
-				$classname = 'audio_'.$this->transcribe_engine;
+				$classname = 'ai_'.$this->transcribe_engine;
 
 				//load the class
 				//require_once $classname . '.php';
 
 				//create the object
-				$object = new $classname($this->setting);
+				$object = new $classname($this->settings);
 				//ensure the class has implemented the audio_interface interface
-				if ($object instanceof audio_interface) {
+				if ($object instanceof ai_transcribe) {
+					$object->set_language($this->audio_language);
 					$object->set_path($this->audio_path);
 					$object->set_filename($this->audio_filename);
 					return $object->transcribe();

+ 114 - 18
core/ai/resources/classes/ai_openai.php

@@ -22,15 +22,11 @@ if (!class_exists('ai_openai')) {
 		/**
 		 * called when the object is created
 		 */
-		public function __construct($setting) {
-			//make the setting object
-			if (!$setting) {
-				$setting = new settings();
-			}
+		public function __construct($settings) {
 
 			//build the setting object and get the recording path
-			$this->transcribe_key = $setting->get('audio', 'transcribe_key');
-			$this->speech_key = $setting->get('audio', 'speech_key');
+			$this->transcribe_key = $settings->get('ai', 'transcribe_key');
+			$this->speech_key = $settings->get('ai', 'speech_key');
 
 		}
 
@@ -50,10 +46,108 @@ if (!class_exists('ai_openai')) {
 			$this->voice = $audio_voice;
 		}
 
+		public function set_language(string $audio_language) {
+			$this->language = $audio_language;
+		}
+
+		public function set_translate(string $audio_translate) {
+			$this->translate = $audio_translate;
+		}
+
 		public function set_message(string $audio_message) {
 			$this->message = $audio_message;
 		}
 
+		public function get_language_enabled() : bool {
+			//return the whether engine is handles languages
+			return false;
+		}
+
+		public function get_translate_enabled() : bool {
+			//return the whether engine is able to translate
+			return false;
+		}
+
+		public function get_voices() : array {
+			$voices = array(
+				"alloy",
+				"echo",
+				"fable",
+				"nova",
+				"onyx",
+				"shimmer"
+			);
+
+			//return the languages array
+			return $voices;
+		}
+
+		public function get_languages() : array {
+			//create the languages array
+			$languages = array(
+				"af" => "Afrikaans",
+				"ar" => "Arabic",
+				"hy" => "Armenian",
+				"az" => "Azerbaijani",
+				"be" => "Belarusian",
+				"bs" => "Bosnian",
+				"bg" => "Bulgarian",
+				"ca" => "Catalan",
+				"zh" => "Chinese",
+				"hr" => "Croatian",
+				"cs" => "Czech",
+				"da" => "Danish",
+				"nl" => "Dutch",
+				"en" => "English",
+				"et" => "Estonian",
+				"fi" => "Finnish",
+				"fr" => "French",
+				"gl" => "Galician",
+				"de" => "German",
+				"el" => "Greek",
+				"he" => "Hebrew",
+				"hi" => "Hindi",
+				"hu" => "Hungarian",
+				"is" => "Icelandic",
+				"id" => "Indonesian",
+				"it" => "Italian",
+				"ja" => "Japanese",
+				"kn" => "Kannada",
+				"kk" => "Kazakh",
+				"ko" => "Korean",
+				"lv" => "Latvian",
+				"lt" => "Lithuanian",
+				"mk" => "Macedonian",
+				"ms" => "Malay",
+				"mr" => "Marathi",
+				"mi" => "Maori",
+				"ne" => "Nepali",
+				"no" => "Norwegian",
+				"fa" => "Persian",
+				"pl" => "Polish",
+				"pt" => "Portuguese",
+				"ro" => "Romanian",
+				"ru" => "Russian",
+				"sr" => "Serbian",
+				"sk" => "Slovak",
+				"sl" => "Slovenian",
+				"es" => "Spanish",
+				"sw" => "Swahili",
+				"sv" => "Swedish",
+				"tl" => "Tagalog",
+				"ta" => "Tamil",
+				"th" => "Thai",
+				"tr" => "Turkish",
+				"uk" => "Ukrainian",
+				"ur" => "Urdu",
+				"vi" => "Vietnamese",
+				"cy" => "Welsh"
+			);
+
+			//return the languages array
+			return $languages;
+		}
+
 		/**
 		 * speech - text to speech
 		 */
@@ -69,12 +163,16 @@ if (!class_exists('ai_openai')) {
 			];
 
 			// Set the request data format, wav, mp3, opus
-			$data = [
-				'model' => 'tts-1-hd',
-				'input' => $this->message,
-				'voice' => $this->voice,
-				'response_format' => 'wav'
-			];
+			$data['model'] = 'tts-1-hd';
+			$data['input'] = $this->message;
+			$data['voice'] = $this->voice;
+			$data['response_format'] = 'wav';
+			if (isset($this->language)) {
+				$data['language'] = $this->language;
+			}
+			if (isset($this->translate)) {
+				$data['task'] = 'translate';
+			}
 
 			// initialize curl handle
 			$ch = curl_init($url);
@@ -123,11 +221,9 @@ if (!class_exists('ai_openai')) {
 			));
 
 			// set the POST data
-			$post_data = array(
-				'file' => new CURLFile($this->path.'/'.$this->filename),
-				'model' => 'whisper-1',
-				'response_format' => 'text'
-			);
+			$post_data['file'] = new CURLFile($this->path.'/'.$this->filename);
+			$post_data['model'] = 'whisper-1';
+			$post_data['response_format'] = 'text';
 			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
 
 			// return the response as a string instead of outputting it directly