"""
Example demonstrating full voice cloning workflow and obtaining preview audio.
Note: Set `MINIMAX_API_KEY` in environment variables,
and replace "<your_custom_voice_id>" with your defined voice ID.
"""
import requests
import os
api_key = os.getenv("MINIMAX_API_KEY")
upload_url = "https://api.minimax.io/v1/files/upload"
clone_url = "https://api.minimax.io/v1/voice_clone"
headers = {"Authorization": f"Bearer {api_key}"}
# 1. Upload source audio
with open("/path/to/clone_input.mp3", "rb") as f:
files = {"file": ("clone_input.mp3", f)}
data = {"purpose": "voice_clone"}
response = requests.post(upload_url, headers=headers, data=data, files=files)
file_id = response.json()["file"]["file_id"]
print(f"File ID of the cloned audio: {file_id}")
# 2. Upload example audio
with open("/path/to/clone_prompt.mp3", "rb") as f:
files = {"file": ("clone_prompt.mp3", f)}
data = {"purpose": "prompt_audio"}
response = requests.post(upload_url, headers=headers, data=data, files=files)
prompt_file_id = response.json()["file"]["file_id"]
print(f"File ID of the prompt audio: {prompt_file_id}")
# 3. Clone the voice
clone_payload = {
"file_id": file_id,
"voice_id": "<your_custom_voice_id>",
"clone_prompt": {
"prompt_audio": prompt_file_id,
"prompt_text": "This is a child's voice to be used for cloning."
},
"text": "A gentle breeze passes over the soft grass, accompanied by the fresh scent and birdsong.",
"model": "speech-2.5-hd-preview"
}
clone_headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
response = requests.post(clone_url, headers=clone_headers, json=clone_payload)
print(response.text)