scstt.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # DashScope SDK 版本不低于 1.24.6
  2. import os
  3. import requests
  4. import dashscope
  5. # 以下为北京地域url,若使用新加坡地域的模型,需将url替换为:https://dashscope-intl.aliyuncs.com/api/v1
  6. dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'
  7. text = """Ella loves her grandpa. He comes to visit her about once a month. She wishes he came to visit every week because he tells great stories and makes her favorite dinner. Ella asks her mom to make the barbeque chicken that Grandpa makes. Ella's mother says, "I will try, but no one makes it as well as Grandpa does." Ella watches her mom prepare the sauces for the chicken. It looks tasty. When the chicken is finished, her mom was right. No one makes it as well as Grandpa."""
  8. response = dashscope.MultiModalConversation.call(
  9. model="qwen3-tts-flash",
  10. # 新加坡和北京地域的API Key不同。获取API Key:https://help.aliyun.com/zh/model-studio/get-api-key
  11. # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key = "sk-xxx"
  12. api_key="sk-92f2fec23dc94de49a31c9945d04e5f7",
  13. text=text,
  14. voice="Eric",
  15. language_type="English", # 建议与文本语种一致,以获得正确的发音和自然的语调。
  16. stream=False
  17. )
  18. # 检查API响应
  19. if response.status_code != 200:
  20. exit(1)
  21. if response.output is None:
  22. exit(1)
  23. if response.output.audio is None:
  24. exit(1)
  25. audio_url = response.output.audio.url
  26. save_path = "./音色/四川-程川-Eric.wav" # 自定义保存路径
  27. try:
  28. http_response = requests.get(audio_url)
  29. http_response.raise_for_status() # 检查请求是否成功
  30. with open(save_path, 'wb') as f:
  31. f.write(http_response.content)
  32. except Exception as e:
  33. pass