root commited on
Commit
971080c
·
1 Parent(s): c8eafd7
Files changed (1) hide show
  1. expressivespeech.py +107 -0
expressivespeech.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # expressivespeech.py
2
+ import json
3
+ import os
4
+ import datasets
5
+
6
+ _REPO_ID = "FreedomIntelligence/ExpressiveSpeech"
7
+ _BRANCH = "main"
8
+
9
+ _DESCRIPTION = "Expressive speech dataset (loader for data.jsonl with audio column)."
10
+ _HOMEPAGE = f"https://huggingface.co/datasets/{_REPO_ID}"
11
+ _CITATION = ""
12
+
13
+ class ExpressiveSpeech(datasets.GeneratorBasedBuilder):
14
+ VERSION = datasets.Version("1.0.0")
15
+
16
+ BUILDER_CONFIGS = [
17
+ datasets.BuilderConfig(
18
+ name="default",
19
+ version=VERSION,
20
+ description="Default config that loads turns from data.jsonl and exposes audio as datasets.Audio",
21
+ ),
22
+ ]
23
+
24
+ def _info(self):
25
+ return datasets.DatasetInfo(
26
+ description=_DESCRIPTION,
27
+ features=datasets.Features(
28
+ {
29
+ "from": datasets.Value("string"),
30
+ "value": datasets.Value("string"),
31
+ "emotion": datasets.Value("string"),
32
+ "length": datasets.Value("float"),
33
+ "score_arousal": datasets.Value("float"),
34
+ "score_prosody": datasets.Value("float"),
35
+ "score_nature": datasets.Value("float"),
36
+ "score_expressive": datasets.Value("float"),
37
+ "audio": datasets.Audio(sampling_rate=None),
38
+ "dialog_id": datasets.Value("int32"),
39
+ "turn_no": datasets.Value("int32"),
40
+ }
41
+ ),
42
+ homepage=_HOMEPAGE,
43
+ citation=_CITATION,
44
+ )
45
+
46
+ def _split_generators(self, dl_manager: datasets.DownloadManager):
47
+
48
+ jsonl_url = f"hf://datasets/{_REPO_ID}/resolve/{_BRANCH}/data.jsonl"
49
+ data_jsonl_path = dl_manager.download(jsonl_url)
50
+
51
+ return [
52
+ datasets.SplitGenerator(
53
+ name=datasets.Split.TRAIN,
54
+ gen_kwargs={
55
+ "data_jsonl_path": data_jsonl_path,
56
+ },
57
+ )
58
+ ]
59
+
60
+ def _generate_examples(self, data_jsonl_path: str):
61
+
62
+ def to_hf_url(rel_path: str) -> str:
63
+
64
+ rel_path = rel_path.lstrip("./")
65
+ return f"hf://datasets/{_REPO_ID}/resolve/{_BRANCH}/{rel_path}"
66
+
67
+ with open(data_jsonl_path, "r", encoding="utf-8") as f:
68
+ dialog_idx = 0
69
+ row_idx = 0
70
+ for line in f:
71
+ line = line.strip()
72
+ if not line:
73
+ continue
74
+ obj = json.loads(line)
75
+ conversations = obj.get("conversations", [])
76
+ for turn in conversations:
77
+
78
+ from_role = turn.get("from", "")
79
+ value = turn.get("value", "")
80
+ emotion = turn.get("emotion", "")
81
+ length = float(turn.get("length", 0.0) or 0.0)
82
+ score_arousal = float(turn.get("score_arousal", 0.0) or 0.0)
83
+ score_prosody = float(turn.get("score_prosody", 0.0) or 0.0)
84
+ score_nature = float(turn.get("score_nature", 0.0) or 0.0)
85
+ score_expressive = float(turn.get("score_expressive", 0.0) or 0.0)
86
+ audio_rel = turn.get("audio-path", None)
87
+
88
+
89
+ audio_field = None
90
+ if audio_rel:
91
+ audio_field = to_hf_url(audio_rel)
92
+
93
+ yield row_idx, {
94
+ "from": from_role,
95
+ "value": value,
96
+ "emotion": emotion,
97
+ "length": length,
98
+ "score_arousal": score_arousal,
99
+ "score_prosody": score_prosody,
100
+ "score_nature": score_nature,
101
+ "score_expressive": score_expressive,
102
+ "audio": audio_field,
103
+ "dialog_id": dialog_idx,
104
+ "turn_no": int(turn.get("No", 0) or 0),
105
+ }
106
+ row_idx += 1
107
+ dialog_idx += 1