Flutra commited on
Commit
3a2b404
·
verified ·
1 Parent(s): 6fb01d7

Upload common_voice_sq_local.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. common_voice_sq_local.py +67 -0
common_voice_sq_local.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+ import tarfile
4
+ from datasets import Dataset, DatasetDict, Audio, Features, Value, Sequence
5
+
6
+ def load_common_voice_dataset(data_dir):
7
+ # Extract tar if not already extracted
8
+ tar_path = os.path.join(data_dir, "common_voice_sq_20_local.tar")
9
+ extract_dir = os.path.join(data_dir, "extracted")
10
+ if not os.path.exists(extract_dir):
11
+ os.makedirs(extract_dir)
12
+ with tarfile.open(tar_path, 'r') as tar_ref:
13
+ tar_ref.extractall(extract_dir)
14
+
15
+ splits = {}
16
+ for split in ["train", "dev", "validated"]:
17
+ tsv_path = os.path.join(extract_dir, f"{split}.tsv")
18
+ if not os.path.exists(tsv_path):
19
+ continue
20
+
21
+ df = pd.read_csv(tsv_path, sep=" ")
22
+ # Use 'path' column from TSV, prepending 'clips/' to construct full audio paths
23
+ audio_paths = [os.path.join(extract_dir, "clips", p) for p in df["path"]]
24
+
25
+ data = {
26
+ "audio": audio_paths,
27
+ "sentence": df["sentence"].tolist(),
28
+ "client_id": df["client_id"].tolist(),
29
+ "sentence_id": df["sentence_id"].tolist(),
30
+ "sentence_domain": df["sentence_domain"].tolist(),
31
+ "up_votes": df["up_votes"].tolist(),
32
+ "down_votes": df["down_votes"].tolist(),
33
+ "age": df["age"].tolist(),
34
+ "gender": df["gender"].tolist(),
35
+ "accents": df["accents"].tolist(),
36
+ "variant": df["variant"].tolist(),
37
+ "locale": df["locale"].tolist(),
38
+ "segment": df["segment"].tolist()
39
+ }
40
+
41
+ features = Features({
42
+ "audio": Audio(sampling_rate=16000),
43
+ "sentence": Value("string"),
44
+ "client_id": Value("string"),
45
+ "sentence_id": Value("string"),
46
+ "sentence_domain": Value("string"),
47
+ "up_votes": Value("int32"),
48
+ "down_votes": Value("int32"),
49
+ "age": Value("string"),
50
+ "gender": Value("string"),
51
+ "accents": Value("string"),
52
+ "variant": Value("string"),
53
+ "locale": Value("string"),
54
+ "segment": Value("string")
55
+ })
56
+
57
+ splits[split] = Dataset.from_dict(data, features=features)
58
+
59
+ return DatasetDict(splits)
60
+
61
+ def get_dataset(data_dir):
62
+ return load_common_voice_dataset(data_dir)
63
+
64
+ if __name__ == "__main__":
65
+ import sys
66
+ dataset = get_dataset(sys.argv[1])
67
+ print(dataset)