0best commited on
Commit
600d6cd
·
verified ·
1 Parent(s): 823bf5f

Upload dump_keme.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. dump_keme.py +62 -0
dump_keme.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, sys, json, torch, soundfile as sf
2
+ sys.path.insert(0, os.environ.get('MODEL_DIR', '.'))
3
+ sys.path.insert(0, 'kokoro_patched')
4
+ sys.path.insert(0, '.')
5
+ from kemetone import EgyptianG2P
6
+
7
+ MODEL_DIR = os.environ.get('MODEL_DIR', '/tmp/kilo/kemetone_full')
8
+ OUT_DIR = os.environ.get('OUT_DIR', 'dump')
9
+ TEXT_FILE = os.environ.get('TEXT_FILE', 'data.txt')
10
+ os.makedirs(OUT_DIR, exist_ok=True)
11
+ os.makedirs(os.path.join(OUT_DIR, 'wavs'), exist_ok=True)
12
+
13
+ from kokoro import KModel
14
+ m = KModel(config=os.path.join(MODEL_DIR, 'config.json'),
15
+ model=os.path.join(MODEL_DIR, 'kemetone.pth'),
16
+ disable_complex=True).eval()
17
+ voice = torch.load(os.path.join(MODEL_DIR, 'voices', 'kemetone.pt'),
18
+ map_location='cpu', weights_only=True)
19
+ g = EgyptianG2P()
20
+ vocab = m.vocab
21
+
22
+ rows = []
23
+ with open(TEXT_FILE, encoding='utf-8') as f:
24
+ lines = [l.strip() for l in f if l.strip()]
25
+
26
+ for i, text in enumerate(lines):
27
+ try:
28
+ ph = g(text)
29
+ ids = [idx for idx in (vocab.get(c) for c in ph) if idx is not None]
30
+ if not ids:
31
+ continue
32
+ if isinstance(voice, torch.Tensor) and voice.dim() == 3:
33
+ ref_s = voice[min(len(ph)-1, voice.shape[0]-1)]
34
+ else:
35
+ ref_s = voice
36
+ out = m(ph, ref_s, speed=1.0, return_output=True)
37
+ pred_dur = out.pred_dur.tolist()
38
+ if len(pred_dur) == len(ids) + 2:
39
+ bos = pred_dur[0]
40
+ dur = pred_dur[1:-1]
41
+ elif len(pred_dur) == len(ids):
42
+ bos = 0
43
+ dur = pred_dur
44
+ else:
45
+ dur = pred_dur[:len(ids)]
46
+ bos = 0
47
+ if len(dur) != len(ids):
48
+ print('SKIP len mismatch', i, len(dur), len(ids), ph)
49
+ continue
50
+ wav_path = os.path.join(OUT_DIR, 'wavs', f'{i:06d}.wav')
51
+ sf.write(wav_path, out.audio.numpy(), 24000)
52
+ rows.append({"wav": os.path.abspath(wav_path), "ids": ids, "dur": dur, "start": bos})
53
+ if (i+1) % 100 == 0:
54
+ print(f' {i+1}/{len(lines)} done', flush=True)
55
+ except Exception as e:
56
+ print('ERR', i, repr(e), flush=True)
57
+
58
+ index_path = os.path.join(OUT_DIR, 'index.jsonl')
59
+ with open(index_path, 'w', encoding='utf-8') as f:
60
+ for r in rows:
61
+ f.write(json.dumps(r, ensure_ascii=False) + '\n')
62
+ print(f'Wrote {len(rows)} rows to {index_path}')