Cheezecaike
Cheezecaike

Reputation: 1

Azure TTS, Python SDK Error 1007: Data at the root level is invalid

Getting this error code when passing xml to Azure, assuming the issue lies with how I've got the xml set up.

Here is my code building the xml:

from lxml.builder import ElementMaker

ns = {
            None: "https://www.w3.org/2001/10/synthesis",
            "mstts": "https://www.w3.org/2001/mstts",
            "xml": "http://www.w3.org/XML/1998/namespace",
        }

E = ElementMaker(namespace=ns[None], nsmap=ns)
TTS = ElementMaker(namespace=ns['mstts'])

xml_body = E.speak(
    {"version": "1.0",
    "{http://www.w3.org/XML/1998/namespace}lang": "en-US"},
    E.voice(
        {"name": "en-US-JessaNeural"},
        TTS.express_as(
            "That'd be just amazing!",
            type="cheerful",
        )
    )
)

ssml_string = ET.tostring(xml_body, encoding='utf-8', method='xml')
print(ssml_string)

This is the output of print(str(ssml_string)):

b'<ns0:speak xmlns:ns0="https://www.w3.org/2001/10/synthesis" xmlns:ns1="https://www.w3.org/2001/mstts" version="1.0" xml:lang="en-US"><ns0:voice name="en-US-JessaNeural"><ns1:express_as type="cheerful">That\'d be just amazing!</ns1:express_as></ns0:voice></ns0:speak>'

**Error Details:**

Error code: 1007. Error details: Data at the root level is invalid. Line 1, position 1.

Any help is appreciated!

I've tried Googling and most of what I've found is that this error happens when you've incorrectly formatted your xml or missed entering something.

Upvotes: 0

Views: 56

Answers (1)

Dasari Kamali
Dasari Kamali

Reputation: 3649

The error you encountered

(Error 1007: Data at the root level is invalid)

  • Is due to the incorrect format of XML and issues with namespaces in the SSML.
  • To resolve this, below is the corrected code snippet that generates and handles SSML (Speech Synthesis Markup Language) using the Azure Cognitive Services Speech SDK.
  • This code ensures that the SSML is properly formatted, includes the necessary XML declaration, and uses the correct namespaces.

Code :

import os
from lxml import etree
from lxml.builder import ElementMaker
import azure.cognitiveservices.speech as speechsdk

ns = {
    None: "http://www.w3.org/2001/10/synthesis",
    "mstts": "http://www.w3.org/2001/mstts",
    "xml": "http://www.w3.org/XML/1998/namespace"
}

E = ElementMaker(namespace=ns[None], nsmap=ns)
TTS = ElementMaker(namespace=ns['mstts'])

xml_body = E.speak(
    {"version": "1.0", "{http://www.w3.org/XML/1998/namespace}lang": "en-US"},
    E.voice(
        {"name": "en-US-JessaNeural"},
        TTS.express_as(
            "That'd be just amazing!",
            type="cheerful"
        )
    )
)

ssml_string = etree.tostring(xml_body, encoding='utf-8', pretty_print=True, xml_declaration=True)
print(ssml_string.decode('utf-8'))

speech_key = "<speech_key>"
service_region = "<speech_region>"

speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
result = synthesizer.speak_ssml_async(ssml_string.decode('utf-8')).get()

if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
    print("Speech synthesized to speaker for text [{}]".format(ssml_string))
elif result.reason == speechsdk.ResultReason.Canceled:
    cancellation_details = result.cancellation_details
    print("Speech synthesis canceled: {}".format(cancellation_details.reason))
    if cancellation_details.reason == speechsdk.CancellationReason.Error:
        print("Error details: {}".format(cancellation_details.error_details))

Output :

The following code ran successfully and I heard the speech output for the text as below.

enter image description here

Upvotes: 0

Related Questions