Created
March 8, 2023 14:40
-
-
Save ploeber/ff8b10f98dad3034678a18af8f406fbb to your computer and use it in GitHub Desktop.
AssemblyAI Transcription Example C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net.Http; | |
using System.Text; | |
// Third-pary package needs to be added, e.g, with: | |
// dotnet add package NewtonSoft.Json | |
using Newtonsoft.Json; | |
string url = "https://api.assemblyai.com/v2/transcript"; | |
var data = new Dictionary<string, string>(){ | |
{ "audio_url", "https://bit.ly/3yxKEIY" } | |
}; | |
using (var client = new HttpClient()) | |
{ | |
// Add headers | |
client.DefaultRequestHeaders.Add("authorization", "API_TOKEN"); | |
var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); | |
HttpResponseMessage response = await client.PostAsync(url, content); | |
var responseContent = await response.Content.ReadAsStringAsync(); | |
var responseJson = JsonConvert.DeserializeObject<dynamic>(responseContent); | |
string transcriptId = responseJson.id; | |
string pollingEndpoint = $"https://api.assemblyai.com/v2/transcript/{transcriptId}"; | |
while (true) { | |
var pollingResponse = await client.GetAsync(pollingEndpoint); | |
var pollingResponseContent = await pollingResponse.Content.ReadAsStringAsync(); | |
var pollingResponseJson = JsonConvert.DeserializeObject<dynamic>(pollingResponseContent); | |
if (pollingResponseJson.status == "completed") { | |
Console.WriteLine(pollingResponseJson.text); | |
break; | |
} else if (pollingResponseJson.status == "error") { | |
throw new Exception($"Transcription failed: {pollingResponseJson.error}"); | |
} else { | |
Thread.Sleep(3000); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment