add script
Browse files
psst.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2021 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
3 |
+
# Copyright 2022 Jim O'Regan
|
4 |
+
#
|
5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6 |
+
# you may not use this file except in compliance with the License.
|
7 |
+
# You may obtain a copy of the License at
|
8 |
+
#
|
9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
#
|
11 |
+
# Unless required by applicable law or agreed to in writing, software
|
12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 |
+
# See the License for the specific language governing permissions and
|
15 |
+
# limitations under the License.
|
16 |
+
# Lint as: python3
|
17 |
+
|
18 |
+
from email.mime import audio
|
19 |
+
from pathlib import Path
|
20 |
+
import os
|
21 |
+
import csv
|
22 |
+
|
23 |
+
import datasets
|
24 |
+
from datasets.tasks import AutomaticSpeechRecognition
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
_DESCRIPTION = """
|
29 |
+
The PSST Challenge focuses on a technically-challenging and clinically
|
30 |
+
important task—high-accuracy automatic phoneme recognition of disordered
|
31 |
+
speech, in a diagnostic context—which has applications in many different
|
32 |
+
areas relating to speech and language disorders.
|
33 |
+
"""
|
34 |
+
|
35 |
+
class PSSTDataset(datasets.GeneratorBasedBuilder):
|
36 |
+
"""PSST Dataset"""
|
37 |
+
|
38 |
+
VERSION = datasets.Version("1.1.0")
|
39 |
+
|
40 |
+
BUILDER_CONFIGS = [
|
41 |
+
datasets.BuilderConfig(name="psst"),
|
42 |
+
]
|
43 |
+
|
44 |
+
# utterance_id session test prompt transcript correctness aq_index duration_frames filename
|
45 |
+
def _info(self):
|
46 |
+
features = datasets.Features(
|
47 |
+
{
|
48 |
+
"utterance_id": datasets.Value("string"),
|
49 |
+
"session": datasets.Value("string"),
|
50 |
+
"test": datasets.Value("string"),
|
51 |
+
"prompt": datasets.Value("string"),
|
52 |
+
"transcript": datasets.Value("string"),
|
53 |
+
"phonemes": datasets.Sequence(datasets.Value("string")),
|
54 |
+
"correctness": datasets.Value("bool"),
|
55 |
+
"aq_index": datasets.Value("float"),
|
56 |
+
"duration_frames": datasets.Value("uint64"),
|
57 |
+
"audio": datasets.Audio(sampling_rate=16_000)
|
58 |
+
}
|
59 |
+
)
|
60 |
+
|
61 |
+
return datasets.DatasetInfo(
|
62 |
+
description=_DESCRIPTION,
|
63 |
+
features=features,
|
64 |
+
supervised_keys=None,
|
65 |
+
homepage="https://psst.study/",
|
66 |
+
task_templates=[
|
67 |
+
AutomaticSpeechRecognition(audio_file_path_column="filename", transcription_column="transcript")
|
68 |
+
],
|
69 |
+
)
|
70 |
+
|
71 |
+
|
72 |
+
def _split_generators(self, dl_manager):
|
73 |
+
if hasattr(dl_manager, 'manual_dir') and dl_manager.manual_dir is not None:
|
74 |
+
data_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir))
|
75 |
+
else:
|
76 |
+
raise Exception("No path to data specified")
|
77 |
+
|
78 |
+
return [
|
79 |
+
datasets.SplitGenerator(
|
80 |
+
name=datasets.Split.TRAIN,
|
81 |
+
gen_kwargs={
|
82 |
+
"split": "train",
|
83 |
+
"data_dir": data_dir
|
84 |
+
},
|
85 |
+
),
|
86 |
+
datasets.SplitGenerator(
|
87 |
+
name=datasets.Split.VALIDATION,
|
88 |
+
gen_kwargs={
|
89 |
+
"split": "valid",
|
90 |
+
"data_dir": data_dir
|
91 |
+
},
|
92 |
+
),
|
93 |
+
]
|
94 |
+
|
95 |
+
# utterance_id session test prompt transcript correctness aq_index duration_frames filename
|
96 |
+
def _generate_examples(
|
97 |
+
self, split, data_dir
|
98 |
+
):
|
99 |
+
"""Yields examples as (key, example) tuples. """
|
100 |
+
data_path = Path(data_dir)
|
101 |
+
split_path = data_path / split
|
102 |
+
if not split_path.exists():
|
103 |
+
raise Exception(f"{split} directory not found ({split_path})")
|
104 |
+
utterances = split_path / "utterances.tsv"
|
105 |
+
if not utterances.exists():
|
106 |
+
raise Exception(f"utterances.tsv not found in {split} directory ({split_path})")
|
107 |
+
with open(utterances) as tsvfile:
|
108 |
+
data = csv.DictReader(tsvfile, delimiter='\t')
|
109 |
+
for row in data:
|
110 |
+
audiopath = split_path / row["filename"]
|
111 |
+
if audiopath.exists():
|
112 |
+
with open(audiopath, "rb") as audiofile:
|
113 |
+
yield row["utterance_id"], {
|
114 |
+
"utterance_id": row["utterance_id"],
|
115 |
+
"session": row["session"],
|
116 |
+
"test": row["test"],
|
117 |
+
"prompt": row["prompt"],
|
118 |
+
"transcript": row["transcript"],
|
119 |
+
"phonemes": row["transcript"].strip().split(" "),
|
120 |
+
"correctness": (row["correctness"] == "True"),
|
121 |
+
"aq_index": float(row["aq_index"]),
|
122 |
+
"duration_frames": int(row["duration_frames"]),
|
123 |
+
"audio": {
|
124 |
+
"path": str(audiopath),
|
125 |
+
"bytes": audiofile.read()
|
126 |
+
}
|
127 |
+
}
|