예제 #1
0
def processGETRequest(appKey, token, voice, text, 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)
    url = url + '&voice=' + voice
    logger.debug(url)
    conn = http.client.HTTPSConnection(host)
    conn.request(method='GET', url=url)
    # 处理服务端返回的响应
    response = conn.getresponse()
    logger.debug('Response status and response reason:')
    logger.debug(response.status, response.reason)
    contentType = response.getheader('Content-Type')
    logger.debug(contentType)
    body = response.read()
    if 'audio/mpeg' == contentType:
        logger.debug('The GET request succeed!')
        tmpfile = utils.write_temp_file(body, '.mp3')
        conn.close()
        return tmpfile
    else:
        logger.debug('The GET request failed: ' + str(body))
        conn.close()
        return None
예제 #2
0
 def post(self):
     global conversation
     if self.validate(self.get_argument('validate')):
         if self.get_argument('type') == 'text':
             query = self.get_argument('query')
             uuid = self.get_argument('uuid')
             conversation.doResponse(query, uuid)
             res = {'code': 0, 'message': 'ok'}
             self.write(json.dumps(res))
         elif self.get_argument('type') == 'voice':
             voice_data = self.get_argument('voice')
             tmpfile = utils.write_temp_file(base64.b64decode(voice_data),
                                             '.wav')
             fname, suffix = os.path.splitext(tmpfile)
             nfile = fname + '-16k' + suffix
             # downsampling
             soxCall = 'sox ' + tmpfile + \
                       ' ' + nfile + ' rate 16k'
             subprocess.call([soxCall], shell=True, close_fds=True)
             utils.check_and_delete(tmpfile)
             conversation.doConverse(nfile)
             res = {'code': 0, 'message': 'ok'}
             self.write(json.dumps(res))
         else:
             res = {'code': 1, 'message': 'illegal type'}
             self.write(json.dumps(res))
     else:
         res = {'code': 1, 'message': 'illegal visit'}
         self.write(json.dumps(res))
     self.finish()
예제 #3
0
def processPOSTRequest(appKey, token, voice, text, 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, 'voice': voice}
    body = json.dumps(body)
    logger.debug('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()
    logger.debug('Response status and response reason:')
    logger.debug(response.status ,response.reason)
    contentType = response.getheader('Content-Type')
    logger.debug(contentType)
    body = response.read()
    if 'audio/mpeg' == contentType :
        logger.debug('The POST request succeed!')
        tmpfile = utils.write_temp_file(body, '.mp3')
        conn.close()
        return tmpfile
    else :
        logger.debug('The POST request failed: ' + str(body))
        conn.close()
        return None
예제 #4
0
def processGETRequest(appKey, token, voice, text, 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)
    url = url + "&voice=" + voice
    logger.debug(url)
    conn = http.client.HTTPSConnection(host)
    conn.request(method="GET", url=url)
    # 处理服务端返回的响应
    response = conn.getresponse()
    logger.debug("Response status and response reason:")
    logger.debug(response.status, response.reason)
    contentType = response.getheader("Content-Type")
    logger.debug(contentType)
    body = response.read()
    if "audio/mpeg" == contentType:
        logger.debug("The GET request succeed!")
        tmpfile = utils.write_temp_file(body, ".mp3")
        conn.close()
        return tmpfile
    else:
        logger.debug("The GET request failed: " + str(body))
        conn.close()
        return None
예제 #5
0
 def post(self):
     global conversation
     if self.validate(self.get_argument("validate", default=None)):
         if self.get_argument("type") == "text":
             query = self.get_argument("query")
             uuid = self.get_argument("uuid")
             if query == "":
                 res = {"code": 1, "message": "query text is empty"}
                 self.write(json.dumps(res))
             else:
                 conversation.doResponse(
                     query,
                     uuid,
                     onSay=lambda msg, audio, plugin: self.onResp(
                         msg, audio, plugin),
                 )
         elif self.get_argument("type") == "voice":
             voice_data = self.get_argument("voice")
             tmpfile = utils.write_temp_file(base64.b64decode(voice_data),
                                             ".wav")
             fname, suffix = os.path.splitext(tmpfile)
             nfile = fname + "-16k" + suffix
             # downsampling
             soxCall = "sox " + tmpfile + " " + nfile + " rate 16k"
             subprocess.call([soxCall], shell=True, close_fds=True)
             utils.check_and_delete(tmpfile)
             conversation.doConverse(
                 nfile,
                 onSay=lambda msg, audio, plugin: self.onResp(
                     msg, audio, plugin),
             )
         else:
             res = {"code": 1, "message": "illegal type"}
             self.write(json.dumps(res))
     else:
         res = {"code": 1, "message": "illegal visit"}
         self.write(json.dumps(res))
     self.finish()
예제 #6
0
def processPOSTRequest(appKey, token, voice, text, 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,
        "voice": voice,
    }
    body = json.dumps(body)
    logger.debug("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()
    logger.debug("Response status and response reason:")
    logger.debug(response.status, response.reason)
    contentType = response.getheader("Content-Type")
    logger.debug(contentType)
    body = response.read()
    if "audio/mpeg" == contentType:
        logger.debug("The POST request succeed!")
        tmpfile = utils.write_temp_file(body, ".mp3")
        conn.close()
        return tmpfile
    else:
        logger.critical("The POST request failed: " + str(body))
        conn.close()
        return None
예제 #7
0
def convert(faq_str, json_file_str):
    faq_file_str = utils.write_temp_file(faq_str, ".csv", mode="w")
    run(faq_file_str, json_file_str)
예제 #8
0
def convert(faq_str, json_file_str):
    faq_file_str = utils.write_temp_file(faq_str, '.csv', mode='w')
    run(faq_file_str, json_file_str)
예제 #9
0
 def download_mp3_by_link(self, song_info):
     logger.debug("begin DownLoad {}" % (song_info))
     mp3 = requests.get(song_info['song_link']).content
     tmpfile = utils.write_temp_file(mp3, '.mp3')
     return tmpfile