Refactors the code based on PR comments and adds the relevant changelog entry.

This commit is contained in:
Yohan Liyanage
2025-08-13 16:34:33 +05:30
parent 248206e234
commit 4bcca7956e
2 changed files with 21 additions and 10 deletions

View File

@@ -72,6 +72,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `on_transcription_stopped` and `on_transcription_error` to Daily
callbacks.
- Added SSML reserved character escaping to `AzureBaseTTSService` to properly handle special characters in text sent to Azure TTS. This fixes an issue where characters like `&`, `<`, `>`, `"`, and `'` in LLM-generated text would cause TTS failures.
-
### Changed
- Changed the default `url` for `NeuphonicTTSService` to

View File

@@ -68,6 +68,16 @@ class AzureBaseTTSService(TTSService):
construction, voice configuration, and parameter management.
"""
# Define SSML escape mappings based on SSML reserved characters
# See - https://learn.microsoft.com/en-us/azure/ai-services/speech-service/speech-synthesis-markup-structure
SSML_ESCAPE_CHARS = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&apos;",
}
class InputParams(BaseModel):
"""Input parameters for Azure TTS voice configuration.
@@ -128,14 +138,6 @@ class AzureBaseTTSService(TTSService):
"volume": params.volume,
}
# Define SSML escape mappings based on SSML reserved characters
# See - https://learn.microsoft.com/en-us/azure/ai-services/speech-service/speech-synthesis-markup-structure
self.ssml_escape_chars = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;'
}
self._api_key = api_key
self._region = region
self._voice_id = voice
@@ -210,7 +212,14 @@ class AzureBaseTTSService(TTSService):
return ssml
def _escape_text(self, text: str) -> str:
"""Escape special characters in text for SSML.
"""Escapes XML/SSML reserved characters according to Microsoft documentation.
This method escapes the following characters:
- & becomes &amp;
- < becomes &lt;
- > becomes &gt;
- " becomes &quot;
- ' becomes &apos;
Args:
text: The text to escape.
@@ -219,7 +228,7 @@ class AzureBaseTTSService(TTSService):
The escaped text.
"""
escaped_text = text
for char, escape_code in self.ssml_escape_chars.items():
for char, escape_code in AzureBaseTTSService.SSML_ESCAPE_CHARS.items():
escaped_text = escaped_text.replace(char, escape_code)
return escaped_text