Browse Source

Initial commit for Transcribe

FusionPBX 1 năm trước cách đây
mục cha
commit
e1b7278a3b

+ 40 - 0
app_config.php

@@ -0,0 +1,40 @@
+<?php
+
+	//application details
+		$apps[$x]['name'] = 'Transcribe';
+		$apps[$x]['uuid'] = '8da245ba-e559-4094-9862-4bfaf5cec713';
+		$apps[$x]['category'] = 'API';
+		$apps[$x]['subcategory'] = '';
+		$apps[$x]['version'] = '1.0';
+		$apps[$x]['license'] = 'Mozilla Public License 1.1';
+		$apps[$x]['url'] = 'http://www.fusionpbx.com';
+		$apps[$x]['description']['en-us'] = 'Speech to Text';
+
+	//default settings
+		$y=0;
+		$apps[$x]['default_settings'][$y]['default_setting_uuid'] = "bc054920-5877-4695-9885-9c9009a7713c";
+		$apps[$x]['default_settings'][$y]['default_setting_category'] = "transcribe";
+		$apps[$x]['default_settings'][$y]['default_setting_subcategory'] = "enabled";
+		$apps[$x]['default_settings'][$y]['default_setting_name'] = "boolean";
+		$apps[$x]['default_settings'][$y]['default_setting_value'] = "true";
+		$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "false";
+		$apps[$x]['default_settings'][$y]['default_setting_description'] = "Speech to Text API enabled.";
+		$y++;
+		$apps[$x]['default_settings'][$y]['default_setting_uuid'] = "72b9feeb-b21c-4dad-ad26-dde86955d87b";
+		$apps[$x]['default_settings'][$y]['default_setting_category'] = "transcribe";
+		$apps[$x]['default_settings'][$y]['default_setting_subcategory'] = "engine";
+		$apps[$x]['default_settings'][$y]['default_setting_name'] = "text";
+		$apps[$x]['default_settings'][$y]['default_setting_value'] = "openai";
+		$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "false";
+		$apps[$x]['default_settings'][$y]['default_setting_description'] = "Speech to Text API engine.";
+		$y++;
+		$apps[$x]['default_settings'][$y]['default_setting_uuid'] = "7883f9fc-9259-4f9b-a73d-532f44db2a28";
+		$apps[$x]['default_settings'][$y]['default_setting_category'] = "transcribe";
+		$apps[$x]['default_settings'][$y]['default_setting_subcategory'] = "api_key";
+		$apps[$x]['default_settings'][$y]['default_setting_name'] = "text";
+		$apps[$x]['default_settings'][$y]['default_setting_value'] = "";
+		$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "false";
+		$apps[$x]['default_settings'][$y]['default_setting_description'] = "Speech to Text API key.";
+		$y++;
+
+?>

+ 80 - 0
resources/interfaces/classes/transcribe.php

@@ -0,0 +1,80 @@
+<?php
+
+/**
+ * transcribe class
+ *
+ * @method null download
+ */
+if (!class_exists('transcribe')) {
+	class transcribe {
+
+		/**
+		 * declare private variables
+		 */
+		private $api_key;
+
+		/** @var string $engine */
+		private $engine;
+
+		/** @var template_engine $object */
+		private $transcribe_object;
+
+		private $settings;
+
+		public $audio_path;
+		public $audio_filename;
+		public $audio_format;
+		public $audio_model;
+		public $audio_voice;
+		public $audio_language;
+		public $audio_message;
+
+		/**
+		 * called when the object is created
+		 */
+		public function __construct(settings $settings = null) {
+			//make the setting object
+			if ($settings === null) {
+				$settings = new settings();
+			}
+
+			//add the settings object to the class
+			$this->settings = $settings;
+
+			//build the setting object and get the recording path
+			$this->api_key = $settings->get('transcribe', 'api_key');
+			$this->engine = $settings->get('transcribe', 'engine');
+		}
+
+		/**
+		 * transcribe - speech to text
+		 */
+		public function transcribe() : string {
+
+			if (!empty($this->engine)) {
+				//set the class interface to use the _template suffix
+				$classname = 'transcribe_'.$this->engine;
+
+				//create the object
+				$object = new $classname($this->settings);
+
+				//ensure the class has implemented the audio_interface interface
+				if ($object instanceof transcribe_interface) {
+					if ($object->is_language_enabled() && !empty($this->audio_language)) {
+						$object->set_language($this->audio_language);
+					}
+					$object->set_path($this->audio_path);
+					$object->set_filename($this->audio_filename);
+					return $object->transcribe();
+				}
+				else {
+					return '';
+				}
+			}
+
+		}
+
+	}
+}
+
+?>

+ 215 - 0
resources/interfaces/classes/transcribe_openai.php

@@ -0,0 +1,215 @@
+<?php
+
+
+ /**
+ * transcribe_openai class
+ *
+ * @method null download
+ */
+if (!class_exists('transcribe_openai')) {
+	class transcribe_openai implements transcribe_interface {
+
+		/**
+		 * declare private variables
+		 */
+		private $api_key;
+		private $path;
+		private $filename;
+		private $format;
+		private $voice;
+		private $message;
+		private $model;
+
+		/**
+		 * called when the object is created
+		 */
+		public function __construct($settings) {
+
+			//build the setting object and get the recording path
+			$this->api_key = $settings->get('transcribe', 'api_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_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 is_language_enabled() : bool {
+			//return the whether engine is handles languages
+			return false;
+		}
+
+		public function is_translate_enabled() : bool {
+			//return the whether engine is able to translate
+			return false;
+		}
+
+		public function get_voices() : array {
+			$voices = array(
+				"alloy" => "alloy",
+				"echo" => "echo",
+				"fable" => "fable",
+				"nova" => "nova",
+				"onyx" => "onyx",
+				"shimmer" => "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;
+		}
+
+		/**
+		 * 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->api_key,
+				'Content-Type: multipart/form-data'
+			));
+
+			// set the POST data
+			$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
+			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);
+			}
+		}
+
+		public function set_model(string $model): void {
+			if (array_key_exists($model, $this->get_models())) {
+				$this->model = $model;
+			}
+		}
+
+		public function get_models(): array {
+			return [
+				'tts-1-hd' => 'tts-1-hd'
+			];
+		}
+
+	}
+}
+
+?>

+ 14 - 0
resources/interfaces/transcribe_interface.php

@@ -0,0 +1,14 @@
+<?php
+
+//define the template class
+if (!interface_exists('transcribe_interface')) {
+	interface transcribe_interface {
+		public function set_path(string $audio_path);
+		public function set_filename(string $audio_filename);
+		public function transcribe() : string;
+		public function set_language(string $audio_language);
+		public function get_languages() : array;
+	}
+}
+
+?>