Prompt48 commited on
Commit
bd00b49
·
verified ·
1 Parent(s): 36ac01c

Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\transformers\models\granite_speech\processing_granite_speech.py with huggingface_hub

Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//transformers//models//granite_speech//processing_granite_speech.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2025 The HuggingFace Inc. team.
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
+ """Processor class for Granite Speech."""
16
+
17
+ from typing import Union
18
+
19
+ from ...feature_extraction_utils import BatchFeature
20
+ from ...processing_utils import ProcessorMixin
21
+ from ...tokenization_utils import PreTokenizedInput, TextInput
22
+ from ...utils import is_torch_available, logging
23
+ from ...utils.import_utils import requires_backends
24
+
25
+
26
+ if is_torch_available():
27
+ import torch
28
+
29
+ logger = logging.get_logger(__name__)
30
+
31
+
32
+ class GraniteSpeechProcessor(ProcessorMixin):
33
+ attributes = ["audio_processor", "tokenizer"]
34
+ audio_processor_class = "GraniteSpeechFeatureExtractor"
35
+ tokenizer_class = "AutoTokenizer"
36
+
37
+ def __init__(
38
+ self,
39
+ audio_processor,
40
+ tokenizer,
41
+ audio_token="<|audio|>",
42
+ chat_template=None,
43
+ ):
44
+ self.audio_token = tokenizer.audio_token if hasattr(tokenizer, "audio_token") else audio_token
45
+ super().__init__(audio_processor, tokenizer, chat_template=chat_template)
46
+
47
+ def __call__(
48
+ self,
49
+ text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]],
50
+ audio: Union["torch.Tensor", list["torch.Tensor"]] = None,
51
+ device: str = "cpu",
52
+ images=None,
53
+ videos=None,
54
+ **kwargs,
55
+ ) -> BatchFeature:
56
+ requires_backends(self, ["torch"])
57
+
58
+ text = self._get_validated_text(text)
59
+ prompt_strings = text
60
+
61
+ if audio is not None:
62
+ # NOTE - we intentionally avoid throwing for potentially misaligned
63
+ # text / audio inputs here because some inference engines will
64
+ # trigger the conditions due to the way they call multimodal
65
+ # processors, e.g., vLLM.
66
+ audio_inputs = self.audio_processor(audio, device=device)
67
+
68
+ # TODO (@alex-jw-brooks); we should add a util to get_num_audio_tokens
69
+ # from feature lengths and call it here, rather than returning it
70
+ # from the feature extractor.
71
+ audio_embed_sizes = audio_inputs.pop("audio_embed_sizes")
72
+
73
+ # Expand the audio placeholders to match the feature dims; this
74
+ # is similar to how many VLMs handle image tokens, e.g., llava next
75
+ prompt_strings = []
76
+ num_replaced = 0
77
+ for sample in text:
78
+ while self.audio_token in sample:
79
+ sample = sample.replace(
80
+ self.audio_token,
81
+ "<placeholder>" * audio_embed_sizes[num_replaced],
82
+ 1,
83
+ )
84
+ num_replaced += 1
85
+ prompt_strings.append(sample)
86
+
87
+ prompt_strings = [sample.replace("<placeholder>", self.audio_token) for sample in prompt_strings]
88
+ else:
89
+ audio_inputs = {}
90
+
91
+ if "padding" not in kwargs:
92
+ kwargs["padding"] = True
93
+ text_inputs = self.tokenizer(prompt_strings, **kwargs)
94
+ return BatchFeature(data={**text_inputs, **audio_inputs})
95
+
96
+ def _get_validated_text(self, text: Union[str, list]) -> list[str]:
97
+ if isinstance(text, str):
98
+ return [text]
99
+ elif isinstance(text, list) and isinstance(text[0], str):
100
+ return text
101
+ raise TypeError("Invalid text provided! Text should be a string or list of strings.")
102
+
103
+
104
+ __all__ = ["GraniteSpeechProcessor"]