Audio spam moderation

Updated at:

This topic describes how to use the PHP SDK to call the audio spam moderation API and detect spam in real-time audio streams or audio files.

Feature description

Audio stream and audio file moderation are both asynchronous. You can retrieve the moderation results by polling or using callbacks. For more information about the scenes request parameter, the label classification parameter, and the suggestion parameter in the response, see Asynchronous audio moderation.

Audio moderation is billed per minute based on the duration of the moderated audio files or streams. The total duration is calculated daily. If the total duration for a day is less than one minute, it is billed as one minute.

Note
  • 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 PHP dependencies using the required PHP version. Using a different version causes operation calls to fail. See Install PHP dependencies.

  • An Alibaba Cloud RAM user with AccessKey ID and AccessKey secret stored as environment variables:

    • ALIBABA_CLOUD_ACCESS_KEY_ID

    • ALIBABA_CLOUD_ACCESS_KEY_SECRET

Submit an asynchronous audio moderation task

Interface

Description

Supported region

VoiceAsyncScanRequest

Asynchronously detects violations in audio streams or audio files.

  • Supported audio stream protocols: HTTP, RTMP, and RTSP

  • Supported audio stream formats: M3U8 and FLV

cn-shanghai: China (Shanghai)

Sample code

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Green\Green;

try {
    /**
     * Note: Reuse the instantiated client to improve moderation performance. This avoids repeatedly establishing connections.
     * Common ways to get environment variables:
     * Get the AccessKey ID of the RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
     * Get the AccessKey secret of the RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     */
    AlibabaCloud::accessKeyClient('Obtain the AccessKey ID of the RAM user from an environment variable', 'Obtain the AccessKey secret of the RAM user from an environment variable')
        ->timeout(10) // The timeout period is 10 seconds. This setting applies to all requests that use this client and do not have a timeout period specified.
        ->connectTimeout(3) // The connection timeout period is 3 seconds. If the unit is less than 1, it is automatically converted to milliseconds. This setting applies to all requests that use this client and do not have a connection timeout period specified.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $task1 = array('dataId' => 'Business data ID', 'url' => 'URL of the audio to be moderated');

    /**
     * Audio spam moderation: antispam.
     * If you moderate an audio stream, set the live parameter to true.
     * callback and seed: optional parameters used for callback notifications.
     **/
    $result = Green::v20180509()->voiceAsyncScan()
        ->timeout(10) // The timeout period is 10 seconds. The request timeout setting is valid only for the current request.
        ->connectTimeout(3) // The connection timeout period is 3 seconds. If the unit is less than 1, it is automatically converted to milliseconds. The request timeout setting is valid only for the current request.
        ->body(json_encode(array('tasks' => array($task1),
            'scenes' => array('antispam'),
            'bizType' => 'Business scenario',
            'callback' => 'Webhook address',
            'seed' => 'Random string')))
        ->request();
    print_r($result->toArray());
} catch (ClientException $exception) {
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

Query the results of an asynchronous audio moderation task

API

Description

Supported regions

VoiceAsyncScanResultsRequest

Queries asynchronous audio moderation results.

cn-shanghai: China (Shanghai)

Sample code

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Green\Green;

try {
    /**
     * Note: Reuse the instantiated client to improve moderation performance. This avoids repeatedly establishing connections.
     * Common ways to get environment variables:
     * Get the AccessKey ID of the RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
     * Get the AccessKey secret of the RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     */
    AlibabaCloud::accessKeyClient('Obtain the AccessKey ID of the RAM user from an environment variable', 'Obtain the AccessKey secret of the RAM user from an environment variable')
        ->timeout(10) // The timeout period is 10 seconds. This setting applies to all requests that use this client and do not have a timeout period specified.
        ->connectTimeout(3) // The connection timeout period is 3 seconds. If the unit is less than 1, it is automatically converted to milliseconds. This setting applies to all requests that use this client and do not have a connection timeout period specified.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $result = Green::v20180509()->voiceAsyncScanResults()
        ->timeout(10) // The timeout period is 10 seconds. The request timeout setting is valid only for the current request.
        ->connectTimeout(3) // The connection timeout period is 3 seconds. If the unit is less than 1, it is automatically converted to milliseconds. The request timeout setting is valid only for the current request.
        ->body(json_encode(array('ID of the asynchronous audio moderation task')))
        ->request();
    print_r($result->toArray());
} catch (ClientException $exception) {
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}