Spaces:
Build error
Build error
File size: 4,009 Bytes
6c1e91d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# -*- coding: UTF-8 -*-
# Python 2.x引入httplib模块。
# import httplib
# Python 3.x引入http.client模块。
import http.client
# Python 2.x引入urllib模块。
# import urllib
# Python 3.x引入urllib.parse模块。
import urllib.parse
import json
def processGETRequest(appKey, token, text, audioSaveFile, format, sampleRate):
host = 'nls-gateway-cn-shanghai.aliyuncs.com'
url = 'https://' + host + '/stream/v1/tts'
# 设置URL请求参数
url = url + '?appkey=' + appKey
url = url + '&token=' + token
url = url + '&text=' + text
url = url + '&format=' + format
url = url + '&sample_rate=' + str(sampleRate)
# voice 发音人,可选,默认是xiaoyun。
# url = url + '&voice=' + 'xiaoyun'
# volume 音量,范围是0~100,可选,默认50。
# url = url + '&volume=' + str(50)
# speech_rate 语速,范围是-500~500,可选,默认是0。
# url = url + '&speech_rate=' + str(0)
# pitch_rate 语调,范围是-500~500,可选,默认是0。
# url = url + '&pitch_rate=' + str(0)
# print(url)
# Python 2.x请使用httplib。
# conn = httplib.HTTPSConnection(host)
# Python 3.x请使用http.client。
conn = http.client.HTTPSConnection(host)
conn.request(method='GET', url=url)
# 处理服务端返回的响应。
response = conn.getresponse()
# print('Response status and response reason:')
# print(response.status, response.reason)
contentType = response.getheader('Content-Type')
# print(contentType)
body = response.read()
if 'audio/mpeg' == contentType:
with open(audioSaveFile, mode='wb') as f:
f.write(body)
print('The GET request succeed!')
else:
print('The GET request failed: ' + str(body))
conn.close()
def processPOSTRequest(appKey, token, text, audioSaveFile, format, sampleRate):
host = 'nls-gateway-cn-shanghai.aliyuncs.com'
url = 'https://' + host + '/stream/v1/tts'
# 设置HTTPS Headers。
httpHeaders = {
'Content-Type': 'application/json'
}
# 设置HTTPS Body。
body = {'appkey': appKey, 'token': token, 'text': text, 'format': format, 'sample_rate': sampleRate}
body = json.dumps(body)
print('The POST request body content: ' + body)
# Python 2.x请使用httplib。
# conn = httplib.HTTPSConnection(host)
# Python 3.x请使用http.client。
conn = http.client.HTTPSConnection(host)
conn.request(method='POST', url=url, body=body, headers=httpHeaders)
# 处理服务端返回的响应。
response = conn.getresponse()
print('Response status and response reason:')
print(response.status, response.reason)
contentType = response.getheader('Content-Type')
print(contentType)
body = response.read()
if 'audio/mpeg' == contentType:
with open(audioSaveFile, mode='wb') as f:
f.write(body)
print('The POST request succeed!')
else:
print('The POST request failed: ' + str(body))
conn.close()
# appKey = '您的appkey'
# token = '您的token'
# token = "f9e6939d959b47e7ac97cab755560edd"
# appKey = "YdRfHrZVeusHKfv6"
# text = '我是A i Vtuber,很高兴认识你,你有什么开心的事情嘛。'
# 采用RFC 3986规范进行urlencode编码。
# textUrlencode = text
# Python 2.x请使用urllib.quote。
# textUrlencode = urllib.quote(textUrlencode, '')
# Python 3.x请使用urllib.parse.quote_plus。
# textUrlencode = urllib.parse.quote_plus(textUrlencode)
# textUrlencode = textUrlencode.replace("+", "%20")
# textUrlencode = textUrlencode.replace("*", "%2A")
# textUrlencode = textUrlencode.replace("%7E", "~")
# print('text: ' + textUrlencode)
# audioSaveFile = 'syAudios.wav'
# format = 'wav'
# sampleRate = 16000
# GET请求方式
# processGETRequest(appKey, token, textUrlencode, audioSaveFile, format, sampleRate)
# POST请求方式
# processPOSTRequest(appKey, token, text, audioSaveFile, format, sampleRate)
# if __name__ == '__main__':
# appKey = ""
|