Convert text to speech
Convert text into natural-sounding speech using the IBM Text to Speech service.
import requests
import base64
service_url = 'https://api.us-south.text-to-speech.watson.cloud.ibm.com/v1/synthesize'
api_key = 'YOUR_API_KEY'
text_to_synthesize = 'Hello, world!'
headers = {
'Content-Type': 'application/json',
'Accept': 'audio/ogg',
'Authorization': 'Basic ' + base64.b64encode(f'apikey:{api_key}'.encode()).decode()
}
data = {
'text': text_to_synthesize,
'voice': 'en-US_AllisonV3Voice' # Example voice
}
response = requests.post(service_url, headers=headers, json=data, stream=True)
if response.status_code == 200:
with open('output.ogg', 'wb') as audio_file:
for chunk in response.iter_content(chunk_size=1024):
audio_file.write(chunk)
print('Audio saved to output.ogg')
else:
print(f'Error: {response.status_code} - {response.text}')Last 50 checks (10 min intervals)