This topic describes how to use the .NET software development kit (SDK) to detect spam in real-time audio streams and audio files.
Feature description
Audio stream and audio file detection are both asynchronous operations. You can obtain the detection results using polling or callbacks. For more information about the scenes parameter in the request, the label parameter in the response, and the suggestion parameter, see the Audio asynchronous detection API documentation.
Audio detection is billed daily based on the total duration of the detected audio files or audio streams. The billing granularity is per minute. If the total detection duration for a day is less than one minute, it is billed as one minute.
-
This SDK supports only public audio URLs. It does not support local files or binary data.
-
Supported URL types:
-
Public HTTP/HTTPS URLs that do not exceed 2048 characters in length.
-
Alibaba Cloud OSS URL:
oss://<bucket-name>.<endpoint>/<object-name>. You must authorize AI Guardrails to access the destination OSS bucket. The bucket and the AI Guardrails service must be in the same region. For more information, see Authorize AI Guardrails to access an OSS bucket.
-
Prerequisites
The .NET dependencies must be installed. For instructions, see Install .NET dependencies.
You must use the required .NET version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
Submit an asynchronous audio detection task
|
Interface |
Description |
Supported region |
|
VoiceAsyncScanRequest |
Asynchronously detects violations in audio streams or audio files.
|
cn-shanghai: China (Shanghai) |
Sample code
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
/**
* Common ways to get environment variables:
* Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
"Obtain the AccessKey ID of the RAM user from an environment variable.",
"Obtain the AccessKey secret of the RAM user from an environment variable.");
// Note: Reuse the instantiated client to improve detection performance and avoid repeatedly establishing connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
VoiceAsyncScanRequest request = new VoiceAsyncScanRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
Dictionary<string, object> task1 = new Dictionary<string, object>();
task1.Add("dataId", "Data ID");
task1.Add("url", "URL of the audio file to be detected");
// scenes: The detection scenario. The only valid value is antispam.
// callback and seed are optional parameters used for callback notifications.
Dictionary<string, object> httpBody = new Dictionary<string, object>();
httpBody.Add("scenes", new List<string> { "antispam" });
httpBody.Add("bizType", "Business scenario");
httpBody.Add("callback", "Webhook address");
httpBody.Add("seed", "Random string");
httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });
request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
try
{
VoiceAsyncScanResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
}
}Query the results of an asynchronous audio detection task
|
API |
Description |
Supported regions |
|
VoiceAsyncScanResultsRequest |
Queries asynchronous audio moderation results. |
|
Sample code
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
/**
* Common ways to get environment variables:
* Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
"Obtain the AccessKey ID of the RAM user from an environment variable.",
"Obtain the AccessKey secret of the RAM user from an environment variable.");
// Note: Reuse the instantiated client to improve detection performance and avoid repeatedly establishing connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
VoiceAsyncScanResultsRequest request = new VoiceAsyncScanResultsRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
List<string> taskIdList = new List<string> { "Audio detection task ID" };
request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(taskIdList)), "utf-8", FormatType.JSON);
try
{
VoiceAsyncScanResultsResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
}
}Synchronous detection of short audio files
API | Description | Supported regions |
VoiceSyncScanRequest | You can query the short speech detection results. |
|
Sample code
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
/**
* Common ways to get environment variables:
* Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
"Obtain the AccessKey ID of the RAM user from an environment variable.",
"Obtain the AccessKey secret of the RAM user from an environment variable.");
// Note: Reuse the instantiated client to improve detection performance and avoid repeatedly establishing connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
VoiceSyncScanRequest request = new VoiceSyncScanRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
Dictionary<string, object> task1 = new Dictionary<string, object>();
task1.Add("dataId", "Data ID");
task1.Add("url", "URL of the audio file to be detected");
Dictionary<string, object> httpBody = new Dictionary<string, object>();
// scenes: The detection scenarios. You can specify multiple scenarios.
httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
httpBody.Add("bizType", "Business scenario");
httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });
request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
try
{
VoiceSyncScanResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
}
}