transcribe_openai.php 6.7 KB

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