File size: 3,070 Bytes
e20f826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# quest.py — for VoiceProfiler/QuEsT
# Keeps `text_quz` (Quechua) and `text_es` (Spanish), plus a unified `text` column for the viewer.

import os
import datasets
from datasets import load_dataset, Audio

class QuEsT(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="default", version=VERSION, description="Default configuration"),
    ]
    DEFAULT_CONFIG_NAME = "default"

    def _info(self):
        return datasets.DatasetInfo(
            description="Quechua–Spanish speech dataset (QuEsT) with aligned transcripts.",
            features=datasets.Features({
                "id":               datasets.Value("string"),
                "language":         datasets.Value("string"),
                "text":             datasets.Value("string"),
                "has_transcription": datasets.Value("bool"),
                "audio":            Audio(sampling_rate=None, mono=True),
            }),
        )

    def _split_generators(self, dl_manager):
        base = os.path.dirname(os.path.abspath(__file__))
        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN,
                gen_kwargs={"parquet_path": os.path.join(base, "data", "train.parquet")}),
            datasets.SplitGenerator(name=datasets.Split.TEST,
                gen_kwargs={"parquet_path": os.path.join(base, "data", "test.parquet")}),
        ]

    def _generate_examples(self, parquet_path: str):
        ds = load_dataset("parquet", data_files=parquet_path, split="train")

        cols = set(ds.column_names)
        length = len(ds)

        # Normalize possible alternate column names
        if "audio" in cols and "path" not in cols:
            ds = ds.rename_column("audio", "path")
        if "label" in cols and "text" not in cols:
            ds = ds.rename_column("label", "text")
        if "lang" in cols and "language" not in cols:
            ds = ds.rename_column("lang", "language")
        if "has_transcription" not in ds.column_names:
            texts = ds["text"] if "text" in ds.column_names else [None] * length
            default = [False if t in (None, "") else True for t in texts]
            ds = ds.add_column("has_transcription", default)

        # Ensure required columns exist
        for c in ["id", "language", "text", "path", "has_transcription"]:
            if c not in ds.column_names:
                fill = [None] * length
                if c == "has_transcription":
                    fill = [False] * length
                ds = ds.add_column(c, fill)

        # Cast to Audio() for playback in viewer
        ds = ds.cast_column("path", Audio(sampling_rate=None, mono=True))
        ds = ds.rename_column("path", "audio")

        # Keep only desired columns (and in order)
        keep = ["id", "language", "text", "has_transcription", "audio"]
        drop = [c for c in ds.column_names if c not in keep]
        if drop:
            ds = ds.remove_columns(drop)

        for i, ex in enumerate(ds):
            yield i, ex