Python3调用腾讯长语音接口(重点在回调url(callback url))

另一部分调用的代码主要用用的这篇文章现成的:https://blog.csdn.net/huangmingleiluo/article/details/82784877 

当时看了上面的参照,上传文件没问题了,回调url不知道咋搞,用tornado写了个服务部署在阿里云上,还真成功了。

但是发现腾讯有的任务不调用我的回调url,测试10个音频可能有一两个没结果,不知道为啥

 

callback_url.py

#callback_url.py

import time
import  logging
import logging.handlers
import  json
import tornado.ioloop
import tornado.web
import tornado.httpserver

#设置日志,可有可无,不是重点
def setup_logging(level=logging.DEBUG, stream=True, stream_level=logging.DEBUG,
                  *, save_file=True, filename=''):
    formatter = logging.Formatter(
        '[%(levelname)1.1s %(asctime)s.%(msecs)03d %(name)s:%(lineno)d] %(message)s',
        datefmt='%y%m%d %H:%M:%S',
    )
    logging.getLogger().setLevel(level)
    if save_file:
        rotating_file_handler = logging.handlers.RotatingFileHandler(
            'server.log' if not filename else filename,
            mode='a',
            maxBytes=100*1024*1024,
            backupCount=10,
            encoding='utf-8',
        )
        rotating_file_handler.setFormatter(formatter)
        logging.getLogger().addHandler(rotating_file_handler)
    if stream:
        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(formatter)
        stream_handler.setLevel(stream_level)
        logging.getLogger().addHandler(stream_handler)

setup_logging(filename='server.log')

logger=logging.getLogger(__name__)

class MainHandler(tornado.web.RequestHandler):


    #定义tornado的post方法
    def post(self, *args, **kwargs):
        callback = {}

        
        try:
            response = self.request.body
            res_dict=json.loads(response.decode('utf-8'))
            text=res_dict['data']['text']
        except Exception as err:
            logger.exception('get_result_fail')
            # logger.debug('get_result_fail %r',err)
            callback['ret'] = -1
            callback['msg'] = err.__repr__()
        else:
            logger.debug('res_dict:%r'%res_dict)

            fp=open('tencent.txt','a')
            fp.write(text+'\n')

            callback['ret']=0
            callback['msg']='ok'



        self.write(callback)

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.bind(8322)
    http_server.start(1)
    tornado.ioloop.IOLoop.current().start()

 

 

在贴一下主程序代码:

client.py

# -*- coding: utf-8 -*-
import os
import time

import urllib.parse
import urllib.request
import hashlib
import base64
import uuid
import wave

import requests
import urllib
import json


app_id = 'xxx',
app_key='xxx'
url = 'https://api.ai.qq.com/fcgi-bin/aai/aai_wxasrlong'

#我们自己的阿里云服务器的地址开了8322端口,来跑我的tornado服务
callback_url='https:/xxxx:8322'
# callback_url='http://localhost:8322'


def md5(string):
    md = hashlib.md5()
    md.update(string.encode('utf-8'))
    md5 = md.hexdigest().upper()
    return md5


def urlencode(args):
    tuples = [(k, args[k]) for k in sorted(args.keys()) if args[k]]
    query_str = urllib.parse.urlencode(tuples)
    return query_str


def signify(args, app_key):
    query_str = urlencode(args)
    query_str = query_str + '&app_key=' + app_key
    signiture = md5(query_str)
    return signiture


def http_post(api_url, args):
    resp = requests.post(url=api_url, data=args)
    resp = json.loads(resp.text)
    return resp

def get_wav_time(file_path):
    #获取时间用第三方库pydub更方便,生成实例AudioSegment实例,调用duration_seconds属性,不过懒得改了
    wave_f = wave.open(file_path, 'r')
    nframe = wave_f.getnframes()
    frame_rate=wave_f.getframerate()
    wav_time=nframe//frame_rate
    return wav_time

def send_file(file_path):
    f = open(file_path, 'rb')
    file_content = f.read()

    base64_audio = base64.b64encode(file_content)
    uuidstr = uuid.uuid4().hex

    body = {
        'app_id': '2113196713',
        'format': '1',
        'callback_url': 'http://47.105.69.153:8322',
        'speech': base64_audio,
        'time_stamp': str(time.time()),
        'nonce_str': uuidstr,
        'sign': ''
    }

    body['sign'] = signify(body, 'HoMecLBIe6xvJpLC')

    resp = http_post(url, body)
    f.close()
    return resp







def get_result(file_path):
    index=0

    resp=send_file(file_path)
    if resp.get('ret')!=0:
        print('上传音频失败')
        print('resp',resp)
        return
    task_id = resp['data']['task_id']
    print(task_id)

    wav_time=get_wav_time(file_path)
    print('wav_time:',wav_time)

    while True:
        print(index)
        #尝试五次,失败就跳出循环
        if index>5:
            return ''

        #请求我我写的另一个tornado服务获取结果
        res = requests.get(url='http://xxxx:8323')
        res_dict = res.json()

        try:
            text = res_dict[task_id]
        except Exception as e:
            index+=1
            print('----------',e)
        else:
            return text

                # return text
        time.sleep(wav_time // 3)




if __name__ == '__main__':
    files_path='/home/middle_wav'
    files_list=os.listdir(files_path)
    for file in files_list:        
        file_path=os.path.join(files_path,file)
        print(file_path)
        res=get_result(file_path)
        print("识别结果:",res)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值