transcribe_openai.php 6.8 KB

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