srt / app /text_to_speech.py
badalsahani's picture
Update app/text_to_speech.py
fd664a5
import os
import requests
SPEECH_REGION = os.environ.get("SPEECH_REGION")
SPEECH_KEY = os.environ.get("SPEECH_KEY")
def synthesize_ssml_to_speech(ssml, output_file_path):
url = f"https://{SPEECH_REGION}.tts.speech.microsoft.com/cognitiveservices/v1"
headers = {
'Ocp-Apim-Subscription-Key': SPEECH_KEY,
'Content-Type': 'application/ssml+xml',
'X-Microsoft-OutputFormat': 'audio-16khz-128kbitrate-mono-mp3',
'User-Agent': 'curl'
}
try:
response = requests.request("POST", url, headers=headers, data=ssml.encode("utf-8"))
if response.status_code == 200:
with open(output_file_path, "wb") as f:
f.write(response.content)
print(f"Audio file saved as {output_file_path}")
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
except requests.exceptions.RequestException as e:
print("Request failed with an exception:")
print(e)