sattycodes commited on
Commit
cb35c36
·
verified ·
1 Parent(s): 65eb895

Upload data_generator.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. data_generator.py +106 -0
data_generator.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import torchaudio
4
+ from tqdm import tqdm
5
+ from chatterbox.tts_turbo import ChatterboxTurboTTS
6
+
7
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
8
+ BASE_DIR = "/home/cloud/StyleTTS2-fine-tuning"
9
+ OUTPUT_DIR = os.path.join(BASE_DIR, "Data")
10
+
11
+ REFERENCE_AUDIO_PATH = os.path.join(OUTPUT_DIR, "reference_wavs/british_accent_audio.wav")
12
+ INPUT_TEXT_FILE = os.path.join(OUTPUT_DIR, "source_text_final.txt")
13
+ TRAIN_LIST_PATH = os.path.join(OUTPUT_DIR, "train_list_new.txt")
14
+ WAVS_DIR = os.path.join(OUTPUT_DIR, "wavs")
15
+ TARGET_SAMPLE_RATE = 24000
16
+
17
+ os.makedirs(WAVS_DIR, exist_ok=True)
18
+
19
+ model = ChatterboxTurboTTS.from_pretrained(device=DEVICE)
20
+
21
+ def get_sentences(text_path):
22
+ if not os.path.exists(text_path):
23
+ return []
24
+
25
+ with open(text_path, 'r', encoding='utf-8') as f:
26
+ lines = f.readlines()
27
+
28
+ valid_sentences = []
29
+ for line in lines:
30
+ cleaned = line.strip()
31
+ if cleaned:
32
+ valid_sentences.append(cleaned)
33
+
34
+ return valid_sentences
35
+
36
+ def get_completed_indices():
37
+ if not os.path.exists(TRAIN_LIST_PATH):
38
+ return set()
39
+
40
+ completed = set()
41
+ with open(TRAIN_LIST_PATH, "r", encoding="utf-8") as f:
42
+ for line in f:
43
+ parts = line.strip().split("|")
44
+ if parts and len(parts) >= 1:
45
+ filename = parts[0]
46
+ try:
47
+ number_part = filename.split("_")[1].split(".")[0]
48
+ completed.add(int(number_part))
49
+ except:
50
+ continue
51
+ return completed
52
+
53
+ def generate_dataset():
54
+ sentences = get_sentences(INPUT_TEXT_FILE)
55
+ completed_indices = get_completed_indices()
56
+
57
+ print(f"Total sentences: {len(sentences)}")
58
+ print(f"Already done: {len(completed_indices)}")
59
+
60
+ resampler = None
61
+ if model.sr != TARGET_SAMPLE_RATE:
62
+ resampler = torchaudio.transforms.Resample(orig_freq=model.sr, new_freq=TARGET_SAMPLE_RATE).to(DEVICE)
63
+
64
+ for i, sentence in enumerate(tqdm(sentences)):
65
+ if (i + 1) in completed_indices:
66
+ continue
67
+
68
+ filename = f"file_{i+1:04d}.wav"
69
+ filepath = os.path.join(WAVS_DIR, filename)
70
+ print(f"Generating {filename}...")
71
+ try:
72
+ with torch.inference_mode():
73
+ wav_tensor = model.generate(
74
+ sentence,
75
+ audio_prompt_path=REFERENCE_AUDIO_PATH
76
+ )
77
+
78
+ if wav_tensor.dim() == 1:
79
+ wav_tensor = wav_tensor.unsqueeze(0)
80
+
81
+ if wav_tensor.shape[0] > 1:
82
+ wav_tensor = wav_tensor.mean(dim=0, keepdim=True)
83
+
84
+ wav_tensor = wav_tensor.cpu()
85
+
86
+ if resampler:
87
+ wav_tensor = resampler(wav_tensor)
88
+
89
+ torchaudio.save(filepath, wav_tensor, TARGET_SAMPLE_RATE)
90
+
91
+ with open(TRAIN_LIST_PATH, "a", encoding="utf-8") as f:
92
+ f.write(f"{filename}|{sentence}|0\n")
93
+
94
+ del wav_tensor
95
+ torch.cuda.empty_cache()
96
+
97
+ if (i + 1) % 50 == 0:
98
+ torch.cuda.synchronize()
99
+
100
+ except Exception as e:
101
+ print(f"Error at sample {i+1}: {e}")
102
+ continue
103
+
104
+
105
+ if __name__ == "__main__":
106
+ generate_dataset()