Advertisement
Guest User

Speech To Text Method

a guest
Dec 8th, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1.         private static async Task<Stream> Upload(string modelString, byte[] audioFile)
  2.         {
  3.             HttpContent modelContent = new StringContent(modelString);
  4.             HttpContent partContent = new StringContent("audio/wav");
  5.             HttpContent continuousContent = new StringContent("true");
  6.             HttpContent audioContent = new ByteArrayContent(audioFile);
  7.             string actionUrl = "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize";
  8.  
  9.             using (var client = new HttpClient())
  10.             {
  11.                 var byteArray = new UTF8Encoding().GetBytes("<username>:<password>");
  12.                 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
  13.  
  14.                 using (var formData = new MultipartFormDataContent())
  15.                 {
  16.                     formData.Add(modelContent, "model");
  17.                     formData.Add(partContent, "audio/wav");
  18.                     formData.Add(continuousContent, "continuous");
  19.                     formData.Add(audioContent, "file1", "file1.wav");
  20.  
  21.                     var response = await client.PostAsync(actionUrl, formData);
  22.                     if (!response.IsSuccessStatusCode)
  23.                     {
  24.                         return null;
  25.                     }
  26.                     return await response.Content.ReadAsStreamAsync();
  27.                 }
  28.             }
  29.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement