litagin commited on
Commit
344cf8a
1 Parent(s): 5975982
Files changed (4) hide show
  1. .gitignore +2 -0
  2. README.md +1 -1
  3. app.py +260 -0
  4. requirements.txt +5 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ __pycache__/
2
+ .venv/
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Make Anime Ser Dataset
3
  emoji: ⚡
4
  colorFrom: red
5
  colorTo: gray
 
1
  ---
2
+ title: Make Anime Emotion Dataset
3
  emoji: ⚡
4
  colorFrom: red
5
  colorTo: gray
app.py ADDED
@@ -0,0 +1,260 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import warnings
4
+
5
+ import gradio as gr
6
+ import librosa
7
+ import numpy as np
8
+ from datasets import IterableDatasetDict, load_dataset
9
+ from gradio_client import Client
10
+ from loguru import logger
11
+
12
+ warnings.filterwarnings("ignore")
13
+
14
+ NUM_TAR_FILES = 115
15
+ HF_PATH_TO_DATASET = "litagin/Galgame_Speech_SER_16kHz"
16
+
17
+ hf_token = os.getenv("HF_TOKEN")
18
+ client = Client("litagin/ser_record", hf_token=hf_token)
19
+
20
+ id2label = {
21
+ 0: "Angry",
22
+ 1: "Disgusted",
23
+ 2: "Embarrassed",
24
+ 3: "Fearful",
25
+ 4: "Happy",
26
+ 5: "Sad",
27
+ 6: "Surprised",
28
+ 7: "Neutral",
29
+ 8: "Sexual1",
30
+ 9: "Sexual2",
31
+ }
32
+
33
+ id2rich_label = {
34
+ 0: "😠 怒り (0)",
35
+ 1: "😒 嫌悪 (1)",
36
+ 2: "😳 恥ずかしさ・戸惑い (2)",
37
+ 3: "😨 恐怖 (3)",
38
+ 4: "😊 幸せ (4)",
39
+ 5: "😢 悲しみ (5)",
40
+ 6: "😲 驚き (6)",
41
+ 7: "😐 中立 (7)",
42
+ 8: "🥰 NSFW1 (8)",
43
+ 9: "🍭 NSFW2 (9)",
44
+ }
45
+
46
+ current_item: dict | None = None
47
+
48
+
49
+ def _load_dataset(
50
+ *,
51
+ streaming: bool = True,
52
+ use_local_dataset: bool = False,
53
+ local_dataset_path: str | None = None,
54
+ data_dir: str = "data",
55
+ ) -> IterableDatasetDict:
56
+ data_files = {
57
+ "train": [
58
+ f"galgame-speech-ser-16kHz-train-000{index:03d}.tar"
59
+ for index in range(0, NUM_TAR_FILES)
60
+ ],
61
+ }
62
+ if use_local_dataset:
63
+ assert local_dataset_path is not None
64
+ path = local_dataset_path
65
+ else:
66
+ path = HF_PATH_TO_DATASET
67
+ dataset: IterableDatasetDict = load_dataset(
68
+ path=path, data_dir=data_dir, data_files=data_files, streaming=streaming
69
+ ) # type: ignore
70
+
71
+ dataset = dataset.remove_columns(["__url__"])
72
+ dataset = dataset.rename_column("ogg", "audio")
73
+
74
+ return dataset
75
+
76
+
77
+ logger.info("Start loading dataset")
78
+ ds = _load_dataset(streaming=True, use_local_dataset=False)
79
+ logger.info("Dataset loaded")
80
+ # seed = random.randint(0, 2**32 - 1)
81
+ # logger.info(f"Seed: {seed}")
82
+ # ds_iter = iter(ds["train"].shuffle(seed=seed))
83
+ ds_iter = iter(ds["train"])
84
+
85
+ shortcut_js = """
86
+ <script>
87
+ function shortcuts(e) {
88
+ if (e.key === "Enter") {
89
+ document.getElementById("btn_skip").click();
90
+ } else if (e.key === "0") {
91
+ document.getElementById("btn_0").click();
92
+ } else if (e.key === "1") {
93
+ document.getElementById("btn_1").click();
94
+ } else if (e.key === "2") {
95
+ document.getElementById("btn_2").click();
96
+ } else if (e.key === "3") {
97
+ document.getElementById("btn_3").click();
98
+ } else if (e.key === "4") {
99
+ document.getElementById("btn_4").click();
100
+ } else if (e.key === "5") {
101
+ document.getElementById("btn_5").click();
102
+ } else if (e.key === "6") {
103
+ document.getElementById("btn_6").click();
104
+ } else if (e.key === "7") {
105
+ document.getElementById("btn_7").click();
106
+ } else if (e.key === "8") {
107
+ document.getElementById("btn_8").click();
108
+ } else if (e.key === "9") {
109
+ document.getElementById("btn_9").click();
110
+ }
111
+ }
112
+ document.addEventListener('keypress', shortcuts, false);
113
+ </script>
114
+ """
115
+
116
+
117
+ def modify_speed(
118
+ data: tuple[int, np.ndarray], speed: float = 1.0
119
+ ) -> tuple[int, np.ndarray]:
120
+ if speed == 1.0:
121
+ return data
122
+ sr, array = data
123
+ return sr, librosa.effects.time_stretch(array, rate=speed)
124
+
125
+
126
+ def parse_item(item, speed: float = 1.0) -> dict:
127
+ label_id = item["cls"]
128
+ sampling_rate = item["audio"]["sampling_rate"]
129
+ array = item["audio"]["array"]
130
+
131
+ return {
132
+ "key": item["__key__"],
133
+ "audio": (sampling_rate, array),
134
+ "text": item["txt"],
135
+ "label": id2rich_label[label_id],
136
+ "label_id": label_id,
137
+ }
138
+
139
+
140
+ def get_next_parsed_item(speed: float = 1.0) -> dict:
141
+ logger.info("Getting next item")
142
+ next_item = next(ds_iter)
143
+ parsed = parse_item(next_item, speed=speed)
144
+ logger.info(
145
+ f"Next item:\nkey={parsed['key']}\ntext={parsed['text']}\nlabel={parsed['label']}"
146
+ )
147
+ return parsed
148
+
149
+
150
+ md = """
151
+ # 説明
152
+
153
+ - このアプリは、ゲームのセリフを感情ラベル付けして、大規模な感情音声データセットを作成するためのものです
154
+ - **性的な音声が含まれるため、18歳未満の方はご利用をお控えください**
155
+ - 既存のラベルが適切であれば、そのまま「現在の感情ラベルで適切」ボタンを押してください
156
+ - ラベルを修正する場合は、適切なボタンを押してください
157
+ - ショートカットキー(カッコ内)を使うこともできます
158
+
159
+ # 補足
160
+
161
+ - `🥰 NSFW1` は女性の性的行為中の音声(喘ぎ声等)
162
+ - `🍭 NSFW2` はキスシーンでのリップ音やフェラシーンでのしゃぶる音(チュパ音)を表します
163
+ - 感情が音声からは特に読み取れない場合は `😐 中立` を選択してください
164
+ """
165
+
166
+ with gr.Blocks(head=shortcut_js) as app:
167
+ gr.Markdown(md)
168
+ with gr.Row():
169
+ with gr.Column():
170
+ btn_init = gr.Button("初期化・再読み込み")
171
+ speed = gr.Slider(
172
+ minimum=0.5, maximum=5.0, step=0.1, value=1.0, label="再生速度"
173
+ )
174
+ with gr.Column(variant="panel"):
175
+ key = gr.Textbox(label="Key")
176
+ audio = gr.Audio()
177
+ text = gr.Textbox(label="Text")
178
+ label = gr.Textbox(label="感情ラベル")
179
+ label_id = gr.Textbox(visible=False)
180
+ btn_skip = gr.Button("現在の感情ラベルで適切 (Enter)", elem_id="btn_skip")
181
+ with gr.Column():
182
+ gr.Markdown("# 感情ラベルを修正する場合")
183
+ btn_list = [
184
+ gr.Button(id2rich_label[_id], elem_id=f"btn_{_id}") for _id in range(10)
185
+ ]
186
+
187
+ def update_current_item(data: dict) -> dict:
188
+ global current_item
189
+ if current_item is None:
190
+ speed_value = data[speed]
191
+ current_item = get_next_parsed_item(speed=speed_value)
192
+ modified_audio = modify_speed(current_item["audio"], speed=data[speed])
193
+ return {
194
+ key: current_item["key"],
195
+ audio: gr.Audio(modified_audio, autoplay=True),
196
+ text: current_item["text"],
197
+ label: current_item["label"],
198
+ label_id: current_item["label_id"],
199
+ }
200
+
201
+ def set_next_item(data: dict) -> dict:
202
+ global current_item
203
+ speed_value = data[speed]
204
+ current_item = get_next_parsed_item(speed=speed_value)
205
+ return update_current_item(data)
206
+
207
+ def put_unmodified(data: dict) -> dict:
208
+ logger.info("Putting unmodified")
209
+ current_key = data[key]
210
+ current_label_id = data[label_id]
211
+ _ = client.predict(
212
+ new_data=json.dumps(
213
+ {
214
+ "key": current_key,
215
+ "cls": int(current_label_id),
216
+ }
217
+ ),
218
+ api_name="/put_data",
219
+ )
220
+ logger.info("Unmodified sent")
221
+ return set_next_item(data)
222
+
223
+ btn_init.click(
224
+ update_current_item, inputs={speed}, outputs=[key, audio, text, label, label_id]
225
+ )
226
+
227
+ btn_skip.click(
228
+ put_unmodified,
229
+ inputs={key, label_id, speed},
230
+ outputs=[key, audio, text, label, label_id],
231
+ )
232
+
233
+ functions_list = []
234
+ for _id in range(10):
235
+
236
+ def put_label(data: dict, _id=_id) -> dict:
237
+ logger.info(f"Putting label: {id2rich_label[_id]}")
238
+ current_key = data[key]
239
+ _ = client.predict(
240
+ new_data=json.dumps(
241
+ {
242
+ "key": current_key,
243
+ "cls": _id,
244
+ }
245
+ ),
246
+ api_name="/put_data",
247
+ )
248
+ logger.info("Modified sent")
249
+ return set_next_item(data)
250
+
251
+ functions_list.append(put_label)
252
+
253
+ for _id in range(10):
254
+ btn_list[_id].click(
255
+ functions_list[_id],
256
+ inputs={key, speed},
257
+ outputs=[key, audio, text, label, label_id],
258
+ )
259
+
260
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ datasets
2
+ gradio
3
+ librosa
4
+ loguru
5
+ numpy<2.0