当前位置:   article > 正文

python 讯飞开放平台语音转写API,双人对话区分角色_讯飞语音转写api

讯飞语音转写api

讯飞给的语音转写demo没有添加角色分离的参数

语音转写 API 文档 | 讯飞开放平台文档中心

 双人对话,取roleType=1 roleNum=2,添加在upload函数中param_dict

以下是添加了区分角色参数后改写的代码(包括json文件的保存)

  1. # -*- coding: utf-8 -*-
  2. import base64
  3. import hashlib
  4. import hmac
  5. import json
  6. import os
  7. import time
  8. import requests
  9. import urllib
  10. lfasr_host = 'https://raasr.xfyun.cn/v2/api'
  11. # 请求的接口名
  12. api_upload = '/upload'
  13. api_get_result = '/getResult'
  14. roleType=1
  15. class RequestApi(object):
  16. def __init__(self, appid, secret_key, upload_file_path):
  17. self.appid = appid
  18. self.secret_key = secret_key
  19. self.upload_file_path = upload_file_path
  20. self.ts = str(int(time.time()))
  21. self.signa = self.get_signa()
  22. def get_signa(self):
  23. appid = self.appid
  24. secret_key = self.secret_key
  25. m2 = hashlib.md5()
  26. m2.update((appid + self.ts).encode('utf-8'))
  27. md5 = m2.hexdigest()
  28. md5 = bytes(md5, encoding='utf-8')
  29. # 以secret_key为key, 上面的md5为msg, 使用hashlib.sha1加密结果为signa
  30. signa = hmac.new(secret_key.encode('utf-8'), md5, hashlib.sha1).digest()
  31. signa = base64.b64encode(signa)
  32. signa = str(signa, 'utf-8')
  33. return signa
  34. def upload(self):
  35. print("上传部分:")
  36. upload_file_path = self.upload_file_path
  37. file_len = os.path.getsize(upload_file_path)
  38. file_name = os.path.basename(upload_file_path)
  39. param_dict = {}
  40. param_dict['appId'] = self.appid
  41. param_dict['signa'] = self.signa
  42. param_dict['ts'] = self.ts
  43. param_dict["fileSize"] = file_len
  44. param_dict["fileName"] = file_name
  45. param_dict["duration"] = "200"
  46. param_dict["roleNum"] = 2
  47. param_dict["roleType"] = 1
  48. print("upload参数:", param_dict)
  49. data = open(upload_file_path, 'rb').read(file_len)
  50. response = requests.post(url =lfasr_host + api_upload+"?"+urllib.parse.urlencode(param_dict),
  51. headers = {"Content-type":"application/json"},data=data)
  52. print("upload_url:",response.request.url)
  53. result = json.loads(response.text)
  54. print("upload resp:", result)
  55. return result
  56. def get_result(self):
  57. uploadresp = self.upload()
  58. orderId = uploadresp['content']['orderId']
  59. param_dict = {}
  60. param_dict['appId'] = self.appid
  61. param_dict['signa'] = self.signa
  62. param_dict['ts'] = self.ts
  63. param_dict['orderId'] = orderId
  64. param_dict['resultType'] = "transfer,predict"
  65. print("")
  66. print("查询部分:")
  67. print("get result参数:", param_dict)
  68. status = 3
  69. # 建议使用回调的方式查询结果,查询接口有请求频率限制
  70. while status == 3:
  71. response = requests.post(url=lfasr_host + api_get_result + "?" + urllib.parse.urlencode(param_dict),
  72. headers={"Content-type": "application/json"})
  73. # print("get_result_url:",response.request.url)
  74. result = json.loads(response.text)
  75. print(result)
  76. status = result['content']['orderInfo']['status']
  77. print("status=",status)
  78. if status == 4:
  79. break
  80. time.sleep(5)
  81. print("get_result resp:",result)
  82. return result
  83. # 输入讯飞开放平台的appid,secret_key和待转写的文件路径
  84. if __name__ == '__main__':
  85. api = RequestApi(appid="xxxx",
  86. secret_key="xxxx",
  87. upload_file_path=r"xxxxxxxx.wav")
  88. result = api.get_result()
  89. json.dump(result, open('result.json', 'w'), ensure_ascii=False)

出来的结果是这样的,多层嵌套,找到规律一层一层解开就行了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/201635
推荐阅读
相关标签
  

闽ICP备14008679号