Skip to content

Instantly share code, notes, and snippets.

@reuben
Last active April 1, 2023 18:09
Show Gist options
  • Save reuben/997e0ff953021d3141d9e49131687947 to your computer and use it in GitHub Desktop.
Save reuben/997e0ff953021d3141d9e49131687947 to your computer and use it in GitHub Desktop.
Script to create a custom voice in the Coqui API
import sys
import requests
import argparse
import json
import os
from requests_toolbelt.multipart.encoder import MultipartEncoder
try:
API_TOKEN = os.environ["COQUI_API_TOKEN"]
except KeyError:
print(
"Set the COQUI_API_TOKEN environment variable with your API token to use this script.",
file=sys.stderr,
)
sys.exit(1)
def create_voice(voice_name: str, file_path: str) -> dict:
# Open the file in binary mode
with open(file_path, "rb") as file:
# Create a multi-part encoded request
multipart_data = MultipartEncoder(
fields={
"name": voice_name,
"file": ("audio", file, "application/octet-stream"),
}
)
# Set the headers for the request
headers = {
"Content-Type": multipart_data.content_type,
"Authorization": f"Bearer {API_TOKEN}",
}
# Make the POST request
response = requests.post(
"https://app.coqui.ai/api/v2/voices", data=multipart_data, headers=headers
)
# Return the response JSON
return response.json()
def main():
parser = argparse.ArgumentParser(description="Create a custom voice clone.")
parser.add_argument("name", help="The name of the custom voice.")
parser.add_argument("file_path", help="The path to the file to be sent.")
args = parser.parse_args()
# Make the POST request
response_json = create_voice(args.name, args.file_path)
# Print the response JSON
print(json.dumps(response_json, indent=2))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment