MCPcopy Create free account
hub / github.com/modelscope/FunASR / inference

Method inference

funasr/models/ctc/model.py:189–298  ·  view source on GitHub ↗

Run inference on input data. Args: data_in: Input data (audio samples, file paths, or text). data_lengths: Lengths of each input sample in the batch. key: Sample identifiers. tokenizer: Tokenizer instance for text e

(
        self,
        data_in,
        data_lengths=None,
        key: list = None,
        tokenizer=None,
        frontend=None,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

187
188
189 def inference(
190 self,
191 data_in,
192 data_lengths=None,
193 key: list = None,
194 tokenizer=None,
195 frontend=None,
196 **kwargs,
197 ):
198
199 """Run inference on input data.
200
201 Args:
202 data_in: Input data (audio samples, file paths, or text).
203 data_lengths: Lengths of each input sample in the batch.
204 key: Sample identifiers.
205 tokenizer: Tokenizer instance for text encoding/decoding.
206 frontend: Audio frontend for feature extraction.
207 **kwargs: Additional keyword arguments.
208 """
209 if kwargs.get("batch_size", 1) > 1:
210 raise NotImplementedError("batch decoding is not implemented")
211
212 meta_data = {}
213 if (
214 isinstance(data_in, torch.Tensor) and kwargs.get("data_type", "sound") == "fbank"
215 ): # fbank
216 speech, speech_lengths = data_in, data_lengths
217 if len(speech.shape) < 3:
218 speech = speech[None, :, :]
219 if speech_lengths is None:
220 speech_lengths = speech.shape[1]
221 else:
222 # extract fbank feats
223 time1 = time.perf_counter()
224 audio_sample_list = load_audio_text_image_video(
225 data_in,
226 fs=frontend.fs,
227 audio_fs=kwargs.get("fs", 16000),
228 data_type=kwargs.get("data_type", "sound"),
229 tokenizer=tokenizer,
230 )
231 time2 = time.perf_counter()
232 meta_data["load_data"] = f"{time2 - time1:0.3f}"
233 speech, speech_lengths = extract_fbank(
234 audio_sample_list, data_type=kwargs.get("data_type", "sound"), frontend=frontend
235 )
236 time3 = time.perf_counter()
237 meta_data["extract_feat"] = f"{time3 - time2:0.3f}"
238 meta_data["batch_data_time"] = (
239 speech_lengths.sum().item() * frontend.frame_shift * frontend.lfr_n / 1000
240 )
241
242 speech = speech.to(device=kwargs["device"])
243 speech_lengths = speech_lengths.to(device=kwargs["device"])
244 # Encoder
245 encoder_out, encoder_out_lens = self.encode(speech, speech_lengths)
246 if isinstance(encoder_out, tuple):

Callers

nothing calls this directly

Calls 9

encodeMethod · 0.95
extract_fbankFunction · 0.90
HypothesisClass · 0.90
DatadirWriterClass · 0.90
log_softmaxMethod · 0.45
argmaxMethod · 0.45
ids2tokensMethod · 0.45
tokens2textMethod · 0.45

Tested by

no test coverage detected