add bulk procesing
This commit is contained in:
@@ -5,6 +5,7 @@ Static clasess and functions for general use
|
||||
"""
|
||||
|
||||
# Standard Library
|
||||
import random
|
||||
from pathlib import Path
|
||||
|
||||
# Pip
|
||||
@@ -15,7 +16,17 @@ from cedict_utils.cedict import CedictEntry, CedictParser
|
||||
from qwen_tts import Qwen3TTSModel
|
||||
|
||||
# Local
|
||||
from .constants import CCCEDICT_PATH, GWEN_TTS, INPUT, LANGUAGES, OUTPUT, RESOURCES
|
||||
from .constants import (
|
||||
CCCEDICT_PATH,
|
||||
DICTATION_TYPE,
|
||||
DICT_TYPE,
|
||||
GWEN_TTS,
|
||||
INPUT,
|
||||
LANGUAGES,
|
||||
OUTPUT,
|
||||
PHRASES_TYPE,
|
||||
RESOURCES,
|
||||
)
|
||||
|
||||
# Static Clases
|
||||
|
||||
@@ -46,19 +57,28 @@ class TRANS:
|
||||
packages_to_install = []
|
||||
for in_package in packages:
|
||||
if in_package.from_code == from_code:
|
||||
if in_package.to_code == to_code:
|
||||
# Single package between 2 languages
|
||||
print(
|
||||
f"Installing package {in_package.from_code}"
|
||||
f"->{in_package.to_code}"
|
||||
)
|
||||
packages_to_install.append(in_package)
|
||||
break
|
||||
for out_package in packages:
|
||||
if out_package.to_code == to_code:
|
||||
if in_package.to_code == out_package.from_code:
|
||||
print(
|
||||
f"Check in_package {in_package.from_code}"
|
||||
f"{in_package.to_code}"
|
||||
f"Installing in_package {in_package.from_code}"
|
||||
f"->{in_package.to_code}"
|
||||
)
|
||||
print(
|
||||
f"Check out_package {out_package.from_code}"
|
||||
f"{out_package.to_code}"
|
||||
f"Installing out_package {out_package.from_code}"
|
||||
f"->{out_package.to_code}"
|
||||
)
|
||||
packages_to_install.append(in_package)
|
||||
packages_to_install.append(out_package)
|
||||
break
|
||||
for package in packages_to_install:
|
||||
print(f"instaling package {package}")
|
||||
argostranslate.package.install_from_path(package.download())
|
||||
@@ -143,7 +163,9 @@ class TTS:
|
||||
"language": "Chinese",
|
||||
"speaker": "Uncle_Fu",
|
||||
"instruct": "语速缓慢而审慎,每个音节的语调都拿捏得恰到好处,宛如教授在指导新生。",
|
||||
"instruct": "Speak in a slow pace and enuntiate every word, as a teacher to a learning student",
|
||||
}
|
||||
VOICES = ["Vivian", "Serena", "Uncle_Fu", "Dylan", "Eric"]
|
||||
|
||||
@staticmethod
|
||||
def create_tts():
|
||||
@@ -161,18 +183,73 @@ class TTS:
|
||||
GWEN_TTS,
|
||||
device_map=TTS.DEVICE,
|
||||
dtype=torch.bfloat16,
|
||||
attn_implementation="flash_attention_2",
|
||||
# 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)
|
||||
speaker = random.choice(TTS.VOICES)
|
||||
return TTS.MODEL.generate_custom_voice(
|
||||
text=text, **TTS.DEFAULTS, speaker=speaker
|
||||
)
|
||||
|
||||
|
||||
# Clases
|
||||
|
||||
|
||||
class ProcessFolder:
|
||||
"""Class that represents a folder to processs
|
||||
|
||||
diferent input files has direfent process_files depending on language
|
||||
"""
|
||||
|
||||
def __init__(self, input_folder: Path, language_id: str = None):
|
||||
self.input_folder = input_folder
|
||||
self._language_id = language_id
|
||||
# process file type
|
||||
self.out_folder = OUTPUT / input_folder
|
||||
self.out_folder.mkdir(parents=True, exist_ok=True)
|
||||
self.resources = RESOURCES / input_folder
|
||||
self.resources.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@property
|
||||
def output_name():
|
||||
"""Posible name for the output file, still missing the filetype"""
|
||||
if self.language_id is None:
|
||||
raise ValueError("Not a valid language selected")
|
||||
return self.out_folder / f"{self.input_file.stem}.{self.language_id}.temp"
|
||||
|
||||
@property
|
||||
def absolute_input_file(self):
|
||||
"""Absolute input file"""
|
||||
return INPUT / self.input_file
|
||||
|
||||
@property
|
||||
def input_files(self):
|
||||
input_files = []
|
||||
for file in self.absolute_input_file.glob(f"*.txt"):
|
||||
for file_type in (DICT_TYPE, PHRASES_TYPE, DICTATION_TYPE):
|
||||
if file_type in file.suffixes:
|
||||
input_files.append(ProcessFile(file, language_id=self.language_id))
|
||||
return input_files
|
||||
|
||||
@property
|
||||
def language_id(self):
|
||||
"""language for this trasnlation process"""
|
||||
return self._language_id
|
||||
|
||||
@language_id.setter
|
||||
def language_id(self, value):
|
||||
self._language_id = value
|
||||
|
||||
def __str__(self):
|
||||
return (
|
||||
f"Proccess {self.input_folder}"
|
||||
f"(out: {self.out_folder}, res: {self.resources})"
|
||||
)
|
||||
|
||||
|
||||
class ProcessFile:
|
||||
"""Class that represents a file to processs
|
||||
|
||||
@@ -189,6 +266,17 @@ class ProcessFile:
|
||||
self.resources = resources.parent / resources.stem
|
||||
self.resources.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@property
|
||||
def file_type(self):
|
||||
if DICTATION_TYPE in self.input_file.suffixes:
|
||||
return DICTATION_TYPE
|
||||
elif DICT_TYPE in self.input_file.suffixes:
|
||||
return DICT_TYPE
|
||||
elif PHRASES_TYPE in self.input_file.suffixes:
|
||||
return PHRASES_TYPE
|
||||
else:
|
||||
raise ValueError("File type not recognized")
|
||||
|
||||
@property
|
||||
def absolute_input_file(self):
|
||||
"""Absolute input file"""
|
||||
@@ -196,7 +284,7 @@ class ProcessFile:
|
||||
|
||||
@property
|
||||
def language_id(self):
|
||||
"""language for this trasnlation process"""
|
||||
"""language for this trasnlation proccess"""
|
||||
return self._language_id
|
||||
|
||||
@language_id.setter
|
||||
@@ -226,6 +314,9 @@ class ProcessFile:
|
||||
"""for a Dictionary file loads the avaliable proceced languages"""
|
||||
return [lan.suffixes[0][1:] for lan in self.resources.glob("dictionary.*.tsv")]
|
||||
|
||||
def __str__(self):
|
||||
return f"Proccess:{self.input_file}"
|
||||
|
||||
|
||||
class TranslationResult:
|
||||
"""Result of a translated process"""
|
||||
|
||||
Reference in New Issue
Block a user