transcribe_openai.php 6.0 KB

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