Upload my_dataset.py with huggingface_hub
Browse files- my_dataset.py +114 -0
my_dataset.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import csv
|
3 |
+
import datasets
|
4 |
+
from tqdm import tqdm # لإظهار تقدم القراءة في ملفات metadata
|
5 |
+
|
6 |
+
_DESCRIPTION = "A speech dataset designed for automatic speech recognition (ASR), structured like Mozilla Common Voice."
|
7 |
+
_CITATION = "No citation available yet."
|
8 |
+
|
9 |
+
class MyDatasetConfig(datasets.BuilderConfig):
|
10 |
+
def __init__(self, **kwargs):
|
11 |
+
super(MyDatasetConfig, self).__init__(**kwargs)
|
12 |
+
|
13 |
+
class MyDataset(datasets.GeneratorBasedBuilder):
|
14 |
+
DEFAULT_WRITER_BATCH_SIZE = 1000 # مثل Common Voice
|
15 |
+
|
16 |
+
BUILDER_CONFIGS = [
|
17 |
+
MyDatasetConfig(
|
18 |
+
name="default",
|
19 |
+
version=datasets.Version("1.0.0"),
|
20 |
+
description=_DESCRIPTION,
|
21 |
+
),
|
22 |
+
]
|
23 |
+
|
24 |
+
def _info(self):
|
25 |
+
features = datasets.Features({
|
26 |
+
"client_id": datasets.Value("string"),
|
27 |
+
"path": datasets.Value("string"),
|
28 |
+
"audio": datasets.features.Audio(sampling_rate=16000),
|
29 |
+
"text": datasets.Value("string"),
|
30 |
+
"up_votes": datasets.Value("int64"),
|
31 |
+
"down_votes": datasets.Value("int64"),
|
32 |
+
"age": datasets.Value("string"),
|
33 |
+
"gender": datasets.Value("string"),
|
34 |
+
"accent": datasets.Value("string"),
|
35 |
+
"locale": datasets.Value("string"),
|
36 |
+
"segment": datasets.Value("string"),
|
37 |
+
"variant": datasets.Value("string"),
|
38 |
+
})
|
39 |
+
|
40 |
+
return datasets.DatasetInfo(
|
41 |
+
description=_DESCRIPTION,
|
42 |
+
features=features,
|
43 |
+
supervised_keys=None,
|
44 |
+
citation=_CITATION,
|
45 |
+
version=self.config.version,
|
46 |
+
)
|
47 |
+
|
48 |
+
def _split_generators(self, dl_manager):
|
49 |
+
data_dir = self.config.data_dir
|
50 |
+
|
51 |
+
splits = ["train", "validation", "test"]
|
52 |
+
split_generators = []
|
53 |
+
|
54 |
+
for split in splits:
|
55 |
+
audio_tar = os.path.join(data_dir, f"{split}_audio")
|
56 |
+
metadata_csv = os.path.join(data_dir, f"{split}_metadata.csv")
|
57 |
+
|
58 |
+
if os.path.exists(audio_tar) and os.path.exists(metadata_csv):
|
59 |
+
split_generators.append(
|
60 |
+
datasets.SplitGenerator(
|
61 |
+
name=getattr(datasets.Split, split.upper()),
|
62 |
+
gen_kwargs={
|
63 |
+
"archives": dl_manager.iter_archive(audio_tar),
|
64 |
+
"metadata_path": metadata_csv,
|
65 |
+
},
|
66 |
+
)
|
67 |
+
)
|
68 |
+
|
69 |
+
return split_generators
|
70 |
+
|
71 |
+
def _generate_examples(self, archives, metadata_path):
|
72 |
+
# قراءة الميتاداتا في dict
|
73 |
+
metadata = {}
|
74 |
+
data_fields = list(self._info().features.keys())
|
75 |
+
|
76 |
+
with open(metadata_path, encoding="utf-8-sig") as f:
|
77 |
+
reader = csv.DictReader(f, delimiter=",", quoting=csv.QUOTE_NONE)
|
78 |
+
reader.fieldnames = [name.strip().replace('"', '') for name in reader.fieldnames]
|
79 |
+
|
80 |
+
for row in tqdm(reader, desc="Loading metadata..."):
|
81 |
+
row = {k.replace('"', ''): v.replace('"', '') for k, v in row.items()}
|
82 |
+
if not row["file_name"].endswith(".wav"):
|
83 |
+
row["file_name"] += ".wav"
|
84 |
+
|
85 |
+
# تحويل accents إلى accent (كما في Common Voice 8.0)
|
86 |
+
if "accents" in row:
|
87 |
+
row["accent"] = row["accents"]
|
88 |
+
del row["accents"]
|
89 |
+
|
90 |
+
# ملء الحقول الناقصة بقيم فارغة أو افتراضية
|
91 |
+
for field in data_fields:
|
92 |
+
if field not in row:
|
93 |
+
# للأعداد (up_votes, down_votes) نضع صفر، الباقي فراغ
|
94 |
+
if field in ["up_votes", "down_votes"]:
|
95 |
+
row[field] = 0
|
96 |
+
else:
|
97 |
+
row[field] = ""
|
98 |
+
|
99 |
+
metadata[row["file_name"]] = row
|
100 |
+
|
101 |
+
|
102 |
+
# قراءة ملفات الصوت من الأرشيف وتوليد الأمثلة
|
103 |
+
for i, audio_archive in enumerate(archives):
|
104 |
+
for path_in_tar, file_obj in audio_archive:
|
105 |
+
_, filename = os.path.split(path_in_tar)
|
106 |
+
|
107 |
+
if filename in metadata:
|
108 |
+
example = dict(metadata[filename])
|
109 |
+
|
110 |
+
# ضبط مسار الصوت (لتحميله محلياً أو عبر streaming)
|
111 |
+
example["audio"] = {"path": path_in_tar, "bytes": file_obj.read()}
|
112 |
+
example["path"] = path_in_tar
|
113 |
+
|
114 |
+
yield path_in_tar, example
|