updATE MODELS for audio generation

This commit is contained in:
Wolfang Torres
2026-05-27 23:59:19 +08:00
parent e35bcf6d74
commit ec16342523
118 changed files with 159 additions and 83 deletions

View File

@@ -44,34 +44,34 @@ font-size: 75px;
## Classess
SIMPLE_MODEL = Model(
PHRASE_MODEL = Model(
2076166425,
"Simple Model",
"Phrase Model",
fields=[
{"name": "Question"},
{"name": "Answer"},
{"name": "Translated"},
{"name": "Phrase"},
{"name": "Audio"},
],
templates=[
{
"name": "Card 1",
"qfmt": "{{Question}}",
"afmt": '{{FrontSide}}<hr id="answer">{{Answer}}',
"qfmt": "{{Translated}}<br>{{Audio}}",
"afmt": '{{FrontSide}}<hr id="answer">{{Phrase}}',
},
{
"name": "Card 2",
"qfmt": "{{Phrase}}<br>{{Audio}}",
"afmt": '{{FrontSide}}<hr id="answer">{{Translated}}',
},
{
"name": "Card 3",
"qfmt": "{{Audio}}",
"afmt": '{{FrontSide}}<hr id="answer">{{Phrase}}',
},
],
css=CSS,
)
HSK_FRONT_TEMPLATE = """
<tts service="android" voice="zh-CN">
<strong>{{Pinyin}}</strong>
</tts>
<br>
<tts service="android" voice="en-US">
{{English}}
</tts>
<br>
{{Audio}}
"""
HSK_MODEL = Model(
1708536519,
@@ -81,12 +81,12 @@ HSK_MODEL = Model(
{"name": "Pinyin"},
{"name": "Simplified"},
{"name": "Traditional"},
{'name': 'Audio'},
{"name": "Audio"},
],
templates=[
{
"name": "Card 1",
"qfmt": HSK_FRONT_TEMPLATE,
"qfmt": "<strong>{{Pinyin}}</strong><br>{{English}}<br>{{Audio}}",
"afmt": "{{FrontSide}}<hr id='answer''><div class='simple'>"
"{{Simplified}}</div><br><div class='trad'>{{Traditional}}</div>",
},
@@ -96,6 +96,12 @@ HSK_MODEL = Model(
"{{Traditional}}</div>",
"afmt": '{{FrontSide}}<hr id="answer"><strong>{{Pinyin}}</strong><br>{{English}}<br>{{Audio}}',
},
{
"name": "Card 1",
"qfmt": "{{Audio}}",
"afmt": "{{FrontSide}}<hr id='answer''><strong>{{Pinyin}}</strong><br><div class='simple'>"
"{{Simplified}}</div><br><div class='trad'>{{Traditional}}</div>",
},
],
css=CSS,
)
@@ -129,6 +135,7 @@ def create_translator():
)
argostranslate.package.install_from_path(package_to_install.download())
def create_tts():
# Automatically detect the best available device
if torch.cuda.is_available():
@@ -137,9 +144,10 @@ def create_tts():
device = "mps"
else:
device = "cpu"
tts = ChatterboxMultilingualTTS.from_pretrained(device=device)
tts = ChatterboxMultilingualTTS.from_pretrained(device=device, t3_model="v3")
return tts
## Main
@@ -193,8 +201,14 @@ def dictionary_process(dictionary, tts, in_file, resources):
for m in w.meanings:
print(f"\t{m}")
s = None
while not s or not s.isnumeric() or not (1 <= int(s) <= len(v)):
s = input(f"Please select the correct word [1-{len(v)}]: ")
while (
not s
or not s.isnumeric()
or not (1 <= int(s) <= len(v))
):
s = input(
f"Please select the correct word [1-{len(v)}]: "
)
v = v[int(s) - 1]
else:
v = ml[0]
@@ -202,7 +216,7 @@ def dictionary_process(dictionary, tts, in_file, resources):
v = v[0]
audio_path = resources / f"{word}.wav"
if not audio_path.exists():
audio = tts.generate(word, language_id="zh")
audio = tts.generate(f"{word}", language_id="zh")
torchaudio.save(audio_path, audio, tts.sr)
input_file.write(f"{word}\t{v.pinyin}\n")
results.append((v, audio_path))
@@ -217,16 +231,18 @@ def dictionary_process(dictionary, tts, in_file, resources):
return results
def translator_process(in_file):
"""Process text trasnlate files"""
text_list = in_file.open(encoding="utf8").read().split()[1:]
def translator_process(tts, resources, in_file):
"""Process for phases trasnlation"""
text_list = in_file.open(encoding="utf8").read().strip().split()
results = []
for text in text_list:
text = text.strip()
for par in text.split(""):
if par:
translatedText = argostranslate.translate.translate(par, CN, EN)
results.append([translatedText, par])
for n, phrase in enumerate(text_list):
phrase = phrase.strip()
audio_path = resources / f"N{n}.wav"
if not audio_path.exists():
audio = tts.generate(f"{phrase}", language_id="zh")
torchaudio.save(audio_path, audio, tts.sr)
translated = argostranslate.translate.translate(phrase, CN, EN)
results.append([translated, phrase, audio_path])
return results
@@ -260,7 +276,7 @@ def output_anki_dictionary(out_file, results):
PinyinToneConverter().convert_text(entry.pinyin),
entry.simplified,
entry.traditional,
f"[sound:{audio.name}]"
f"[sound:{audio.name}]",
],
)
audios.append(audio)
@@ -269,32 +285,41 @@ def output_anki_dictionary(out_file, results):
package.write_to_file(final_file)
def output_anki_text(out_file, results):
def output_anki_phrase(out_file, results):
final_file = out_file.parent / f"{out_file.stem}.apkg"
deck_name = "::".join(out_file.relative_to(OUTPUT).parts[:-1] + (out_file.stem,))
deck = Deck(random.randrange(1 << 30, 1 << 31), deck_name)
for entry in results:
package = Package(deck)
audios = []
for translated, phrase, audio in results:
note = Note(
model=SIMPLE_MODEL,
fields=entry,
model=PHRASE_MODEL,
fields=[
translated,
phrase,
f"[sound:{audio.name}]",
],
)
deck.add_note(note)
Package(deck).write_to_file(final_file)
audios.append(audio)
package.media_files = audios
package.write_to_file(final_file)
def main():
in_file, out_file, resources, file_type = process_files()
if PHRASES_TYPE in in_file.suffixes:
create_translator()
results = translator_process(in_file)
output_anki_text(out_file, results)
elif DICT_TYPE in in_file.suffixes:
tts = create_tts()
dictionary = create_cedict()
results = dictionary_process(dictionary, tts, in_file, resources)
output_anki_dictionary(out_file, results)
else:
raise TypeError("Error, filetype not especified!")
tts = create_tts()
dictionary = create_cedict()
create_translator()
while True:
in_file, out_file, resources, file_type = process_files()
if PHRASES_TYPE in in_file.suffixes:
results = translator_process(tts, resources, in_file)
output_anki_phrase(out_file, results)
elif DICT_TYPE in in_file.suffixes:
results = dictionary_process(dictionary, tts, in_file, resources)
output_anki_dictionary(out_file, results)
else:
raise TypeError("Error, filetype not especified!")
if __name__ == "__main__":

View File

@@ -1,4 +1,4 @@
谢谢
不客气
再见
谢谢 xie4 xie5
bu4
不客气 bu4 ke4 qi5
再见 zai4 jian4

View File

@@ -1,2 +1,7 @@
不谢
谢谢你
打开书
清大声读
再读一遍
一起读
有问题吗?

View File

@@ -1,12 +1,12 @@
什么
名字
老师
学生
李明
中国
美国
jiao4
什么 shen2 me5
名字 ming2 zi5
wo3
shi4
老师 lao3 shi1
ma5
学生 xue2 sheng5
ren2
李明 ERROR
中国 Zhong1 guo2
美国 Mei3 guo2

View File

@@ -0,0 +1,6 @@
你叫什么名字?
我叫李月。
你是老师吗?
我不是老师, 我是学生。
你中国人么?
我不是中国人,我是美国人。

View File

@@ -1,12 +1,12 @@
汉语
同学
朋友
委内瑞拉
ta1
ta1
shei2
de5
汉语 Han4 yu3
na3
guo2
ne5
ta1
同学 tong2 xue2
朋友 peng2 you5
委内瑞拉 Wei3 nei4 rui4 la1

View File

@@ -0,0 +1,9 @@
他是谁?
他是我的汉语老师,他叫李月。
你是哪国人?
我是美国人,你呢?
我是中国人。
他是谁?
他是我同学。
他呢?他是你同学吗?
他不是我同学,她是我朋友。

View File

@@ -0,0 +1,9 @@
他是谁?
他是我的汉语老师,他叫李月。
你是哪国人?
我是美国人,你呢?
我是中国人。
他是谁?
他是我同学。
他呢?他是你同学吗?
他不是我同学,她是我朋友。

View File

@@ -0,0 +1,14 @@
温柔 wen1 rou2
星星 xing1 xing5
摘 zhai1
颗 ke1
亲手 qin1 shou3
浪漫 lang4 man4
飞行员 fei1 xing2 yuan2
标准 biao1 zhun3
考虑 kao3 lu:4
十全十美 shi2 quan2 shi2 mei3
理想 li3 xiang3
支持 zhi1 chi2
性格 xing4 ge2
恋爱 lian4 ai4

View File

@@ -0,0 +1,8 @@
她每年都会送孩子新年礼物
有一年,孩子要星星,她没办法去亲手,就摘颗送给他。
有些人在选择男/女朋友时总是有很高的标准,会考虑很多方面。
但是世界上哪有理想的人啊?
我觉得十全十美的男/女朋友就是在一起时每天都很开心。
我理想中的女朋友有两个标准:
第一,性格要非常温柔;
第二,要支挡我的工作,即使。

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More