PageRenderTime 1316ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/email_queue/resources/functions/transcribe.php

https://github.com/fusionpbx/fusionpbx
PHP | 266 lines | 178 code | 44 blank | 44 comment | 55 complexity | bc100c35a2f074acb2b2366cc90545c7 MD5 | raw file
  1. <?php
  2. if (!function_exists('transcribe')) {
  3. function transcribe ($file_path, $file_name, $file_extension) {
  4. //transcription variables
  5. $transcribe_provider = $_SESSION['voicemail']['transcribe_provider']['text'];
  6. $transcribe_language = $_SESSION['voicemail']['transcribe_language']['text'];
  7. //transcribe - watson
  8. if ($transcribe_provider == 'watson') {
  9. $api_key = $_SESSION['voicemail']['watson_key']['text'];
  10. $api_url = $_SESSION['voicemail']['watson_url']['text'];
  11. if ($file_extension == "mp3") {
  12. $content_type = 'audio/mp3';
  13. }
  14. if ($file_extension == "wav") {
  15. $content_type = 'audio/wav';
  16. }
  17. if (isset($api_key) && $api_key != '') {
  18. //check if the file exists
  19. if (!file_exists($file_path.'/'.$file_name)) {
  20. echo "file not found ".$file_path.'/'.$file_name;
  21. exit;
  22. }
  23. //start output buffer
  24. ob_start();
  25. $out = fopen('php://output', 'w');
  26. //create the curl resource
  27. $ch = curl_init();
  28. //set the curl options
  29. curl_setopt($ch, CURLOPT_URL, $api_url);
  30. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  31. curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . $api_key);
  32. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //set the authentication type
  33. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: '.$content_type]);
  34. curl_setopt($ch, CURLOPT_BINARYTRANSFER,TRUE);
  35. curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file_path.'/'.$file_name));
  36. curl_setopt($ch, CURLOPT_POST, 1);
  37. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); //The number of seconds to wait while trying to connect.
  38. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any "Location: " header that the server sends as part of the HTTP header.
  39. curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect.
  40. curl_setopt($ch, CURLOPT_TIMEOUT, 300); //The maximum number of seconds to allow cURL functions to execute.
  41. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); //To stop cURL from verifying the peer's certificate.
  42. curl_setopt($ch, CURLOPT_HEADER, 0); //hide the headers when set to 0
  43. //add verbose for debugging
  44. curl_setopt($ch, CURLOPT_VERBOSE, true);
  45. curl_setopt($ch, CURLOPT_STDERR, $out);
  46. //execute the curl with the options
  47. $http_content = curl_exec($ch);
  48. //return the error
  49. if (curl_errno($ch)) {
  50. echo 'Error:' . curl_error($ch);
  51. }
  52. //close the curl resource
  53. curl_close($ch);
  54. //show the debug information
  55. fclose($out);
  56. $debug = ob_get_clean();
  57. echo $debug;
  58. //$command = "curl -X POST -silent -u \"apikey:".$api_key."\" --header \"Content-type: ".$content_type."\" --data-binary @".$file_path."/".$file_name." \"".$api_url."\"";
  59. //echo "command: ".$command."\n";
  60. //ob_start();
  61. //$result = passthru($command);
  62. //$json_result = ob_get_contents();
  63. //ob_end_clean();
  64. //run the command
  65. //$http_response = shell_exec($command);
  66. //echo "http_response:\n".$http_response."\n";
  67. //remove headers and return the http content
  68. //$http_response = trim(str_ireplace("HTTP/1.1 100 Continue", "", $http_response));
  69. //$temp_array = explode("HTTP/1.1 200 OK", $http_response);
  70. //$http_array = explode("\r\n\r\n", $temp_array[1]);
  71. //$http_content = trim($http_array[1]);
  72. echo "http_content:\n".$http_content."\n";
  73. //validate the json
  74. $ob = json_decode($http_content);
  75. if($ob === null) {
  76. echo "invalid json\n";
  77. return false;
  78. }
  79. $message = '';
  80. $json = json_decode($http_content, true);
  81. //echo "json; ".$json."\n";
  82. foreach($json['results'] as $row) {
  83. $message .= $row['alternatives'][0]['transcript'];
  84. }
  85. $message = str_replace("%HESITATION", " ", trim($message));
  86. $message = ucfirst($message);
  87. $array['provider'] = $transcribe_provider;
  88. $array['language'] = $transcribe_language;
  89. //$array['command'] = $command;
  90. $array['message'] = $message;
  91. return $array;
  92. }
  93. }
  94. //transcribe - google
  95. if ($transcribe_provider == 'google') {
  96. $api_key = $_SESSION['voicemail']['google_key']['text'];
  97. $api_url = $_SESSION['voicemail']['google_url']['text'];
  98. $transcribe_language = $_SESSION['voicemail']['transcribe_language']['text'];
  99. $transcribe_alternate_language = $_SESSION['voicemail']['transcribe_alternate_language']['text'];
  100. if (!isset($transcribe_language) && strlen($transcribe_language) == 0) {
  101. $transcribe_language = 'en-Us';
  102. }
  103. if (!isset($transcribe_alternate_language) && strlen($transcribe_alternate_language) == 0) {
  104. $transcribe_alternate_language = 'es-Us';
  105. }
  106. if ($file_extension == "mp3") {
  107. $content_type = 'audio/mp3';
  108. }
  109. if ($file_extension == "wav") {
  110. $content_type = 'audio/wav';
  111. }
  112. if (isset($api_key) && $api_key != '') {
  113. //$command = "curl -X POST -silent -u \"apikey:".$api_key."\" --header \"Content-type: ".$content_type."\" --data-binary @".$file_path."/".$file_name." \"".$api_url."\"";
  114. //echo "command: ".$command."\n";
  115. $command = "sox ".$file_path."/".$file_name." ".$file_path."/".$file_name.".flac trim 0 00:59 ";
  116. $command .= "&& echo \"{ 'config': { 'languageCode': '".$transcribe_language."', 'enableWordTimeOffsets': false , 'enableAutomaticPunctuation': true , 'alternativeLanguageCodes': '".$transcribe_alternate_language."' }, 'audio': { 'content': '`base64 -w 0 ".$file_path."/".$file_name.".flac`' } }\" ";
  117. $command .= "| curl -X POST -H \"Content-Type: application/json\" -d @- ".$api_url.":recognize?key=".$api_key." ";
  118. $command .= "&& rm -f ".$file_path."/".$file_name.".flac";
  119. echo $command."\n";
  120. //ob_start();
  121. //$result = passthru($command);
  122. //$json_result = ob_get_contents();
  123. //ob_end_clean();
  124. //run the command
  125. $http_response = shell_exec($command);
  126. //validate the json
  127. $ob = json_decode($http_response);
  128. if($ob === null) {
  129. echo "invalid json\n";
  130. return false;
  131. }
  132. $message = '';
  133. $json = json_decode($http_response, true);
  134. //echo "json; ".$json."\n";
  135. foreach($json['results'] as $row) {
  136. $message .= $row['alternatives'][0]['transcript'];
  137. }
  138. //build the response
  139. $array['provider'] = $transcribe_provider;
  140. $array['language'] = $transcribe_language;
  141. $array['command'] = $command;
  142. $array['message'] = $message;
  143. //print_r($array);
  144. return $array;
  145. }
  146. }
  147. //transcribe - azure
  148. if ($transcribe_provider == 'azure') {
  149. $api_key = $_SESSION['voicemail']['azure_key']['text'];
  150. $api_url = $_SESSION['voicemail']['azure_server_region']['text'];
  151. if (strlen($transcribe_language) == 0) {
  152. $transcribe_language = 'en-US';
  153. }
  154. if ($file_extension == "mp3") {
  155. $content_type = 'audio/mp3';
  156. }
  157. if ($file_extension == "wav") {
  158. $content_type = 'audio/wav';
  159. }
  160. if (isset($api_key) && $api_key != '') {
  161. $command = "curl -X POST \"https://".$api_url.".api.cognitive.microsoft.com/sts/v1.0/issueToken\" -H \"Content-type: application/x-www-form-urlencoded\" -H \"Content-Length: 0\" -H \"Ocp-Apim-Subscription-Key: ".$api_key."\"";
  162. $access_token_result = shell_exec($command);
  163. if (strlen($access_token_result) == 0) {
  164. return false;
  165. }
  166. else {
  167. $file_path = $file_path.'/'.$file_name;
  168. $command = "curl -X POST \"https://".$api_url.".stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=".$transcribe_language."&format=detailed\" -H 'Authorization: Bearer ".$access_token_result."' -H 'Content-type: audio/wav; codec=\"audio/pcm\"; samplerate=8000; trustsourcerate=false' --data-binary @".$file_path;
  169. echo $command."\n";
  170. $http_response = shell_exec($command);
  171. $array = json_decode($http_response, true);
  172. if ($array === null) {
  173. return false;
  174. }
  175. else {
  176. $message = $array['NBest'][0]['Display'];
  177. }
  178. }
  179. $array['provider'] = $transcribe_provider;
  180. $array['language'] = $transcribe_language;
  181. $array['api_key'] = $api_key;
  182. $array['command'] = $command;
  183. $array['message'] = $message;
  184. return $array;
  185. }
  186. }
  187. //transcribe - custom
  188. //Works with Mozilla DeepSpeech or Coqui with https://github.com/AccelerateNetworks/DeepSpeech_Frontend
  189. //or Vosk with https://git.callpipe.com/fusionpbx/vosk_frontend
  190. if ($transcribe_provider == 'custom') {
  191. $api_key = $_SESSION['voicemail']['api_key']['text'];
  192. $api_url = $_SESSION['voicemail']['transcription_server']['text'];
  193. if (strlen($transcribe_language) == 0) {
  194. $transcribe_language = 'en-US';
  195. }
  196. if ($file_extension == "mp3") {
  197. $content_type = 'audio/mp3';
  198. }
  199. if ($file_extension == "wav") {
  200. $content_type = 'audio/wav';
  201. }
  202. $file_path = $file_path.'/'.$file_name;
  203. $command = "curl -X POST ".$api_url." -H 'Authorization: Bearer ".$api_key."' -F file=@".$file_path;
  204. echo $command."\n";
  205. $http_response = shell_exec($command);
  206. $array = json_decode($http_response, true);
  207. if ($array === null) {
  208. return false;
  209. }
  210. else {
  211. $message = $array['message'];
  212. }
  213. $array['provider'] = $transcribe_provider;
  214. $array['language'] = $transcribe_language;
  215. $array['api_key'] = $api_key;
  216. $array['command'] = $command;
  217. $array['message'] = $message;
  218. return $array;
  219. }
  220. }
  221. }
  222. ?>