Procházet zdrojové kódy

Add support for audio string
Usefu when transcribing from a string rather than a file

FusionPBX před 1 rokem
rodič
revize
d17952858d

+ 10 - 1
resources/classes/transcribe.php

@@ -23,6 +23,8 @@ if (!class_exists('transcribe')) {
 
 		public $audio_path;
 		public $audio_filename;
+		public $audio_string;
+		public $audio_mime_type;
 		public $audio_format;
 		public $audio_model;
 		public $audio_voice;
@@ -55,6 +57,10 @@ if (!class_exists('transcribe')) {
 				//set the class interface to use the _template suffix
 				$classname = 'transcribe_'.$this->engine;
 
+				if (empty($this->audio_path)) {
+					$this->audio_path = null;
+				}
+
 				//create the object
 				$object = new $classname($this->settings);
 
@@ -63,6 +69,9 @@ if (!class_exists('transcribe')) {
 					if ($object->is_language_enabled() && !empty($this->audio_language)) {
 						$object->set_language($this->audio_language);
 					}
+					$object->set_audio_string($this->audio_string);
+echo "bbb audio_mime_type: ".$this->audio_mime_type."\n";
+					$object->set_audio_mime_type($this->audio_mime_type);
 					$object->set_path($this->audio_path);
 					$object->set_filename($this->audio_filename);
 					return $object->transcribe();
@@ -77,4 +86,4 @@ if (!class_exists('transcribe')) {
 	}
 }
 
-?>
+?>

+ 33 - 7
resources/classes/transcribe_azure.php

@@ -17,6 +17,8 @@ if (!class_exists('transcribe_azure')) {
 		private $language;
 		private $path;
 		private $filename;
+		private $audio_string;
+		private $audio_mime_type;
 		private $format;
 		private $voice;
 		private $message;
@@ -42,6 +44,14 @@ if (!class_exists('transcribe_azure')) {
 			$this->filename = $audio_filename;
 		}
 
+		public function set_audio_string(string $audio_string) {
+			$this->audio_string = $audio_string;
+		}
+
+		public function set_audio_mime_type(string $audio_mime_type) {
+			$this->audio_mime_type = $audio_mime_type;
+		}
+
 		public function set_format(string $audio_format) {
 			$this->format = $audio_format;
 		}
@@ -144,12 +154,17 @@ if (!class_exists('transcribe_azure')) {
 		public function transcribe() : string {
 
 			//get the content type
-			$path_array = pathinfo($this->path.'/'.$this->filename);
-			if ($path_array['extension'] == "mp3") {
-				$content_type = 'audio/mp3';
+			if (file_exists($this->path.'/'.$this->filename)) {
+				$path_array = pathinfo($this->path.'/'.$this->filename);
+				if ($path_array['extension'] == "mp3") {
+					$content_type = 'audio/mp3';
+				}
+				if ($path_array['extension'] == "wav") {
+					$content_type = 'audio/wav';
+				}
 			}
-			if ($path_array['extension'] == "wav") {
-				$content_type = 'audio/wav';
+			elseif (!empty($this->audio_string)) {
+				$content_type = $this->audio_mime_type;
 			}
 
 			//start output buffer
@@ -204,8 +219,19 @@ if (!class_exists('transcribe_azure')) {
 					//send the http headers
 					curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 					
-					//send the file using 
-					curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file_path));
+					//prepare to send the file or audio
+					if (file_exists($this->path.'/'.$this->filename)) {
+						//send the file using
+						curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file_path));
+					}
+					elseif (!empty($this->audio_string)) {
+						//send the audio from as a string
+						curl_setopt($ch, CURLOPT_POSTFIELDS, $this->audio_string));
+					}
+					else {
+						//audio file or string not found
+						return false;
+					}
 
 					//return the response as a string instead of outputting it directly
 					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

+ 49 - 7
resources/classes/transcribe_google.php

@@ -19,6 +19,8 @@ if (!class_exists('transcribe_google')) {
 		private $application_credentials;
 		private $path;
 		private $filename;
+		private $audio_string;
+		private $audio_mime_type;
 		private $format;
 		private $voice;
 		private $message;
@@ -46,6 +48,14 @@ if (!class_exists('transcribe_google')) {
 			$this->filename = $audio_filename;
 		}
 
+		public function set_audio_string(string $audio_string) {
+			$this->audio_string = $audio_string;
+		}
+
+		public function set_audio_mime_type(string $audio_mime_type) {
+			$this->audio_mime_type = $audio_mime_type;
+		}
+
 		public function set_format(string $audio_format) {
 			$this->format = $audio_format;
 		}
@@ -155,12 +165,17 @@ if (!class_exists('transcribe_google')) {
 			}
 
 			// get the content type
-			$path_array = pathinfo($this->path.'/'.$this->filename);
-			if ($path_array['extension'] == "mp3") {
-				$content_type = 'audio/mp3';
+			if (file_exists($this->path.'/'.$this->filename)) {
+				$path_array = pathinfo($this->path.'/'.$this->filename);
+				if ($path_array['extension'] == "mp3") {
+					$content_type = 'audio/mp3';
+				}
+				if ($path_array['extension'] == "wav") {
+					$content_type = 'audio/wav';
+				}
 			}
-			if ($path_array['extension'] == "wav") {
-				$content_type = 'audio/wav';
+			elseif (!empty($this->audio_string)) {
+				$content_type = $this->audio_mime_type;
 			}
 
 			// start output buffer
@@ -170,6 +185,23 @@ if (!class_exists('transcribe_google')) {
 			// version 1
 			if (trim($this->api_url) == 'https://speech.googleapis.com/v1p1beta1/speech') {
 				if (isset($this->api_key) && $this->api_key != '') {
+					
+					if (file_exists($this->path.'/'.$this->filename)) {
+						//file has been found
+					}
+					elseif (!empty($this->audio_string)) {
+						//if this is empty then use the temp directory
+						if (empty($this->path)) {
+							$this->path = sys_get_temp_dir();
+						}
+
+						//save the audio string on the file system
+						file_put_contents($this->path.'/'.$this->filename, $this->audio_string);
+					}
+					else {
+						//audio file or string not found
+						return false;
+					}
 
 					//get the length of the audio file
 					$audio_length = (float)system("soxi -D ".$this->path."/".$this->filename);
@@ -238,8 +270,18 @@ if (!class_exists('transcribe_google')) {
 					putenv("GOOGLE_APPLICATION_CREDENTIALS=".$this->application_credentials);
 				}
 
-				// Base64 encode audio file
-				$audio_base64 = base64_encode(file_get_contents($this->file_path . '/' . $this->file_name));
+				// Base64 encode the audio
+				if (file_exists($this->path.'/'.$this->filename)) {
+					//file has been found
+					$audio_base64 = base64_encode(file_get_contents($this->file_path . '/' . $this->file_name));
+				}
+				elseif (!empty($this->audio_string)) {
+					$audio_base64 = base64_encode($this->audio_string);
+				}
+				else {
+					//audio file or string not found
+					return false;
+				}
 
 				// Prepare JSON data
 				$data = [

+ 24 - 8
resources/classes/transcribe_openai.php

@@ -15,6 +15,8 @@ if (!class_exists('transcribe_openai')) {
 		private $api_key;
 		private $path;
 		private $filename;
+		private $audio_string;
+		private $audio_mime_type;
 		private $format;
 		private $voice;
 		private $message;
@@ -38,6 +40,14 @@ if (!class_exists('transcribe_openai')) {
 			$this->filename = $audio_filename;
 		}
 
+		public function set_audio_string(string $audio_string) {
+			$this->audio_string = $audio_string;
+		}
+
+		public function set_audio_mime_type(string $audio_mime_type) {
+			$this->audio_mime_type = $audio_mime_type;
+		}
+
 		public function set_format(string $audio_format) {
 			$this->format = $audio_format;
 		}
@@ -163,12 +173,6 @@ if (!class_exists('transcribe_openai')) {
 			//echo " --form 'response_format=text' ";
 			//echo "\n";
 
-			//return false if the file is not found
-			if (!file_exists($this->path.'/'.$this->filename)) {
-				echo "file not found\n";
-				return false;
-			}
-
 			//start output buffer
 			ob_start();
 			$out = fopen('php://output', 'w');
@@ -188,8 +192,20 @@ if (!class_exists('transcribe_openai')) {
 				'Content-Type: multipart/form-data'
 			));
 
-			// set the POST data
-			$post_data['file'] = new CURLFile($this->path.'/'.$this->filename);
+			// prepare the HTTP POST data
+			if (file_exists($this->path.'/'.$this->filename)) {
+				//send the audio from the file system
+				$post_data['file'] = new CURLFile($this->path.'/'.$this->filename);
+			}
+			elseif (!empty($this->audio_string)) {
+				//send the audio from as a string
+				$post_data['file'] = new CURLStringFile($this->audio_string, $this->filename, $this->audio_mime_type);
+			}
+			else {
+				//audio file or string not found
+				return false;
+			}
+
 			$post_data['model'] = 'whisper-1';
 			$post_data['response_format'] = 'text';
 			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

+ 34 - 6
resources/classes/transcribe_watson.php

@@ -17,6 +17,8 @@ if (!class_exists('transcribe_watson')) {
 		private $language;
 		private $path;
 		private $filename;
+		private $audio_string;
+		private $audio_mime_type;
 		private $format;
 		private $voice;
 		private $message;
@@ -42,6 +44,14 @@ if (!class_exists('transcribe_watson')) {
 			$this->filename = $audio_filename;
 		}
 
+		public function set_audio_string(string $audio_string) {
+			$this->audio_string = $audio_string;
+		}
+
+		public function set_audio_mime_type(string $audio_mime_type) {
+			$this->audio_mime_type = $audio_mime_type;
+		}
+
 		public function set_format(string $audio_format) {
 			$this->format = $audio_format;
 		}
@@ -144,12 +154,17 @@ if (!class_exists('transcribe_watson')) {
 		public function transcribe() : string {
 
 			//get the content type
-			$path_array = pathinfo($this->path.'/'.$this->filename);
-			if ($path_array['extension'] == "mp3") {
-				$content_type = 'audio/mp3';
+			if (file_exists($this->path.'/'.$this->filename)) {
+				$path_array = pathinfo($this->path.'/'.$this->filename);
+				if ($path_array['extension'] == "mp3") {
+					$content_type = 'audio/mp3';
+				}
+				if ($path_array['extension'] == "wav") {
+					$content_type = 'audio/wav';
+				}
 			}
-			if ($path_array['extension'] == "wav") {
-				$content_type = 'audio/wav';
+			elseif (!empty($this->audio_string)) {
+				$content_type = $this->audio_mime_type;
 			}
 
 			//start output buffer
@@ -181,7 +196,20 @@ if (!class_exists('transcribe_watson')) {
 
 			//send the file as binary
 			curl_setopt($ch, CURLOPT_BINARYTRANSFER,TRUE);
-			curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($this->path.'/'.$this->filename));
+
+			//prepare to send the file or audio
+			if (file_exists($this->path.'/'.$this->filename)) {
+				//send the audio from the file system
+				curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($this->path.'/'.$this->filename));
+			}
+			elseif (!empty($this->audio_string)) {
+				//send the audio from as a string
+				curl_setopt($ch, CURLOPT_POSTFIELDS, $this->audio_string));
+			}
+			else {
+				//audio file or string not found
+				return false;
+			}
 
 			//set the connection timeout and the overall maximum curl run time
 			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);