Upload edit\Qwen3-TTS-test\finetuning\prepare_data.py with huggingface_hub
Browse files
edit//Qwen3-TTS-test//finetuning//prepare_data.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2026 The Alibaba Qwen team.
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 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 |
+
|
| 17 |
+
import argparse
|
| 18 |
+
import json
|
| 19 |
+
|
| 20 |
+
from qwen_tts import Qwen3TTSTokenizer
|
| 21 |
+
|
| 22 |
+
BATCH_INFER_NUM = 32
|
| 23 |
+
|
| 24 |
+
def main():
|
| 25 |
+
parser = argparse.ArgumentParser()
|
| 26 |
+
parser.add_argument("--device", type=str, default="cuda:0")
|
| 27 |
+
parser.add_argument("--tokenizer_model_path", type=str, default="Qwen/Qwen3-TTS-Tokenizer-12Hz")
|
| 28 |
+
parser.add_argument("--input_jsonl", type=str, required=True)
|
| 29 |
+
parser.add_argument("--output_jsonl", type=str, required=True)
|
| 30 |
+
args = parser.parse_args()
|
| 31 |
+
|
| 32 |
+
tokenizer_12hz = Qwen3TTSTokenizer.from_pretrained(
|
| 33 |
+
args.tokenizer_model_path,
|
| 34 |
+
device_map=args.device,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
total_lines = open(args.input_jsonl).readlines()
|
| 38 |
+
total_lines = [json.loads(line.strip()) for line in total_lines]
|
| 39 |
+
|
| 40 |
+
final_lines = []
|
| 41 |
+
batch_lines = []
|
| 42 |
+
batch_audios = []
|
| 43 |
+
for line in total_lines:
|
| 44 |
+
|
| 45 |
+
batch_lines.append(line)
|
| 46 |
+
batch_audios.append(line['audio'])
|
| 47 |
+
|
| 48 |
+
if len(batch_lines) >= BATCH_INFER_NUM:
|
| 49 |
+
enc_res = tokenizer_12hz.encode(batch_audios)
|
| 50 |
+
for code, line in zip(enc_res.audio_codes, batch_lines):
|
| 51 |
+
line['audio_codes'] = code.cpu().tolist()
|
| 52 |
+
final_lines.append(line)
|
| 53 |
+
batch_lines.clear()
|
| 54 |
+
batch_audios.clear()
|
| 55 |
+
|
| 56 |
+
if len(batch_audios) > 0:
|
| 57 |
+
enc_res = tokenizer_12hz.encode(batch_audios)
|
| 58 |
+
for code, line in zip(enc_res.audio_codes, batch_lines):
|
| 59 |
+
line['audio_codes'] = code.cpu().tolist()
|
| 60 |
+
final_lines.append(line)
|
| 61 |
+
batch_lines.clear()
|
| 62 |
+
batch_audios.clear()
|
| 63 |
+
|
| 64 |
+
final_lines = [json.dumps(line, ensure_ascii=False) for line in final_lines]
|
| 65 |
+
|
| 66 |
+
with open(args.output_jsonl, 'w') as f:
|
| 67 |
+
for line in final_lines:
|
| 68 |
+
f.writelines(line + '\n')
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
main()
|