895669 2025-05-06 16:12:07

OpenAI使用硅基流动文本转语音

python python
from pathlib import Path
from openai import OpenAI

speech_file_path = Path(__file__).parent / "C:\\Downloads\\out.mp3" # 保存地址

client = OpenAI(
    api_key="sk-xxxxxxxxxxxxxxx",
    base_url="https://api.siliconflow.cn/v1"
)

with client.audio.speech.with_streaming_response.create(
  model="FunAudioLLM/CosyVoice2-0.5B", # 支持 fishaudio / GPT-SoVITS / CosyVoice2-0.5B 系列模型
  voice="FunAudioLLM/CosyVoice2-0.5B:alex", # 系统预置音色
  # 用户输入信息
  input="你能用高兴的情感说吗?<|endofprompt|>今天真是太开心了,马上要放假了!I'm so happy, Spring Festival is coming!",
  response_format="mp3" # 支持 mp3, wav, pcm, opus 格式
) as response:
    response.stream_to_file(speech_file_path)

    print(speech_file_path)

评论

首页 - Wiki
Copyright © 2011-2025 iteam. Current version is 2.143.0. UTC+08:00, 2025-05-17 12:07
浙ICP备14020137号-1 $访客地图$