Audio spam detection
This topic describes how to use the Go software development kit (SDK) to detect spam in real-time audio streams or audio files.
Feature description
Audio stream detection and audio file detection are both asynchronous operations. You can retrieve the detection results using polling or callbacks. For more information about the parameters for detection scenarios (scenes), classification (label), and recommended actions (suggestion), see Asynchronous audio detection.
Audio detection is billed based on the duration of the audio files or streams. The service is billed in one-minute increments, and the total duration is calculated on a daily basis. If the total duration on a given 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
Before you begin, make sure you have:
Installed Go dependencies. See Installation for the required Go version. Using an unsupported version causes subsequent operation calls to fail.
A RAM user AccessKey ID and AccessKey secret stored as environment variables:
ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET
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
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: To improve detection performance, reuse the instantiated client. This avoids repeatedly establishing connections.
* Common methods to obtain environment variables:
* Obtain the AccessKey ID of the RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Obtain the AccessKey secret of the RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
client, _err := green.NewClientWithAccessKey(
"cn-shanghai",
"Obtain the AccessKey ID of a RAM user from an environment variable.",
"Obtain the AccessKey secret of a RAM user from an environment variable.")
if err != nil {
fmt.Println(err.Error())
return
}
task := map[string]interface{}{"dataId": "your_data_id", "url": "url_of_the_audio_to_scan"}
// scenes: The detection scenario. The only valid value is antispam.
content, _ := json.Marshal(
map[string]interface{}{
"tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"antispam"},
"bizType": "your_business_scenario",
},
)
request := green.CreateVoiceAsyncScanRequest()
request.SetContent(content)
response, _err := client.VoiceAsyncScan(request)
if _err != nil {
fmt.Println(_err.Error())
return
}
if response.GetHttpStatus() != 200 {
fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
}
fmt.Println(response.GetHttpContentString())
}
Query the results of an asynchronous audio detection task
|
API |
Description |
Supported regions |
|
VoiceAsyncScanResultsRequest |
Queries asynchronous audio moderation results. |
|
Sample code
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: To improve detection performance, reuse the instantiated client. This avoids repeatedly establishing connections.
* Common methods to obtain environment variables:
* Obtain the AccessKey ID of the RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Obtain the AccessKey secret of the RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
client, _err := green.NewClientWithAccessKey(
"cn-shanghai",
"Obtain the AccessKey ID of a RAM user from an environment variable.",
"Obtain the AccessKey secret of a RAM user from an environment variable.")
if err != nil {
fmt.Println(err.Error())
return
}
content, _ := json.Marshal([...]string{"asynchronous_audio_detection_task_id"})
request := green.CreateVoiceAsyncScanResultsRequest()
request.SetContent(content)
response, _err := client.VoiceAsyncScanResults(request)
if _err != nil {
fmt.Println(_err.Error())
return
}
if response.GetHttpStatus() != 200 {
fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
}
fmt.Println(response.GetHttpContentString())
}
Synchronous detection of short audio clips
|
API |
Description |
Supported regions |
|
VoicesyncscanRequest |
Queries the results of a short audio scan. |
|
Sample code
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: To improve detection performance, reuse the instantiated client. This avoids repeatedly establishing connections.
* Common methods to obtain environment variables:
* Obtain the AccessKey ID of the RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Obtain the AccessKey secret of the RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
client, _err := green.NewClientWithAccessKey(
"cn-shanghai",
"Obtain the AccessKey ID of a RAM user from an environment variable.",
"Obtain the AccessKey secret of a RAM user from an environment variable.")
if err != nil {
fmt.Println(err.Error())
return
}
task := map[string]interface{}{"dataId": "your_data_id", "url": "url_of_the_audio_to_scan"}
// scenes: The detection scenario. The only valid value is antispam.
content, _ := json.Marshal(
map[string]interface{}{
"tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"antispam"},
"bizType": "your_business_scenario",
},
)
request := green.CreateVoiceSyncScanRequest()
request.SetContent(content)
response, _err := client.VoiceSyncScan(request)
if _err != nil {
fmt.Println(_err.Error())
return
}
if response.GetHttpStatus() != 200 {
fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
}
fmt.Println(response.GetHttpContentString())
}