tts_http_demo.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #coding=utf-8
  2. '''
  3. requires Python 3.6 or later
  4. pip install requests
  5. '''
  6. import base64
  7. import json
  8. import uuid
  9. import requests
  10. # 填写平台申请的appid, access_token以及cluster
  11. appid = "4600087561"
  12. access_token= "ATXbVUCNRIzjZQs3ML3ev_fT53LDHiUD"
  13. cluster = "volcano_icl"
  14. voice_type = "S_toCOrDx61"
  15. host = "openspeech.bytedance.com"
  16. api_url = f"https://{host}/api/v1/tts"
  17. header = {"Authorization": f"Bearer;{access_token}"}
  18. request_json = {
  19. "app": {
  20. "appid": appid,
  21. "token": "access_token",
  22. "cluster": cluster
  23. },
  24. "user": {
  25. "uid": "388808087185078"
  26. },
  27. "audio": {
  28. "voice_type": voice_type,
  29. "encoding": "mp3",
  30. "speed_ratio": 1.0,
  31. "volume_ratio": 1.0,
  32. "pitch_ratio": 1.0,
  33. },
  34. "request": {
  35. "reqid": str(uuid.uuid4()),
  36. "text": "小朋友,看出来小逗是怎么整理的吗?",
  37. "text_type": "plain",
  38. "operation": "query",
  39. "with_frontend": 1,
  40. "frontend_type": "unitTson"
  41. }
  42. }
  43. if __name__ == '__main__':
  44. try:
  45. resp = requests.post(api_url, json.dumps(request_json), headers=header)
  46. print(f"resp body: \n{resp.json()}")
  47. if "data" in resp.json():
  48. data = resp.json()["data"]
  49. file_to_save = open("test_submit.mp3", "wb")
  50. file_to_save.write(base64.b64decode(data))
  51. except Exception as e:
  52. e.with_traceback()