Text translation

更新时间:
复制 MD 格式

This topic describes the features and implementation of text translation.

Text translation converts transcribed audio into different languages. It supports bidirectional, real-time translation between Chinese, English, Japanese, Korean, German, French, and Russian. You can also use this feature to translate audio and video files. This feature helps break down communication barriers, making international communication and learning more accessible. The real-time translation service is cost-effective, offering low latency and competitive pricing.

Request parameters

Parameter

Type

Description

TranslationEnabled

boolean

The default value is false.

Translation

object

A collection of parameters that control the translation feature.

Translation.TargetLanguages

list[]

The target languages for translation. Supported languages are Chinese (cn), English (en), Japanese (ja), Korean (ko), German (de), French (fr), and Russian (ru).

For audio and video file transcription, only one target language is supported.

For descriptions of other request parameters, see the document for your scenario:

Example settings

{
    "Input":{
        ...
    },
    "Parameters":{
        ...
        "TranslationEnabled":true,
        "Translation":{
            "TargetLanguages":[
                "en"
            ]
        },
        ...
    }
}

Code examples

#!/usr/bin/env python
#coding=utf-8

import os
import json
import datetime
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from aliyunsdkcore.auth.credentials import AccessKeyCredential

def create_common_request(domain, version, protocolType, method, uri):
    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain(domain)
    request.set_version(version)
    request.set_protocol_type(protocolType)
    request.set_method(method)
    request.set_uri_pattern(uri)
    request.add_header('Content-Type', 'application/json')
    return request

def init_parameters():
    root = dict()
    root['AppKey'] = 'Enter the AppKey that you created in the Tingwu console'

    # Basic request parameters
    input = dict()
    input['SourceLanguage'] = 'cn'
    input['TaskKey'] = 'task' + datetime.datetime.now().strftime('%Y%m%d%H%M%S')
    input['FileUrl'] = 'Enter the URL of the audio file to test'
    root['Input'] = input

    # AI-related parameters. Set as needed.
    parameters = dict()
    # Text translation controls
    parameters['TranslationEnabled'] = True
    translation = dict()
    translation['TargetLanguages'] = ['en'] # Translate to English
    parameters['Translation'] = translation
    root['Parameters'] = parameters
    return root

body = init_parameters()
print(body)

# TODO: Set your AccessKeyId and AccessKeySecret as environment variables.
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
client = AcsClient(region_id='cn-beijing', credential=credentials)

request = create_common_request('tingwu.cn-beijing.aliyuncs.com', '2023-09-30', 'https', 'PUT', '/openapi/tingwu/v2/tasks')
request.add_query_param('type', 'offline')

request.set_content(json.dumps(body).encode('utf-8'))
response = client.do_action_with_exception(request)
print("response: \n" + json.dumps(json.loads(response), indent=4, ensure_ascii=False))
package com.alibaba.tingwu.client.demo.aitest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import org.junit.Test;

/**
 * @author tingwu2023
 */
public class TranslationTest {

    @Test
    public void testTranslate() throws ClientException {
        CommonRequest request = createCommonRequest("tingwu.cn-beijing.aliyuncs.com", "2023-09-30", ProtocolType.HTTPS, MethodType.PUT, "/openapi/tingwu/v2/tasks");
        request.putQueryParameter("type", "offline");

        JSONObject root = new JSONObject();
        root.put("AppKey", "Enter the AppKey that you created in the Tingwu console");
        JSONObject input = new JSONObject();
        input.fluentPut("FileUrl", "Enter the URL of the audio file to test")
                .fluentPut("SourceLanguage", "cn")
                .fluentPut("TaskKey", "task" + System.currentTimeMillis());
        root.put("Input", input);

        JSONObject parameters = new JSONObject();

        // Translation
        parameters.put("TranslationEnabled", true);
        JSONObject translation = new JSONObject();
        JSONArray langArry = new JSONArray();
        // Set the target language to English.
        langArry.add("en");
        translation.put("TargetLanguages", langArry);
        parameters.put("Translation", translation);

        root.put("Parameters", parameters);
        System.out.println(root.toJSONString());
        request.setHttpContent(root.toJSONString().getBytes(), "utf-8", FormatType.JSON);

        // TODO: Set your AccessKeyId and AccessKeySecret as environment variables.
        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        IAcsClient client = new DefaultAcsClient(profile);
        CommonResponse response = client.getCommonResponse(request);
        System.out.println(response.getData());
    }
    public static CommonRequest createCommonRequest(String domain, String version, ProtocolType protocolType, MethodType method, String uri) {
        // Create an API request and set its parameters.
        CommonRequest request = new CommonRequest();
        request.setSysDomain(domain);
        request.setSysVersion(version);
        request.setSysProtocol(protocolType);
        request.setSysMethod(method);
        request.setSysUriPattern(uri);
        request.setHttpContentType(FormatType.JSON);
        return request;
    }
}

Example output

{
    "Code":"0",
    "Data":{
        "TaskId":"7f849f3f85ec4cc0acez3********",
        "TaskStatus":"COMPLETED",
        "Result":{
            "Translation":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/7f849f3f85ec4cc0ace53bf275166882/7f849f3f85ec4cc0ace53bf275166882_Translation_20231031141959.json?Expires=1698819737&OSSAccessKeyId=yourAccessKeyID&Signature=N%2FjNy********brEmy6Ru5vJm0%3D",
            "Transcription":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/7f849f3f85ec4cc0ace53bf275166882/7f849f3f85ec4cc0ace53bf275166882_Transcription_20231031141952.json?Expires=1698819737&OSSAccessKeyId=yourAccessKeyID&Signature=yatEu*********UQ%2F%2Ba38fCHlw4%3D"
        }
    },
    "Message":"success",
    "RequestId":"c88c8ae0-93c9-4d01-b0dd-d8********"
}

The Translation field contains the HTTP URL for downloading the text translation result.

Protocol parsing

The content at the translation result URL is a JSON message. The following example shows the message format.

{
    "TaskId":"7f849f3f85ec4cc0ace53bf27********",
    "Translation":{
        "Paragraphs":[
            {
                "ParagraphId":"1698733191989500000",
                "Sentences":[
                    {
                        "SentenceId":1,
                        "Start":70,
                        "End":1780,
                        "Text":"There will be no Internet companies in the future. "
                    }
                ]
            }
        ]
    }
}

The fields are defined as follows.

Parameter name

Type

Description

TaskId

string

The ID generated when the task is created.

Translation

object

The translation result object.

Translation.Paragraphs

list[]

A collection of translation results organized by paragraph. This corresponds to the speech recognition result message.

Translation.Paragraphs.ParagraphId

string

The paragraph segment ID. This corresponds to the ParagraphId in the speech recognition result.

Translation.Paragraphs.Sentences

list[]

A collection of translated sentences.

Translation.Paragraphs.Sentences[i].SentenceId

long

The sentence ID.

Translation.Paragraphs.Sentences[i].Start

long

The start time of the segment relative to the beginning of the audio, in milliseconds.

Translation.Paragraphs.Sentences[i].End

long

The end time of the segment relative to the beginning of the audio, in milliseconds.

Translation.Paragraphs.Sentences[i].Text

string

The translated text. This corresponds to the speech recognition result message.

FAQ

In which scenarios can the translation feature be invoked?

  • You can set this directly when creating an offline transcription for an audio or video file.

  • After an offline transcription or real-time meeting ends. You can rerun the task. You must use the original TaskId for the rerun request.