transcribe_openai.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // initialize a curl handle
  136. $ch = curl_init();
  137. // set the URL for the request
  138. curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/audio/transcriptions');
  139. // set the request method to POST
  140. curl_setopt($ch, CURLOPT_POST, true);
  141. // set the request headers
  142. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  143. 'Authorization: Bearer '.$this->api_key,
  144. 'Content-Type: multipart/form-data'
  145. ));
  146. // set the POST data
  147. $post_data['file'] = new CURLFile($this->path.'/'.$this->filename);
  148. $post_data['model'] = 'whisper-1';
  149. $post_data['response_format'] = 'text';
  150. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  151. // return the response as a string instead of outputting it directly
  152. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  153. // run the curl request and transcription message
  154. $this->message = curl_exec($ch);
  155. // check for errors
  156. if (curl_errno($ch)) {
  157. echo 'Error: ' . curl_error($ch);
  158. exit;
  159. }
  160. // close the handle
  161. curl_close($ch);
  162. // return the transcription
  163. if (empty($this->message)) {
  164. return '';
  165. }
  166. else {
  167. return trim($this->message);
  168. }
  169. }
  170. public function set_model(string $model): void {
  171. if (array_key_exists($model, $this->get_models())) {
  172. $this->model = $model;
  173. }
  174. }
  175. public function get_models(): array {
  176. return [
  177. 'tts-1-hd' => 'tts-1-hd'
  178. ];
  179. }
  180. }
  181. }
  182. ?>