Syntetisera tal

Slutförd

Talsyntes, eller text-till-tal, är motsatsen till tal till text. Det handlar om att skicka text till en modell, som returnerar en ljudström av den röstbaserade texten.

Modeller som stöder text-till-tal-åtgärder är:

  • gpt-4o-tts
  • gpt-4o-mini-tts

Anmärkning

Modelltillgängligheten varierar beroende på region. Granska tabellen för regional tillgänglighet för modellen i Microsoft Foundry-dokumentationen.

Använda en text-till-tal-modell

På samma sätt som med tal-till-text-modeller kan du använda AzureOpenAI-klienten i OpenAI SDK för att ansluta till slutpunkten för din Microsoft Foundry-resurs och ladda upp text till en text-till-tal-modell för talsyntes.

from openai import AzureOpenAI
from pathlib import Path

# Create an AzureOpenAI client
client = AzureOpenAI(
    azure_endpoint=YOUR_FOUNDRY_ENDPOINT,
    api_key=YOUR_FOUNDRY_KEY,
    api_version="2025-03-01-preview"
)

# Path for audio output file
speech_file_path = Path("output_speech.wav")

# Generate speech and save to file
with client.audio.speech.with_streaming_response.create(
            model=YOUR_MODEL_DEPLOYMENT,
            voice="alloy",
            input="This speech was AI-generated!",
            instructions="Speak in an upbeat, excited tone.",
    ) as response:
    response.stream_to_file(speech_file_path)

print(f"Speech generated and saved to {speech_file_path}")