change from chaterbox to gwen-tts
This commit is contained in:
@@ -30,11 +30,11 @@ dependencies = [
|
|||||||
"pinyin-tone-converter",
|
"pinyin-tone-converter",
|
||||||
"genanki",
|
"genanki",
|
||||||
"argostranslate",
|
"argostranslate",
|
||||||
"chatterbox-tts",
|
|
||||||
"torch",
|
"torch",
|
||||||
"torchaudio",
|
"torchaudio",
|
||||||
"torchcodec",
|
"torchcodec",
|
||||||
"python-dotenv",
|
"python-dotenv",
|
||||||
|
"qwen-tts",
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -18,3 +18,6 @@ HF_TOKEN = os.environ.get("HF_TOKEN")
|
|||||||
default_path = Path.home() / "anki-hsk-creator-data"
|
default_path = Path.home() / "anki-hsk-creator-data"
|
||||||
DATA_FOLDER = Path(os.environ.get("DATA_FOLDER", default_path))
|
DATA_FOLDER = Path(os.environ.get("DATA_FOLDER", default_path))
|
||||||
DATA_FOLDER.mkdir(exist_ok=True, parents=True)
|
DATA_FOLDER.mkdir(exist_ok=True, parents=True)
|
||||||
|
default_gwen = Path.home() / "Gwen"
|
||||||
|
GWEN_FOLDER = Path(os.environ.get("GWEN_FOLDER", default_gwen))
|
||||||
|
GWEN_FOLDER.mkdir(exist_ok=True, parents=True)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
import importlib.resources
|
import importlib.resources
|
||||||
|
|
||||||
# Local
|
# Local
|
||||||
from . import DATA_FOLDER
|
from . import DATA_FOLDER, GWEN_FOLDER
|
||||||
|
|
||||||
# Resources
|
# Resources
|
||||||
CCCEDICT_PATH = importlib.resources.files("anki_hsk_creator").joinpath("cedict_ts.u8")
|
CCCEDICT_PATH = importlib.resources.files("anki_hsk_creator").joinpath("cedict_ts.u8")
|
||||||
@@ -16,16 +16,13 @@ OUTPUT = DATA_FOLDER / "output"
|
|||||||
OUTPUT.mkdir(exist_ok=True, parents=True)
|
OUTPUT.mkdir(exist_ok=True, parents=True)
|
||||||
RESOURCES = DATA_FOLDER / "resources"
|
RESOURCES = DATA_FOLDER / "resources"
|
||||||
RESOURCES.mkdir(exist_ok=True, parents=True)
|
RESOURCES.mkdir(exist_ok=True, parents=True)
|
||||||
|
GWEN_TTS = GWEN_FOLDER / "Qwen3-TTS-12Hz-1.7B-CustomVoice"
|
||||||
|
|
||||||
# File Types
|
# File Types
|
||||||
PHRASES_TYPE = ".phrases"
|
PHRASES_TYPE = ".phrases"
|
||||||
DICT_TYPE = ".dictionary"
|
DICT_TYPE = ".dictionary"
|
||||||
DICTATION_TYPE = ".dictation"
|
DICTATION_TYPE = ".dictation"
|
||||||
|
|
||||||
# time dalays
|
|
||||||
WORD = 0.1
|
|
||||||
COMMA = 0.5
|
|
||||||
POINT = 1.0
|
|
||||||
|
|
||||||
class LANGUAGES:
|
class LANGUAGES:
|
||||||
"""Available laguages for translation"""
|
"""Available laguages for translation"""
|
||||||
@@ -37,7 +34,6 @@ class LANGUAGES:
|
|||||||
RU = "ru"
|
RU = "ru"
|
||||||
TR = "tr"
|
TR = "tr"
|
||||||
TH = "th"
|
TH = "th"
|
||||||
|
|
||||||
AvailableLanguages = (EN, ES, FR, RU, TR, TH)
|
AvailableLanguages = (EN, ES, FR, RU, TR, TH)
|
||||||
LanguageNames = {
|
LanguageNames = {
|
||||||
EN: "English",
|
EN: "English",
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import argostranslate.translate
|
|||||||
import torchaudio
|
import torchaudio
|
||||||
|
|
||||||
# Local
|
# Local
|
||||||
from .constants import LANGUAGES, WORD, COMMA
|
from .constants import LANGUAGES
|
||||||
from .utility import CCCEDICT, TTS, DictionaryResult, ProcessFile, TranslationResult
|
from .utility import CCCEDICT, TTS, DictionaryResult, ProcessFile, TranslationResult
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
@@ -31,10 +31,8 @@ def dictation_process(
|
|||||||
audio_line = audio_line.replace(",", ",。。。]")
|
audio_line = audio_line.replace(",", ",。。。]")
|
||||||
audio_path = process_file.resources / f"N{n:03n}.wav"
|
audio_path = process_file.resources / f"N{n:03n}.wav"
|
||||||
if not audio_path.exists():
|
if not audio_path.exists():
|
||||||
audio = TTS.MODEL.generate(
|
wavs, sr = TTS.generate(f"{audio_line}。")
|
||||||
f"{audio_line}。", language_id=LANGUAGES.CN, **TTS.DEFAULTS
|
torchaudio.save(audio_path, wavs[0], sr)
|
||||||
)
|
|
||||||
torchaudio.save(audio_path, audio, TTS.MODEL.sr)
|
|
||||||
translated = argostranslate.translate.translate(
|
translated = argostranslate.translate.translate(
|
||||||
line, LANGUAGES.CN, process_file.language_id
|
line, LANGUAGES.CN, process_file.language_id
|
||||||
)
|
)
|
||||||
@@ -56,10 +54,8 @@ def translator_process(
|
|||||||
audio_line = audio_line.replace(",", ",。。。]")
|
audio_line = audio_line.replace(",", ",。。。]")
|
||||||
audio_path = process_file.resources / f"N{n:03n}.wav"
|
audio_path = process_file.resources / f"N{n:03n}.wav"
|
||||||
if not audio_path.exists():
|
if not audio_path.exists():
|
||||||
audio = TTS.MODEL.generate(
|
wavs, sr = TTS.generate(f"{audio_line}。")
|
||||||
f"{audio_line}。", language_id=LANGUAGES.CN, **TTS.DEFAULTS
|
torchaudio.save(audio_path, wavs[0], sr)
|
||||||
)
|
|
||||||
torchaudio.save(audio_path, audio, TTS.MODEL.sr)
|
|
||||||
translated = argostranslate.translate.translate(
|
translated = argostranslate.translate.translate(
|
||||||
line, LANGUAGES.CN, process_file.language_id
|
line, LANGUAGES.CN, process_file.language_id
|
||||||
)
|
)
|
||||||
@@ -121,10 +117,8 @@ def dictionary_process(process_file: ProcessFile) -> list[DictionaryResult]:
|
|||||||
for line in reader:
|
for line in reader:
|
||||||
audio_path = process_file.resources / f"{line['pinyin']}.wav"
|
audio_path = process_file.resources / f"{line['pinyin']}.wav"
|
||||||
if not audio_path.exists():
|
if not audio_path.exists():
|
||||||
audio = TTS.MODEL.generate(
|
wavs, sr = TTS.generate(f"{line['simplified']}。")
|
||||||
f"{line['simplified']}。", language_id=LANGUAGES.CN, **TTS.DEFAULTS
|
torchaudio.save(audio_path, wavs[0], sr)
|
||||||
)
|
|
||||||
torchaudio.save(audio_path, audio, TTS.MODEL.sr)
|
|
||||||
print(line)
|
print(line)
|
||||||
result = DictionaryResult(
|
result = DictionaryResult(
|
||||||
**line, audio_path=audio_path, language_id=process_file.language_id
|
**line, audio_path=audio_path, language_id=process_file.language_id
|
||||||
|
|||||||
@@ -12,10 +12,10 @@ import argostranslate.package
|
|||||||
import argostranslate.translate
|
import argostranslate.translate
|
||||||
import torch
|
import torch
|
||||||
from cedict_utils.cedict import CedictEntry, CedictParser
|
from cedict_utils.cedict import CedictEntry, CedictParser
|
||||||
from chatterbox.mtl_tts import ChatterboxMultilingualTTS
|
from qwen_tts import Qwen3TTSModel
|
||||||
|
|
||||||
# Local
|
# Local
|
||||||
from .constants import CCCEDICT_PATH, INPUT, LANGUAGES, OUTPUT, RESOURCES
|
from .constants import CCCEDICT_PATH, GWEN_TTS, INPUT, LANGUAGES, OUTPUT, RESOURCES
|
||||||
|
|
||||||
# Static Clases
|
# Static Clases
|
||||||
|
|
||||||
@@ -50,10 +50,12 @@ class TRANS:
|
|||||||
if out_package.to_code == to_code:
|
if out_package.to_code == to_code:
|
||||||
if in_package.to_code == out_package.from_code:
|
if in_package.to_code == out_package.from_code:
|
||||||
print(
|
print(
|
||||||
f"Check in_package {in_package.from_code} {in_package.to_code}"
|
f"Check in_package {in_package.from_code}"
|
||||||
|
f"{in_package.to_code}"
|
||||||
)
|
)
|
||||||
print(
|
print(
|
||||||
f"Check out_package {out_package.from_code} {out_package.to_code}"
|
f"Check out_package {out_package.from_code}"
|
||||||
|
f"{out_package.to_code}"
|
||||||
)
|
)
|
||||||
packages_to_install.append(in_package)
|
packages_to_install.append(in_package)
|
||||||
packages_to_install.append(out_package)
|
packages_to_install.append(out_package)
|
||||||
@@ -137,7 +139,11 @@ class TTS:
|
|||||||
|
|
||||||
MODEL = None
|
MODEL = None
|
||||||
DEVICE = None
|
DEVICE = None
|
||||||
DEFAULTS = {"cfg_weight": 0.2, "exaggeration": 0.8}
|
DEFAULTS = {
|
||||||
|
"language": "Chinese",
|
||||||
|
"speaker": "Uncle_Fu",
|
||||||
|
"instruct": "语速缓慢而审慎,每个音节的语调都拿捏得恰到好处,宛如教授在指导新生。",
|
||||||
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_tts():
|
def create_tts():
|
||||||
@@ -151,10 +157,18 @@ class TTS:
|
|||||||
else:
|
else:
|
||||||
TTS.DEVICE = "cpu"
|
TTS.DEVICE = "cpu"
|
||||||
if TTS.MODEL is None:
|
if TTS.MODEL is None:
|
||||||
TTS.MODEL = ChatterboxMultilingualTTS.from_pretrained(
|
TTS.MODEL = Qwen3TTSModel.from_pretrained(
|
||||||
device=TTS.DEVICE, t3_model="v3"
|
GWEN_TTS,
|
||||||
|
device_map=TTS.DEVICE,
|
||||||
|
dtype=torch.bfloat16,
|
||||||
|
# attn_implementation="flash_attention_2",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def generate(text: str):
|
||||||
|
"""Generates a Waw using the defaulst values"""
|
||||||
|
return TTS.MODEL.generate_custom_voice(text=text, **TTS.DEFAULTS)
|
||||||
|
|
||||||
|
|
||||||
# Clases
|
# Clases
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user