Spaces:
Sleeping
Sleeping
| from xml.sax.saxutils import escape | |
| # from html import escape | |
| import youdaoDict,EnglishDictAI,DictUtils | |
| ZH_VOICE = 'zh-CN-XiaochenMultilingualNeural' | |
| # EN_VOICE = 'en-US-AvaMultilingualNeural'#太喜悦了,不行 | |
| EN_VOICE = "en-US-JaneNeural" | |
| def generalTTS(text, voice=''): | |
| # Escape the text content and the voice name (in case it ever contains special chars) | |
| wordsSpeech = f''' | |
| <voice name="{escape(voice)}"> | |
| {escape(text)} | |
| </voice> | |
| ''' | |
| return wordsSpeech | |
| def wordsPronunciation(phraseList): | |
| # Escape each phrase in the list *before* joining them | |
| escaped_phrases = (escape(phrase) for phrase in phraseList) | |
| joined_phrases = '<break time="1s"/>'.join(escaped_phrases) | |
| wordsSpeech = f''' | |
| <voice name="{escape(EN_VOICE)}"> | |
| {joined_phrases} | |
| </voice> | |
| ''' | |
| return wordsSpeech | |
| def chineseDefinitionTTS(phraseList): | |
| phraseMeaningSSML = '' | |
| dictResultList = [] | |
| for phrase in phraseList: | |
| dictResult = youdaoDict.query(phrase) | |
| if not dictResult.get('definition'): | |
| dictResult = EnglishDictAI.query(phrase) | |
| dictResultList.append(dictResult) | |
| chineseDefinition = dictResult.get('definition', '') # Default to empty string if no definition | |
| # Ensure definition is a string before escaping (in case it's None) | |
| spokenChineseDefinition = DictUtils.toSpokendefinition(chineseDefinition) | |
| if spokenChineseDefinition is None: | |
| spokenChineseDefinition = "" | |
| # Escape all variable content being inserted into the SSML | |
| escaped_phrase = escape(phrase) | |
| escaped_spell_out = escape(phrase if ' ' not in phrase else '') | |
| escaped_chinese_def = escape(spokenChineseDefinition) | |
| phraseMeaningSSML += f''' | |
| <voice name="{escape(EN_VOICE)}"> | |
| {escaped_phrase} | |
| <p><say-as interpret-as="characters">{escaped_spell_out}</say-as></p> | |
| {escaped_phrase} | |
| </voice> | |
| <voice name="{escape(ZH_VOICE)}"> | |
| <lang xml:lang="zh-CN"> | |
| <s>{escaped_chinese_def}</s> | |
| </lang> | |
| </voice> | |
| ''' | |
| return phraseMeaningSSML, dictResultList | |
| if __name__ == "__main__": | |
| # chineseDefinitionTTS(['colleague', 'viable', 'crack', 'dissolve', 'reinforce', 'plumber', 'tilt', 'exploration'],'outputAudioPath.mp3') | |
| # wordsPronunciation(['colleague', 'viable', 'crack', 'dissolve', 'reinforce', 'plumber', 'tilt', 'exploration'],'outputAudioPath.mp3') | |
| words='''ditch | |
| preparatory | |
| immense | |
| snare drum | |
| stall | |
| inhibit | |
| anchor | |
| priest | |
| affordance | |
| bump | |
| integrity | |
| eavesdrop | |
| monetary | |
| prerequisite | |
| aggregate | |
| benign | |
| keynote | |
| cognitive | |
| apathy | |
| nobility | |
| underperform | |
| insane | |
| liable | |
| conspiracy | |
| proprietary | |
| ditch | |
| nuance | |
| bicker | |
| addictive | |
| logarithm | |
| intimately | |
| peculiarly | |
| eligible | |
| apt | |
| attribute | |
| bizarre | |
| misfortune | |
| onlooker | |
| precursor | |
| curriculum | |
| phobia | |
| impedance | |
| hands-off | |
| introspection | |
| decay | |
| exploration | |
| deposit | |
| quarterly | |
| workbench | |
| phenomenon | |
| indefinitely | |
| bracket | |
| invariably | |
| clumsiness | |
| dutiful | |
| err | |
| entrench | |
| intrinsic | |
| pin | |
| calamity | |
| collaborate | |
| utterance | |
| rudimentary | |
| grievous | |
| rectify | |
| flexibility | |
| smoothly | |
| aerospace | |
| saturated | |
| realism | |
| bummer | |
| prominent | |
| impact | |
| spatial | |
| semantic | |
| appliance | |
| deficient | |
| insurance | |
| prohibitive | |
| anonymous | |
| tuition | |
| doctoral | |
| master | |
| bachelor | |
| fringe | |
| affiliate | |
| pointless | |
| emphasize | |
| contractor | |
| plumber | |
| contiguity | |
| curriculum vitae | |
| dialect | |
| domestic | |
| nerd | |
| faithfully | |
| quarter | |
| ''' | |
| wordsPronunciation(words.split()) | |