CosyVoice Python SDK

Updated at:

Prerequisites

Quickstart

This example uses the China (Beijing) region. Replace {WorkspaceId} in the code with your workspace ID. The example saves the audio as output.mp3 in the current directory.

# coding=utf-8

import os

import dashscope

# If the environment variable is not configured, replace the following line with your Model Studio API Key: api_key="sk-xxx"
# The following configuration is for the China (Beijing) region. Replace "{WorkspaceId}" with your actual workspace ID. The configuration varies by region.
client = dashscope.Client(
    api_key=os.environ.get('DASHSCOPE_API_KEY'),
    workspace="{WorkspaceId}",
    region="cn-beijing",
)

# Model
model = "cosyvoice-v3-plus"
# Voice
voice = "longanyang"

# Send the text for synthesis and get the binary audio
audio = client.audio.tts.synthesize("How is the weather today?", model=model, voice=voice)

# Save the audio to a local file
with open('output.mp3', 'wb') as f:
    f.write(audio)

Examples and reference