File size: 7,943 Bytes
d42bc19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9229bd9
 
 
 
 
 
 
 
 
 
 
d42bc19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd75f48
 
 
 
 
 
 
d42bc19
dd75f48
 
 
 
 
 
 
 
 
 
 
 
d42bc19
 
 
dd75f48
d42bc19
 
 
dd75f48
d42bc19
 
 
 
 
 
 
 
 
dd75f48
 
 
 
 
d42bc19
 
 
 
 
 
 
 
 
 
 
 
 
 
9229bd9
d42bc19
 
9229bd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d42bc19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
"""
Build one canonical LJSpeech dataset from distributed per-video outputs.

Distributed workers write:
  videos/<video_id>/dataset/metadata.csv
  videos/<video_id>/dataset/wavs/*.wav
  done/<video_id>.json

This script downloads only videos that have a done marker, renumbers wavs
deterministically, and writes root metadata/train/val/stats files locally.
Optionally, it can upload the finalized dataset back to the same HF dataset repo.
"""

import argparse
import json
import os
import random
import shutil
from pathlib import Path

import numpy as np


SAMPLE_RATE = 22050
OUTPUT_REPO = "outlawmold/sinhala-tts-dataset"
os.environ.setdefault("HF_ENDPOINT", "https://hf-mirror.com")


def list_paths(api, repo: str, prefix: str):
    try:
        return [
            item.rfilename
            for item in api.list_repo_tree(
                repo,
                repo_type="dataset",
                path_in_repo=prefix,
                recursive=True,
            )
            if hasattr(item, "rfilename")
        ]
    except Exception:
        return []


def file_exists(api, repo: str, path_in_repo: str) -> bool:
    try:
        return api.file_exists(
            repo_id=repo,
            repo_type="dataset",
            filename=path_in_repo,
        )
    except Exception:
        return False


def done_video_ids(api, repo: str):
    return sorted(Path(path).stem for path in list_paths(api, repo, "done") if path.endswith(".json"))


def read_text_file(api, repo: str, path_in_repo: str) -> str:
    path = api.hf_hub_download(
        repo_id=repo,
        repo_type="dataset",
        filename=path_in_repo,
        force_download=True,
    )
    return Path(path).read_text(encoding="utf-8")


def download_file(api, repo: str, path_in_repo: str, local_path: Path):
    downloaded = api.hf_hub_download(
        repo_id=repo,
        repo_type="dataset",
        filename=path_in_repo,
        force_download=True,
    )
    local_path.parent.mkdir(parents=True, exist_ok=True)
    shutil.copy2(downloaded, local_path)


def parse_metadata(text: str):
    rows = []
    for line in text.splitlines():
        line = line.strip()
        if not line:
            continue
        parts = line.split("|", 2)
        if len(parts) != 3:
            continue
        rows.append(tuple(parts))
    return rows


def build_dataset(repo: str, output_dir: Path, val_split: float):
    from huggingface_hub import HfApi

    api = HfApi()
    output_dir.mkdir(parents=True, exist_ok=True)
    wavs_dir = output_dir / "wavs"
    wavs_dir.mkdir(exist_ok=True)

    videos = done_video_ids(api, repo)
    metadata = []
    manifest = []
    durations = []
    next_index = 0

    for video_id in videos:
        metadata_path = f"videos/{video_id}/dataset/metadata.csv"
        try:
            rows = parse_metadata(read_text_file(api, repo, metadata_path))
        except Exception as e:
            print(f"[skip] {video_id}: no metadata ({e})")
            continue

        for old_name, text, normalized in rows:
            old_wav = f"videos/{video_id}/dataset/wavs/{old_name}.wav"
            new_name = f"si_{next_index:06d}"
            new_wav = wavs_dir / f"{new_name}.wav"
            try:
                download_file(api, repo, old_wav, new_wav)
            except Exception as e:
                print(f"[skip] {video_id}/{old_name}: missing wav ({e})")
                continue

            metadata.append(f"{new_name}|{text}|{normalized}")
            manifest.append({
                "name": new_name,
                "source_video": video_id,
                "source_name": old_name,
            })
            try:
                import soundfile as sf
                info = sf.info(str(new_wav))
                durations.append(info.frames / info.samplerate)
            except Exception:
                pass
            next_index += 1

    # Video-level train/val split to prevent data leakage
    video_to_lines = {}
    for line, m in zip(metadata, manifest):
        vid = m["source_video"]
        video_to_lines.setdefault(vid, []).append(line)

    video_ids = sorted(video_to_lines.keys())
    random.seed(42)
    random.shuffle(video_ids)
    n_val_videos = max(1, int(len(video_ids) * val_split)) if video_ids else 0
    val_videos = set(video_ids[:n_val_videos])
    train_videos = set(video_ids[n_val_videos:])

    train_lines = []
    val_lines = []
    for vid in video_ids:
        if vid in val_videos:
            val_lines.extend(video_to_lines[vid])
        else:
            train_lines.extend(video_to_lines[vid])

    (output_dir / "metadata.csv").write_text("\n".join(metadata) + ("\n" if metadata else ""), encoding="utf-8")
    (output_dir / "metadata_train.csv").write_text(
        "\n".join(train_lines) + ("\n" if train_lines else ""),
        encoding="utf-8",
    )
    (output_dir / "metadata_val.csv").write_text(
        "\n".join(val_lines) + ("\n" if val_lines else ""),
        encoding="utf-8",
    )
    (output_dir / "distributed_manifest.json").write_text(
        json.dumps(manifest, indent=2, ensure_ascii=False),
        encoding="utf-8",
    )

    stats = {
        "total_utterances": len(metadata),
        "train_utterances": len(train_lines),
        "val_utterances": len(val_lines),
        "total_videos": len(video_ids),
        "train_videos": len(train_videos),
        "val_videos": len(val_videos),
        "source_videos_done": len(videos),
        "source_videos_with_metadata": len({m["source_video"] for m in manifest}),
        "total_hours": round(sum(durations) / 3600, 2) if durations else 0.0,
        "mean_duration_sec": round(float(np.mean(durations)), 2) if durations else 0.0,
        "median_duration_sec": round(float(np.median(durations)), 2) if durations else 0.0,
        "min_duration_sec": round(min(durations), 2) if durations else 0.0,
        "max_duration_sec": round(max(durations), 2) if durations else 0.0,
        "sample_rate": SAMPLE_RATE,
    }
    (output_dir / "dataset_stats.json").write_text(json.dumps(stats, indent=2), encoding="utf-8")
    return stats


def upload_dataset(repo: str, output_dir: Path):
    from huggingface_hub import CommitOperationDelete, HfApi

    api = HfApi()
    root_files = [
        "metadata.csv",
        "metadata_train.csv",
        "metadata_val.csv",
        "dataset_stats.json",
        "distributed_manifest.json",
    ]
    delete_paths = set(path for path in list_paths(api, repo, "wavs") if path.endswith(".wav"))
    delete_paths.update(path for path in root_files if file_exists(api, repo, path))
    if delete_paths:
        api.create_commit(
            repo_id=repo,
            repo_type="dataset",
            operations=[CommitOperationDelete(path_in_repo=path) for path in sorted(delete_paths)],
            commit_message="Clear finalized dataset before replacement",
        )

    api.upload_folder(
        folder_path=str(output_dir),
        repo_id=repo,
        repo_type="dataset",
        commit_message="Finalize distributed dataset",
    )


def parse_args():
    parser = argparse.ArgumentParser(description="Finalize distributed Sinhala TTS outputs")
    parser.add_argument("--repo", default=OUTPUT_REPO)
    parser.add_argument("--output-dir", default="pipeline_output/distributed_final_dataset")
    parser.add_argument("--val-split", type=float, default=0.05)
    parser.add_argument("--upload", action="store_true")
    return parser.parse_args()


def main():
    args = parse_args()
    output_dir = Path(args.output_dir)
    stats = build_dataset(args.repo, output_dir, args.val_split)
    print(f"Wrote {stats['total_utterances']} utterances to {output_dir}")
    print(f"Hours: {stats['total_hours']}")
    if args.upload:
        upload_dataset(args.repo, output_dir)
        print(f"Uploaded finalized dataset to https://huggingface.co/datasets/{args.repo}")


if __name__ == "__main__":
    main()