WordsStory / WordStoryAudioMaker.py
redstoneleo's picture
Upload 5 files
4946780 verified
import json
import utils
import os
# print(os.getenv("OPENAI_API_KEY"))
import StoryWriterAI
import SSMLmaker,MS_TTS_Batch
def phraseList2SSML(phrases):
# 顺序执行 SSMLmaker
wordsPronunciationSSML=SSMLmaker.wordsPronunciation(phrases)
chineseDefinitionSSML,dictResultList = SSMLmaker.chineseDefinitionTTS(phrases)
# 生成 storyJson
storyJson = StoryWriterAI.compose(json.dumps(phrases))
# storyJson = {'en': "Once in a small town, there was a group of people who believed in the **nobility** of helping others. Among them was a young man named Tom. Tom worked at a local company that had a **proprietary** technology. This technology was supposed to make the company stand out in the market. However, for some unknown reason, the company started to **underperform**. Sales were dropping, and the employees were worried. Some of the more paranoid employees even started to spread rumors of a **conspiracy** within the company. They thought that someone was deliberately sabotaging the technology to bring the company down. Tom, on the other hand, didn't believe in these insane (疯狂的) theories. He knew that things could go wrong for many reasons. One day, while walking to work, Tom noticed a big **ditch** on the side of the road. It was a potential hazard. He realized that just like this ditch, problems in the company could be avoided if people were more careful. He also knew that he was **liable** (有责任的) to do something about it. So, he reported the ditch to the local authorities and also tried to find out the real reasons behind the company's poor performance. In the end, it turned out that there was no conspiracy. The company just needed some improvements in its management. Tom's positive attitude and sense of responsibility helped the company get back on track.", 'zh': '在一个小镇上,有一群人坚信帮助他人的**高贵品质**(nobility)。其中有一个叫汤姆的年轻人。汤姆在当地一家拥有**专有**(proprietary)技术的公司工作。这项技术本应让公司在市场上脱颖而出。然而,不知出于什么原因,公司开始**表现不佳**(underperform)。销售额在下降,员工们都很担心。一些疑神疑鬼的员工甚至开始在公司内部散布**阴谋**(conspiracy)的谣言。他们认为有人故意破坏这项技术,想让公司倒闭。另一方面,汤姆不相信这些**疯狂的**(insane)理论。他知道事情可能因为很多原因而出错。有一天,汤姆在上班的路上注意到路边有一个大**沟渠**(ditch)。这是一个潜在的危险。他意识到,就像这个沟渠一样,如果人们更小心一些,公司的问题是可以避免的。他也知道自己有责任(liable)做点什么。于是,他向当地有关部门报告了沟渠的情况,还试图找出公司表现不佳的真正原因。最后,事实证明并没有阴谋。公司只是需要在管理上进行一些改进。汤姆积极的态度和责任感帮助公司回到了正轨。'}
# print(storyJson)
# 生成英文与中文音频
storyEnSSML=SSMLmaker.generalTTS(
storyJson['en'],
voice='en-US-AriaNeural'
)
storyTranslation = storyJson['zh'].replace('**', '')
storyZhSSML=SSMLmaker.generalTTS(
storyTranslation,
voice='zh-CN-YunxiaNeural'
)
ssml_string=f'''
<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="en-US">
{wordsPronunciationSSML}
{storyZhSSML}
{chineseDefinitionSSML}
{storyEnSSML}
{chineseDefinitionSSML}
{wordsPronunciationSSML}
</speak>
'''
return ssml_string,dictResultList,storyJson
def audioStoryMaker(phrases):
outputDir="words-story"
os.makedirs(outputDir, exist_ok=True)
phrasesListStr=', '.join(phrases)
audioBaseName = utils.sanitize_filename(phrasesListStr)
audioPath = os.path.join(outputDir, f"{audioBaseName}.mp3")
ssml_string,dictResultList,storyJson=phraseList2SSML(phrases)
# print(ssml_string)
# audioFilePath=r'C:\Users\22815\AppData\Local\Temp\gradio\ffd10dc5a7c82b17a5ee911feea71edbb9ae9e4b20354b996cfc044f655f42c0\nobility, underperform, insane, liable, conspiracy, proprietary, ditch.mp3'#
audioFilePath=MS_TTS_Batch.tts(ssml_string,audioPath)
# print(phrasesListStr,audioFilePath,dictResultList,storyJson)
return phrasesListStr,audioFilePath,dictResultList,storyJson
if __name__ == "__main__":
audioStoryMaker(['colleague', 'viable', 'crack', 'dissolve', 'reinforce', 'plumber', 'tilt', 'exploration'])