kemetone-data / run.py
0best's picture
Upload run.py with huggingface_hub
64850d8 verified
Raw History Blame Contribute Delete
5.44 kB
import os, sys, subprocess, shutil
WORK = '/kaggle/working'
os.chdir(WORK)
# 1) system deps
subprocess.run(['apt-get', 'update', '-qq'], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.run(['apt-get', 'install', '-y', '-qq', 'espeak-ng'], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# 2) python deps
subprocess.run([sys.executable, '-m', 'pip', 'install', '-q',
'torch', 'torchaudio', 'soundfile', 'huggingface_hub', 'misaki',
'transformers', 'numpy', 'tqdm', 'loguru', 'attrs', 'phonemizer', 'num2words'], check=False)
from huggingface_hub import hf_hub_download, snapshot_download, upload_file
# 3) download code files from HF dataset
for fn in ['train_student.py', 'dump_keme.py', 'config_keme_student.json', '_env.py']:
p = os.path.join(WORK, fn)
if not os.path.exists(p):
hf_hub_download('0best/kemetone-data', fn, repo_type='dataset', local_dir=WORK)
sys.path.insert(0, WORK)
# 4) clone StyleTTS2
st = os.path.join(WORK, 'StyleTTS2')
if not os.path.exists(st):
subprocess.run(['git', 'clone', '--depth', '1',
'https://github.com/yl4579/StyleTTS2.git', st], check=True)
sys.path.insert(0, st)
sys.path.insert(0, WORK)
# 5) download KemeTone (model + g2p package)
model_dir = os.path.join(WORK, 'kemetone_model')
if not os.path.exists(model_dir):
snapshot_download('Rabe3/kemetone', local_dir=model_dir, ignore_patterns=['*.wav','*.png','*.jpg'])
if model_dir not in sys.path:
sys.path.insert(0, model_dir)
# patch g2p for misaki return arity
g2p_path = os.path.join(model_dir, 'kemetone', 'g2p.py')
if os.path.exists(g2p_path):
src = open(g2p_path).read()
src = src.replace(" raw, _ = self._g2p(text)\n",
" _r = self._g2p(text)\n raw = _r[0] if isinstance(_r, (tuple, list)) else _r\n")
open(g2p_path, 'w').write(src)
# 6) download patched kokoro from Nabra-Saudi-7M
patched = os.path.join(WORK, 'kokoro_patched')
if not os.path.exists(patched):
snapshot_download('oddadmix/Nabra-Saudi-7M', local_dir=os.path.join(WORK,'_nabra'),
allow_patterns=['kokoro_patched/**'])
src = os.path.join(WORK, '_nabra', 'kokoro_patched')
if os.path.exists(src):
shutil.move(src, patched)
sys.path.insert(0, patched)
# 7) create styletts2_gan shim
shim = os.path.join(WORK, 'styletts2_gan')
os.makedirs(shim, exist_ok=True)
open(os.path.join(shim, '__init__.py'), 'w').write("import sys\nsys.path.insert(0,'/kaggle/working/StyleTTS2')\n")
open(os.path.join(shim, 'discriminators.py'), 'w').write(
"import sys\nsys.path.insert(0,'/kaggle/working/StyleTTS2')\n"
"from Modules.discriminators import MultiPeriodDiscriminator, MultiResSpecDiscriminator, WavLMDiscriminator\n")
open(os.path.join(shim, 'losses_styletts2.py'), 'w').write(
"import sys\nsys.path.insert(0,'/kaggle/working/StyleTTS2')\n"
"from losses import GeneratorLoss, DiscriminatorLoss, MultiResolutionSTFTLoss, WavLMLoss\n")
if WORK not in sys.path:
sys.path.insert(0, WORK)
# 8) dump teacher (if not already done)
dump_dir = os.path.join(WORK, 'dump')
index_path = os.path.join(dump_dir, 'index.jsonl')
data_file = os.path.join(WORK, 'data.txt')
if not os.path.exists(data_file):
data_file = hf_hub_download('0best/kemetone-data', 'kemetone_TTS_CLEAN_DEDUPED_PART1.txt',
repo_type='dataset', local_dir=WORK)
if not os.path.exists(index_path):
os.environ['TEXT_FILE'] = data_file
os.environ['OUT_DIR'] = dump_dir
os.environ['MODEL_DIR'] = model_dir
subprocess.run([sys.executable, 'dump_keme.py'], check=True, cwd=WORK)
# 9) train student in chunks of 1000 steps, resuming each time
out_dir = os.path.join(WORK, 'runs', 'egypt7m')
os.makedirs(out_dir, exist_ok=True)
CHUNK = 1000
TOTAL_CHUNKS = 10 # 10 * 1000 = 10000 steps on part 1
for chunk in range(1, TOTAL_CHUNKS + 1):
target_steps = chunk * CHUNK
print(f"\n=== Chunk {chunk}/{TOTAL_CHUNKS}: training to step {target_steps} ===", flush=True)
cmd = [sys.executable, 'train_student.py',
'--index', index_path,
'--config', 'config_keme_student.json',
'--out', out_dir,
'--steps', str(target_steps),
'--batch', '8',
'--lr', '2e-4',
'--lr-decay',
'--mel-weight', '5',
'--sil-weight', '100',
'--slm-weight', '1',
'--slm-adv-weight', '0.2',
'--ckpt-every', '1000',
'--resume']
subprocess.run(cmd, check=True, cwd=WORK)
# Upload checkpoints to HF
for ckpt_file in [os.path.join(out_dir, 'last.pt'), os.path.join(out_dir, 'student.pth')]:
if os.path.exists(ckpt_file):
try:
token = os.environ.get('HF_TOKEN')
upload_file(
path_or_fileobj=ckpt_file,
path_in_repo=f"runs/egypt7m/chunk{chunk}/" + os.path.basename(ckpt_file),
repo_id="0best/kemetone-distill-ckpts",
repo_type="model",
token=token,
)
print(f"[*] uploaded {os.path.basename(ckpt_file)} to HF (chunk {chunk})", flush=True)
except Exception as e:
print(f"[*] upload failed for {ckpt_file}: {e}", flush=True)
print('TRAINING DONE - all 10 chunks completed (10,000 steps on part 1)')