transcribe_local.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * transcribe_local class
  4. *
  5. * @method null download
  6. */
  7. if (!class_exists('transcribe_local')) {
  8. class transcribe_local implements transcribe_interface {
  9. /**
  10. * declare private variables
  11. */
  12. private $api_key;
  13. private $api_url;
  14. public $api_model;
  15. private $path;
  16. private $filename;
  17. private $audio_string;
  18. private $audio_mime_type;
  19. private $format;
  20. private $message;
  21. private $language;
  22. /**
  23. * called when the object is created
  24. */
  25. public function __construct($settings) {
  26. //build the setting object and get the recording path
  27. $this->api_key = $settings->get('transcribe', 'api_key', '');
  28. $this->api_url = $settings->get('transcribe', 'api_url', '');
  29. $this->api_model = $settings->get('transcribe', 'api_model', 'whisper-1');
  30. }
  31. public function set_path(string $audio_path) {
  32. $this->path = $audio_path;
  33. }
  34. public function set_filename(string $audio_filename) {
  35. $this->filename = $audio_filename;
  36. }
  37. public function set_audio_string(string $audio_string) {
  38. $this->audio_string = $audio_string;
  39. }
  40. public function set_audio_mime_type(string $audio_mime_type) {
  41. $this->audio_mime_type = $audio_mime_type;
  42. }
  43. public function set_format(string $audio_format) {
  44. $this->format = $audio_format;
  45. }
  46. public function set_message(string $audio_message) {
  47. $this->message = $audio_message;
  48. }
  49. public function is_language_enabled() : bool {
  50. //return the whether engine is handles languages
  51. return false;
  52. }
  53. public function set_language(string $audio_language) {
  54. $this->language = $audio_language;
  55. }
  56. public function get_languages() : array {
  57. //create the languages array
  58. $languages = array(
  59. "af" => "Afrikaans",
  60. "ar" => "Arabic",
  61. "hy" => "Armenian",
  62. "az" => "Azerbaijani",
  63. "be" => "Belarusian",
  64. "bs" => "Bosnian",
  65. "bg" => "Bulgarian",
  66. "ca" => "Catalan",
  67. "zh" => "Chinese",
  68. "hr" => "Croatian",
  69. "cs" => "Czech",
  70. "da" => "Danish",
  71. "nl" => "Dutch",
  72. "en" => "English",
  73. "et" => "Estonian",
  74. "fi" => "Finnish",
  75. "fr" => "French",
  76. "gl" => "Galician",
  77. "de" => "German",
  78. "el" => "Greek",
  79. "he" => "Hebrew",
  80. "hi" => "Hindi",
  81. "hu" => "Hungarian",
  82. "is" => "Icelandic",
  83. "id" => "Indonesian",
  84. "it" => "Italian",
  85. "ja" => "Japanese",
  86. "kn" => "Kannada",
  87. "kk" => "Kazakh",
  88. "ko" => "Korean",
  89. "lv" => "Latvian",
  90. "lt" => "Lithuanian",
  91. "mk" => "Macedonian",
  92. "ms" => "Malay",
  93. "mr" => "Marathi",
  94. "mi" => "Maori",
  95. "ne" => "Nepali",
  96. "no" => "Norwegian",
  97. "fa" => "Persian",
  98. "pl" => "Polish",
  99. "pt" => "Portuguese",
  100. "ro" => "Romanian",
  101. "ru" => "Russian",
  102. "sr" => "Serbian",
  103. "sk" => "Slovak",
  104. "sl" => "Slovenian",
  105. "es" => "Spanish",
  106. "sw" => "Swahili",
  107. "sv" => "Swedish",
  108. "tl" => "Tagalog",
  109. "ta" => "Tamil",
  110. "th" => "Thai",
  111. "tr" => "Turkish",
  112. "uk" => "Ukrainian",
  113. "ur" => "Urdu",
  114. "vi" => "Vietnamese",
  115. "cy" => "Welsh"
  116. );
  117. //return the languages array
  118. return $languages;
  119. }
  120. /**
  121. * transcribe - speech to text
  122. */
  123. public function transcribe() : string {
  124. // Use the curl command line for debuging
  125. //echo "/usr/bin/curl --request POST ";
  126. //echo " --url 'http://127.0.0.1:8000/transcribe' ";
  127. //echo " --header 'Authorization: Bearer ".$this->api_key."' ";
  128. //echo " --header 'Content-Type: multipart/form-data' ";
  129. //echo " --form 'file=@".$this->path.'/'.$this->filename."' ";
  130. //echo " --form 'model=whisper-1' ";
  131. //echo " --form 'response_format=text' ";
  132. //echo "\n";
  133. //start output buffer
  134. ob_start();
  135. $out = fopen('php://output', 'w');
  136. // initialize a curl handle
  137. $ch = curl_init();
  138. // set the api_url if not already set
  139. if (empty($this->api_url)) {
  140. $this->api_url = 'http://127.0.0.1:8000/transcribe';
  141. }
  142. // set the URL for the request
  143. curl_setopt($ch, CURLOPT_URL, $this->api_url);
  144. // set the request method to POST
  145. curl_setopt($ch, CURLOPT_POST, true);
  146. // set the request headers
  147. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  148. 'Authorization: Bearer '.$this->api_key,
  149. 'Content-Type: multipart/form-data'
  150. ));
  151. // prepare the HTTP POST data
  152. if (file_exists($this->path.'/'.$this->filename)) {
  153. //send the audio from the file system
  154. $post_data['file'] = new CURLFile($this->path.'/'.$this->filename);
  155. }
  156. elseif (!empty($this->audio_string)) {
  157. //send the audio from as a string
  158. $post_data['file'] = new CURLStringFile($this->audio_string, $this->filename, $this->audio_mime_type);
  159. }
  160. else {
  161. //audio file or string not found
  162. return false;
  163. }
  164. $post_data['model'] = $this->api_model;
  165. $post_data['response_format'] = 'text';
  166. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  167. // return the response as a string instead of outputting it directly
  168. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  169. // set the connection timeout and the overall maximum curl run time
  170. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
  171. curl_setopt($ch, CURLOPT_TIMEOUT, 300);
  172. // follow any "Location: " header the server sends as part of the HTTP header.
  173. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  174. // automatically set the Referer: field in requests where it follows a Location: redirect.
  175. curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
  176. // set whether to verify SSL peer
  177. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
  178. // add verbose for debugging
  179. curl_setopt($ch, CURLOPT_VERBOSE, true);
  180. curl_setopt($ch, CURLOPT_STDERR, $out);
  181. // run the curl request and transcription message
  182. $this->message = curl_exec($ch);
  183. // show the debug information
  184. fclose($out);
  185. // check for errors
  186. if (curl_errno($ch)) {
  187. echo 'Error: ' . curl_error($ch);
  188. exit;
  189. }
  190. // close the handle
  191. curl_close($ch);
  192. // return the transcription
  193. if (empty($this->message)) {
  194. return '';
  195. }
  196. else {
  197. return trim($this->message);
  198. }
  199. }
  200. public function set_model(string $model): void {
  201. if (array_key_exists($model, $this->get_models())) {
  202. $this->api_model = $model;
  203. }
  204. }
  205. public function get_models(): array {
  206. return [
  207. 'tts-1-hd' => 'tts-1-hd'
  208. ];
  209. }
  210. }
  211. }
  212. ?>