API TTS - Text To Speech


  • URL

    https://viettelgroup.ai/voice/api/tts/v1/rest/syn

  • Method

    http POST

  • Header Parameters

    1. header "Content-Type: application/json" 2. header "token: token_id"

    Để có "token id" bạn có thể đăng ký tài khoản tại viettelgroup.ai, sau đó login, rồi vào menu token để tạo

Định dạng data gửi lên: Json (default)

{
    "text": "hệ thống tổng hợp tiếng nói trung tâm không gian mạng",
    "voice": "doanngocle",
    "id": "2",
    "without_filter": false,
    "speed": 1.0,
    "tts_return_option": 2
}

Mô tả các tham số:

property name Value Description
text String text đầu vào cần tổng hợp.
voice String giọng đọc muốn tổng hợp.
id String ID request do người dụng đặt (có thể dùng để traceback).
without_filter Boolean có sử dụng filter để tăng chất lượng giọng hay không (chậm hơn).
speed Float tốc độ giọng (có giá trị 0.7 - 1.3, tương ứng tốc độ đọc từ x0.7 đến x1.3 lần).
tts_return_option Interger một số dạng integer để chọn api như sau:
1: streaming
2: wav
3: mp3

Success Response:

  • Code: 200
  • Content: inputstream (audio wav format)

Giọng đọc hiện có

Mã giọng đọc Mô tả
hn-quynhanh Giọng nữ miền Bắc chất lượng cao
hcm-diemmy Giọng nữ miền Nam chất lượng cao
hue-maingoc Giọng nữ miền Trung chất lượng cao
hn-phuongtrang Giọng nữ miền Bắc chất lượng cao
hn-thanhtung Giọng nam miền Bắc
hue-baoquoc Giọng nam miền Trung
hcm-minhquan Giọng nam miền Nam
trinhthiviettrinh Giọng nữ miền Bắc
lethiyen Giọng nữ miền Nam
nguyenthithuyduyen Giọng nữ miền Nam
phamtienquan Giọng nam miền Bắc

Bạn có thể lấy danh sách giọng đọc mới nhất thông qua API: https://viettelgroup.ai/voice/api/tts/v1/rest/voices


example curl:

tạo file data.json như sau:

{
    "text": "Hệ thống tổng hợp tiếng nói trung tâm không gian mạng",
    "voice": "doanngocle",
    "id": "2",
    "without_filter": false,
    "speed": 1.0,
    "tts_return_option": 2
}

command:

curl --header "Content-Type: application/json" --header "token: demo_token"   --request POST   --data "@data.json" https://viettelgroup.ai/voice/api/tts/v1/rest/syn > example.wav
#if encounter SSL error because of https certificate, add option '-k' to  make insecure connections   (this will expose your application to security risks, such as man-in-the-middle attacks.)

output trả về là file: example.wav

example python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import the json library
import json
import requests
import urllib

url = "https://viettelgroup.ai/voice/api/tts/v1/rest/syn"
data = {"text": "hôm nay là thứ mấy", "voice": "doanngocle", "id": "2", "without_filter": False, "speed": 1.0, "tts_return_option": 2}
headers = {'Content-type': 'application/json', 'token': 'demo_token'}
response = requests.post(url, data=json.dumps(data), headers=headers)
#if encounter SSL error because of https certificate, please comment out above line and use the line below to  make insecure connections   (this will expose your application to security risks, such as man-in-the-middle attacks.)
#response = requests.post(url, data=json.dumps(data), headers=headers, verify=False)
# Headers is a dictionary
print(response.headers)
# Get status_code.
print(response.status_code)
# Get the response data as a python object.
data = response.content
f = open("/home/tanpk/log/tanpk.wav", "wb")
f.write(data)
f.close()

example java:


public class TestAPI {
    public static InputStream getTTS(String apiUrl, String datajson) throws IOException {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost request = new HttpPost(apiUrl);
        StringEntity body = new StringEntity(datajson, "UTF-8");
        /*add content-type, token into header request */
        request.addHeader("content-type", "application/json;charset=UTF-8");
        request.addHeader("token", "demo_token");
        request.getRequestLine();
        request.setEntity(body);
        HttpResponse response = httpClient.execute(request);
        System.err.println(response);
        return response.getEntity().getContent();
    }
    public static void main(String[] args) throws IOException {
        /*
        * create datajon format
        * text need to endcode by URLencode
        * */
        String datajson = "{\"text\":\"xin chào các bạn"," +
                "\"voice\":\"doanngocle\"," +
                "\"id\":\"3\"," +
                "\"without_filter\":false," +
                "\"speed\":1.0," +
                "\"tts_return_option\":2}";
        InputStream result = getTTS("https://viettelgroup.ai/voice/api/tts/v1/rest/syn", datajson);
        /* you can write result to file to use */
    }
}
Nội dung