-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnemo_handler.py
More file actions
43 lines (34 loc) · 1.27 KB
/
Copy pathnemo_handler.py
File metadata and controls
43 lines (34 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import json
import io
import os
import torch
from nemo_model import nemoModel
from ts.torch_handler.base_handler import BaseHandler
class CustomASRHandler(BaseHandler):
def initialize(self, context):
properties = context.system_properties
model_dir = properties.get("model_dir")
config_path = os.path.join(model_dir, "config_nm.json")
with open(config_path, "r") as config_file:
config = json.load(config_file)
device = config.get("device", "cuda")
beam_size = config.get("beam_size", 4)
self.model = nemoModel(device, beam_size)
self.initialized = True
def preprocess(self, data):
if 'data' in data[0]:
temp_audio_path = "/tmp/temp_audio_file.wav"
with open(temp_audio_path, 'wb') as f:
f.write(data[0].get("data"))
return temp_audio_path
audio_file = data[0].get("body").strip()
return audio_file
def inference(self, data):
output = self.model(data)
return output
def postprocess(self, inference_output):
return [inference_output]
def handle(self, data, context):
wav = self.preprocess(data)
model_output = self.inference(wav)
return self.postprocess(model_output)