@@ -210,6 +210,129 @@ while (true) {
210
210
await new Promise ((resolve ) => setTimeout (resolve , 3000 ))
211
211
}
212
212
}
213
+ ```
214
+
215
+ </Tab >
216
+ <Tab language = " csharp" title = " C#" >
217
+
218
+ Enable Topic Detection by setting ` redact_pii ` to ` true ` in the JSON payload.
219
+
220
+ Set ` redact_pii_policies ` to specify the information you want to redact. For the full list of policies, see [ PII policies] ( #pii-policies ) .
221
+
222
+ Set ` redact_pii_sub ` to specify the replacement for redacted information.
223
+
224
+ <Info >
225
+ Most of these libraries are included by default, but on .NET Framework and Mono you need to reference the System.Net.Http library and install the [ System.Net.Http.Json NuGet package] ( https://www.nuget.org/packages/System.Net.Http.Json ) .
226
+ </Info >
227
+
228
+ ``` csharp {53-55}
229
+ using System ;
230
+ using System .IO ;
231
+ using System .Net .Http ;
232
+ using System .Net .Http .Headers ;
233
+ using System .Net .Http .Json ;
234
+ using System .Text ;
235
+ using System .Text .Json ;
236
+ using System .Threading .Tasks ;
237
+ using System .Text .Json .Serialization ;
238
+ using System .Collections .Generic ;
239
+
240
+ class Program
241
+ {
242
+ static async Task Main (string [] args )
243
+ {
244
+ string baseUrl = " https://api.assemblyai.com" ;
245
+
246
+ using (var httpClient = new HttpClient ())
247
+ {
248
+ httpClient .DefaultRequestHeaders .Authorization =
249
+ new AuthenticationHeaderValue (" <YOUR_API_KEY>" );
250
+
251
+ string uploadUrl = await UploadFileAsync (" ./local_file.mp3" , httpClient , baseUrl );
252
+
253
+ var transcript = await CreateTranscriptWithPiiRedactionAsync (uploadUrl , httpClient , baseUrl );
254
+
255
+ Console .WriteLine ($" Transcript ID: {transcript .Id }" );
256
+ transcript = await WaitForTranscriptToProcessAndGetRedactedText (transcript , httpClient , baseUrl );
257
+ }
258
+ }
259
+
260
+ static async Task <string > UploadFileAsync (string filePath , HttpClient httpClient , string baseUrl )
261
+ {
262
+ using (var fileStream = File .OpenRead (filePath ))
263
+ using (var fileContent = new StreamContent (fileStream ))
264
+ {
265
+ fileContent .Headers .ContentType = new MediaTypeHeaderValue (" application/octet-stream" );
266
+
267
+ using (var response = await httpClient .PostAsync ($" {baseUrl }/v2/upload" , fileContent ))
268
+ {
269
+ response .EnsureSuccessStatusCode ();
270
+ var jsonDoc = await response .Content .ReadFromJsonAsync <JsonDocument >();
271
+ return jsonDoc .RootElement .GetProperty (" upload_url" ).GetString ();
272
+ }
273
+ }
274
+ }
275
+
276
+ static async Task <Transcript > CreateTranscriptWithPiiRedactionAsync (string audioUrl , HttpClient httpClient , string baseUrl )
277
+ {
278
+ var data = new
279
+ {
280
+ audio_url = audioUrl ,
281
+ redact_pii = true ,
282
+ redact_pii_policies = new [] { " person_name" , " organization" , " occupation" },
283
+ redact_pii_sub = " hash"
284
+ };
285
+
286
+ var content = new StringContent (JsonSerializer .Serialize (data ), Encoding .UTF8 , " application/json" );
287
+
288
+ using (var response = await httpClient .PostAsync ($" {baseUrl }/v2/transcript" , content ))
289
+ {
290
+ response .EnsureSuccessStatusCode ();
291
+ return await response .Content .ReadFromJsonAsync <Transcript >();
292
+ }
293
+ }
294
+
295
+ static async Task <Transcript > WaitForTranscriptToProcessAndGetRedactedText (Transcript transcript , HttpClient httpClient , string baseUrl )
296
+ {
297
+ string pollingEndpoint = $" {baseUrl }/v2/transcript/{transcript .Id }" ;
298
+
299
+ while (true )
300
+ {
301
+ var pollingResponse = await httpClient .GetAsync (pollingEndpoint );
302
+ transcript = await pollingResponse .Content .ReadFromJsonAsync <Transcript >();
303
+
304
+ switch (transcript .Status )
305
+ {
306
+ case " completed" :
307
+ // Print the redacted transcript text
308
+ Console .WriteLine (transcript .Text );
309
+ return transcript ;
310
+
311
+ case " error" :
312
+ throw new Exception ($" Transcription failed: {transcript .Error }" );
313
+
314
+ default :
315
+ await Task .Delay (TimeSpan .FromSeconds (3 ));
316
+ break ;
317
+ }
318
+ }
319
+ }
320
+
321
+ public class Transcript
322
+ {
323
+ [JsonPropertyName (" id" )]
324
+ public string Id { get ; set ; }
325
+
326
+ [JsonPropertyName (" status" )]
327
+ public string Status { get ; set ; }
328
+
329
+ [JsonPropertyName (" text" )]
330
+ public string Text { get ; set ; }
331
+
332
+ [JsonPropertyName (" error" )]
333
+ public string Error { get ; set ; }
334
+ }
335
+ }
213
336
```
214
337
215
338
</Tab >
@@ -519,6 +642,92 @@ while (true) {
519
642
await new Promise ((resolve ) => setTimeout (resolve , 3000 ))
520
643
}
521
644
}
645
+ ```
646
+
647
+ </Tab >
648
+ <Tab value = " csharp" title = " C#" default >
649
+ To create a redacted version of the audio file, set ` redact_pii_audio ` to ` true ` on the JSON payload.
650
+ Set ` redact_pii_audio_quality ` to specify the quality of the redacted audio file.
651
+
652
+ Use the transcript ID to poll the [ GET redacted audio endpoint] ( https://www.assemblyai.com/docs/api-reference/transcripts/get-redacted-audio ) every few seconds to check the status of the redacted audio. Once the status is ` redacted_audio_ready ` , you can retrieve the audio URL from the API response.
653
+ ``` csharp {7-8,29,35-63,67-77}
654
+ var data = new
655
+ {
656
+ audio_url = audioUrl ,
657
+ redact_pii = true ,
658
+ redact_pii_policies = new [] { " person_name" , " organization" , " occupation" },
659
+ redact_pii_sub = " hash" ,
660
+ redact_pii_audio = true ,
661
+ redact_pii_audio_quality = " wav" // Optional. Defaults to "mp3"
662
+ };
663
+
664
+ // ...
665
+ static async Task Main (string [] args )
666
+ {
667
+ string baseUrl = " https://api.assemblyai.com" ;
668
+
669
+ using (var httpClient = new HttpClient ())
670
+ {
671
+ httpClient .DefaultRequestHeaders .Authorization =
672
+ new AuthenticationHeaderValue (" <YOUR_API_KEY>" );
673
+
674
+ string uploadUrl = await UploadFileAsync (" ./local_file.mp3" , httpClient , baseUrl );
675
+
676
+ var transcript = await CreateTranscriptWithPiiRedactionAsync (uploadUrl , httpClient , baseUrl );
677
+
678
+ Console .WriteLine ($" Transcript ID: {transcript .Id }" );
679
+ transcript = await WaitForTranscriptToProcessAndGetRedactedText (transcript , httpClient , baseUrl );
680
+
681
+ // Wait for the redacted audio
682
+ string redactedAudioUrl = await WaitForRedactedAudioAsync (transcript .Id , httpClient , baseUrl );
683
+ }
684
+ }
685
+
686
+ // ...
687
+
688
+ static async Task < string > WaitForRedactedAudioAsync (string transcriptId , HttpClient httpClient , string baseUrl )
689
+ {
690
+ string redactedAudioPollingEndpoint = $" {baseUrl }/v2/transcript/{transcriptId }/redacted-audio" ;
691
+
692
+ while (true )
693
+ {
694
+ var pollingResponse = await httpClient .GetAsync (redactedAudioPollingEndpoint );
695
+ var responseContent = await pollingResponse .Content .ReadAsStringAsync ();
696
+
697
+ var redactedAudioResult = JsonSerializer .Deserialize <RedactedAudioResult >(
698
+ responseContent ,
699
+ new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
700
+ );
701
+
702
+ if (redactedAudioResult .Status == " redacted_audio_ready" )
703
+ {
704
+ Console .WriteLine (redactedAudioResult .RedactedAudioUrl );
705
+ return redactedAudioResult .RedactedAudioUrl ;
706
+ }
707
+ else if (redactedAudioResult .Status == " error" )
708
+ {
709
+ throw new Exception ($" Redacted audio failed: {redactedAudioResult .Error }" );
710
+ }
711
+ else
712
+ {
713
+ await Task .Delay (TimeSpan .FromSeconds (3 ));
714
+ }
715
+ }
716
+ }
717
+
718
+ // ...
719
+
720
+ public class RedactedAudioResult
721
+ {
722
+ [JsonPropertyName (" status" )]
723
+ public string Status { get ; set ; }
724
+
725
+ [JsonPropertyName (" redacted_audio_url" )]
726
+ public string RedactedAudioUrl { get ; set ; }
727
+
728
+ [JsonPropertyName (" error" )]
729
+ public string Error { get ; set ; }
730
+ }
522
731
```
523
732
524
733
</Tab >
0 commit comments