transcribe_watson.php 5.9 KB

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