xunfei.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. """
  3. 科大讯飞的语音识别API.
  4. 外网ip查询:https://ip.51240.com/
  5. voice_name: https://www.xfyun.cn/services/online_tts
  6. """
  7. from __future__ import division
  8. from ..__load__ import *
  9. from .base import Base
  10. import requests
  11. import hashlib
  12. class Xunfei(Base):
  13. url = 'http://api.xfyun.cn/v1/service/v1/'
  14. def __init__(self):
  15. super(self.__class__, self).__init__()
  16. def setting(self, appid, api_key, voice_name='vecan', **args):
  17. self.appid = appid
  18. self.api_key = api_key
  19. self.voice_name = voice_name
  20. def getHeader(self, aue, engineType):
  21. curTime = str(int(time.time()))
  22. if aue == 'raw':
  23. param = "{\"aue\":\"" + aue + "\"" + ",\"engine_type\":\"" + engineType + "\"}"
  24. else:
  25. param = "{\"aue\":\""+aue+"\",\"auf\":\"audio/L16;rate=16000\",\"voice_name\":\"" + self.voice_name + "\",\"engine_type\":\"intp65\"}"
  26. Demeter.logger.debug("param:{}".format(param))
  27. paramBase64 = str(base64.b64encode(param.encode('utf-8')), 'utf-8')
  28. Demeter.logger.debug("x_param:{}".format(paramBase64))
  29. m2 = hashlib.md5()
  30. m2.update((self.api_key + curTime + paramBase64).encode('utf-8'))
  31. checkSum = m2.hexdigest()
  32. header = {
  33. 'X-CurTime': curTime,
  34. 'X-Param': paramBase64,
  35. 'X-Appid': self.appid,
  36. 'X-CheckSum': checkSum,
  37. 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
  38. }
  39. return header
  40. def asr(self, fp):
  41. url = self.url + "iat"
  42. binfile = open(fp, 'rb')
  43. data = {'audio': base64.b64encode(binfile.read())}
  44. r = requests.post(url, headers=self.getHeader('raw', 'sms16k'), data=data)
  45. res = json.loads(r.content.decode('utf-8'))
  46. Demeter.logger.debug(res)
  47. if 'code' in res and res['code'] == '0':
  48. return self.asrLog(1, res['data'])
  49. else:
  50. return self.asrLog(2, '')
  51. def tts(self, phrase):
  52. url = self.url + "tts"
  53. r = requests.post(url, headers=self.getHeader('lame', ''), data={'text':phrase})
  54. contentType = r.headers['Content-Type']
  55. if contentType == "audio/mpeg":
  56. tmpfile = writeTempFile(r.content, '.mp3')
  57. return self.ttsLog(1, tmpfile)
  58. else :
  59. return self.ttsLog(2, r.text)