Coding with Python and using azure cognitive services text to speech. I have Arabic text, and I want to generate the corresponding mp3 speech audio :
input_text="هذا المحتوى مجاني، لذلك لدعم القناة لمزيد من المحتوى المجاني، يرجى الاشتراك، مثل، مشاركة، تعليق"
def generate_speech(self,language_id, input_text, outfile, token):
url = "https://{}.tts.speech.microsoft.com/cognitiveservices/v1".format(self.azure_location)
print("input_text:"+input_text)
header = {
'Authorization': 'Bearer '+str(token),
'Content-Type': 'application/ssml+xml',
'X-Microsoft-OutputFormat': 'audio-24khz-160kbitrate-mono-mp3'
}
data = "<speak version='1.0' xml:lang='ar-SY'>\
<voice xml:lang='ar-SY' xml:gender='Male' name='ar-SY-LaithNeural'>\
{}\
</voice>\
</speak>".format(input_text)
try:
response = requests.post(url, headers=header, data=data)
response.raise_for_status()
with open(outfile, "wb") as file:
file.write(response.content)
print(response)
response.close()
except Exception as e:
print("ERROR: ", e)
I get the following error:
ERROR: 'latin-1' codec can't encode characters in position 127-129: Body ('هذا') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.