Skip to content

Instantly share code, notes, and snippets.

@kristian-io
Last active February 16, 2022 13:07
Show Gist options
  • Save kristian-io/551689b1277561354e3e35117a6bea56 to your computer and use it in GitHub Desktop.
Save kristian-io/551689b1277561354e3e35117a6bea56 to your computer and use it in GitHub Desktop.
Convert Text To Speech in 20 lines of python code
import requests, json, time
text = """Text to speech technology allows you to convert text of unlimited sizes to humanlike voice audio files!"""
apikey = "YOU_API_KEY" # get your free API key from https://rapidapi.com/k_1/api/large-text-to-speech/
filename = "test-file.wav"
headers = {'content-type': "application/json", 'x-rapidapi-host': "large-text-to-speech.p.rapidapi.com", 'x-rapidapi-key': apikey}
response = requests.request("POST", "https://large-text-to-speech.p.rapidapi.com/tts", data=json.dumps({"text": text}), headers=headers)
id = json.loads(response.text)['id']
eta = json.loads(response.text)['eta']
print(f'Waiting {eta} seconds for the job to finish...')
time.sleep(eta)
response = requests.request("GET", "https://large-text-to-speech.p.rapidapi.com/tts", headers=headers, params={'id': id})
while "url" not in json.loads(response.text):
response = requests.request("GET", "https://large-text-to-speech.p.rapidapi.com/tts", headers=headers, params={'id': id})
print(f'Waiting some more...')
time.sleep(3)
url = json.loads(response.text)['url']
response = requests.request("GET", url)
with open(filename, 'wb') as f:
f.write(response.content)
print(f'File saved to {filename} ! \nOr download here: {url}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment