声音注册查询
前提条件
- 确保已按照配置Python环境配置完毕。
- 请参考SDK(websocket)获取最新版本SDK包。
- 仅上海一局点支持该接口。
初始化Client
初始化vcs_client,详见表1。
请求参数(声音注册)
请求类型为register_query_request,详见表2。
|
参数名称 |
是否必选 |
参数类型 |
描述 |
|---|---|---|---|
|
path |
是 |
String |
待注册声音路径,要求采样率不低于16k。 |
|
config |
是 |
Object |
详见表3。 |
请求参数(声音查询)
请求类型register_query_request,详见表4。
响应参数(声音注册)
响应参数(声音查询)
请求示例
- 说明:“endpoint”即调用API的请求地址,不同服务不同区域的“endpoint”不同,具体请参见终端节点,当前仅支持上海一。
- 调用API,注册查询声音。
import base64
import os
from huaweicloud_sis.bean.vcs_register_voice_request import RegisterVoiceRequest
from huaweicloud_sis.client.vcs_client import VcsClient
from huaweicloud_sis.bean.sis_config import SisConfig
# 鉴权参数
# 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
# 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SIS_AK/HUAWEICLOUD_SIS_SK/HUAWEICLOUD_SIS_PROJECT_ID。
ak = os.getenv("HUAWEICLOUD_SIS_AK") # 从环境变量获取ak 参考https://support.huaweicloud.com/sdkreference-sis/sis_05_0003.html
assert ak is not None, "Please add ak in your develop environment"
sk = os.getenv("HUAWEICLOUD_SIS_SK") # 从环境变量获取sk 参考https://support.huaweicloud.com/sdkreference-sis/sis_05_0003.html
assert sk is not None, "Please add sk in your develop environment"
region = "" # region,支持上海一region,cn-east-3
project_id = "" # project id 同region一一对应,参考https://support.huaweicloud.com/api-sis/sis_03_0008.html
path = "" # 参考语音路径,请根据实际路径修改,要求采样率不小于16k,例如 r"D://test.wav"
voice_name = "" # 音色名称,可以使用注册音色或预置音色
service_endpoint = "" # 可选,自定义endpoint,例如"https://sis-ext.cn-east-3.myhuaweicloud.com"
def register_voice_example(path, voice_name):
with open(path, 'rb') as f:
data = f.read()
data = str(base64.b64encode(data), 'utf-8')
config = SisConfig()
config.set_connect_timeout(10) # 设置连接超时,单位s
config.set_read_timeout(60) # 设置读取超时,单位s
vcs_client = VcsClient(ak, sk, region, project_id, sis_config=config, service_endpoint=service_endpoint)
register_voice_request = RegisterVoiceRequest(data, voice_name)
result = vcs_client.register_voice(register_voice_request)
print(result)
def query_example():
config = SisConfig()
config.set_connect_timeout(10) # 设置连接超时,单位s
config.set_read_timeout(60) # 设置读取超时,单位s
vcs_client = VcsClient(ak, sk, region, project_id, sis_config=config, service_endpoint=service_endpoint)
result = vcs_client.query_voice_name(10, 0) # 默认查询10条,支持分页查询
print(result)
if __name__ == '__main__':
# 两个接口可单独运行
register_voice_example(path, voice_name) # 注册
query_example() # 查询
响应示例
状态码:200
成功响应示例(注册声音)
{
"trace_id": "567e8537-a89c-13c3-a882-826321939651",
"result":{
"voice_name": "test_xiaoming"
}
}
状态码:200
成功响应示例(查询声音)
{
"trace_id": "567e8537-a89c-13c3-a882-826321939651",
"result":{
"voices": [
{
"voice_name": "test_xiaoming",
"language": "chinese"
}
}
}
状态码:400
失败响应示例(注册查询声音)
{
"error_code": "SIS.***",
"error_msg": "***"
}