Upload edit\Qwen3-TTS-test\qwen_tts\core\models\processing_qwen3_tts.py with huggingface_hub
Browse files
edit//Qwen3-TTS-test//qwen_tts//core//models//processing_qwen3_tts.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2026 The Qwen team, Alibaba Group and the HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
from transformers.feature_extraction_utils import BatchFeature
|
| 16 |
+
from transformers.processing_utils import ProcessingKwargs, ProcessorMixin
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class Qwen3TTSProcessorKwargs(ProcessingKwargs, total=False):
|
| 20 |
+
_defaults = {
|
| 21 |
+
"text_kwargs": {
|
| 22 |
+
"padding": False,
|
| 23 |
+
"padding_side": "left",
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
class Qwen3TTSProcessor(ProcessorMixin):
|
| 28 |
+
r"""
|
| 29 |
+
Constructs a Qwen3TTS processor.
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
tokenizer ([`Qwen2TokenizerFast`], *optional*):
|
| 33 |
+
The text tokenizer.
|
| 34 |
+
chat_template (`Optional[str]`, *optional*):
|
| 35 |
+
The Jinja template to use for formatting the conversation. If not provided, the default chat template is used.
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
attributes = ["tokenizer"]
|
| 39 |
+
tokenizer_class = ("Qwen2Tokenizer", "Qwen2TokenizerFast")
|
| 40 |
+
|
| 41 |
+
def __init__(
|
| 42 |
+
self, tokenizer=None, chat_template=None
|
| 43 |
+
):
|
| 44 |
+
super().__init__(tokenizer, chat_template=chat_template)
|
| 45 |
+
|
| 46 |
+
def __call__(self, text=None, **kwargs) -> BatchFeature:
|
| 47 |
+
"""
|
| 48 |
+
Main method to prepare for the model one or several sequences(s) and audio(s). This method forwards the `text`
|
| 49 |
+
and `kwargs` arguments to Qwen2TokenizerFast's [`~Qwen2TokenizerFast.__call__`] if `text` is not `None` to encode
|
| 50 |
+
the text.
|
| 51 |
+
|
| 52 |
+
Args:
|
| 53 |
+
text (`str`, `List[str]`, `List[List[str]]`):
|
| 54 |
+
The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
|
| 55 |
+
(pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
|
| 56 |
+
`is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
if text is None:
|
| 60 |
+
raise ValueError("You need to specify either a `text` input to process.")
|
| 61 |
+
|
| 62 |
+
output_kwargs = self._merge_kwargs(
|
| 63 |
+
Qwen3TTSProcessorKwargs,
|
| 64 |
+
tokenizer_init_kwargs=self.tokenizer.init_kwargs,
|
| 65 |
+
**kwargs,
|
| 66 |
+
)
|
| 67 |
+
if not isinstance(text, list):
|
| 68 |
+
text = [text]
|
| 69 |
+
|
| 70 |
+
texts_inputs = self.tokenizer(text, **output_kwargs["text_kwargs"])
|
| 71 |
+
|
| 72 |
+
return BatchFeature(
|
| 73 |
+
data={**texts_inputs},
|
| 74 |
+
tensor_type=kwargs.get("return_tensors"),
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
def batch_decode(self, *args, **kwargs):
|
| 78 |
+
"""
|
| 79 |
+
This method forwards all its arguments to Qwen2TokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
|
| 80 |
+
refer to the docstring of this method for more information.
|
| 81 |
+
"""
|
| 82 |
+
return self.tokenizer.batch_decode(*args, **kwargs)
|
| 83 |
+
|
| 84 |
+
def decode(self, *args, **kwargs):
|
| 85 |
+
"""
|
| 86 |
+
This method forwards all its arguments to Qwen2TokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
|
| 87 |
+
the docstring of this method for more information.
|
| 88 |
+
"""
|
| 89 |
+
return self.tokenizer.decode(*args, **kwargs)
|
| 90 |
+
|
| 91 |
+
def apply_chat_template(self, conversations, chat_template=None, **kwargs):
|
| 92 |
+
if isinstance(conversations[0], dict):
|
| 93 |
+
conversations = [conversations]
|
| 94 |
+
return super().apply_chat_template(conversations, chat_template, **kwargs)
|
| 95 |
+
|
| 96 |
+
@property
|
| 97 |
+
def model_input_names(self):
|
| 98 |
+
tokenizer_input_names = self.tokenizer.model_input_names
|
| 99 |
+
return list(
|
| 100 |
+
dict.fromkeys(
|
| 101 |
+
tokenizer_input_names
|
| 102 |
+
)
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
__all__ = ["Qwen3TTSProcessor"]
|