docstr: Transform input to tensor, resample, run model and return transcribed text.
docstr_tok: ["Transform","input","to","tensor",",","resample",",","run","model","and","return","transcribed","text","."]
code: def handle(self, data, context):
      """Transform input to tensor, resample, run model and return transcribed text."""
      input = data[0].get("data")
      if input is None:
          input = data[0].get("body")
  
      # torchaudio.load accepts file like object, here `input` is bytes
      model_input, sample_rate = torchaudio.load(
          io.BytesIO(input), format="WAV"
      )
  
      # Ensure sampling rate is the same as the trained model
      if sample_rate != self.expected_sampling_rate:
          model_input = torchaudio.functional.resample(
              model_input, sample_rate, self.expected_sampling_rate
          )
  
      model_input = self.processor(
          model_input,
          sampling_rate=self.expected_sampling_rate,
          return_tensors="pt",
      ).input_values[0]
      logits = self.model(model_input)[0]
      pred_ids = torch.argmax(logits, axis=-1)[0]
      output = self.processor.decode(pred_ids)
  
      return [output]
  
file_path: serve/examples/speech2text_wav2vec2/handler.py
imports: import torch
  import torchaudio
  from transformers import AutoProcessor, AutoModelForCTC
  import io
label_desc: Use image node_id 3 for calling the Wav2VecHandler obj's underlying member method code with example usage: obj.handle(data, context) and returns: list