-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
executable file
·269 lines (223 loc) · 8.66 KB
/
server.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# silence all tqdm progress bars
from tqdm import tqdm
from functools import partialmethod
tqdm.__init__ = partialmethod(tqdm.__init__, disable=True)
from version import __version__
import arrow
from typing import Union, List, Dict, Optional, Any
from pydantic import BaseModel
from time import time
from glob import glob
import petname
import shutil
from pydub import AudioSegment
import yaml
import os
from fastapi import FastAPI, HTTPException, Body, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import torch
from nemo.core.classes.modelPT import ModelPT
from nemo.utils import logging
import contextlib
if torch.cuda.is_available() and hasattr(torch.cuda, 'amp') and hasattr(torch.cuda.amp, 'autocast'):
logging.info("AMP enabled!\n")
autocast = torch.cuda.amp.autocast
else:
@contextlib.contextmanager
def autocast():
yield
_AUDIO_DURATION_SECONDS_LIMIT = 300
_AUDIO_FILE_SIZE_LIMIT = 44 + _AUDIO_DURATION_SECONDS_LIMIT*16000*2
_use_gpu_if_available = True
_model_tag = "unknown"
class ASRModel(BaseModel):
class Config:
arbitrary_types_allowed = True
tag: str
nemo: ModelPT
platform: str
active: int
remap: Dict[str,str]
start_time: str = None
models: Dict[str, ASRModel] = {}
num_requests_processed: int = None
app = FastAPI(
title='ASR API',
version=__version__,
contact={
"name": "Vitasis Inc.",
"url": "https://vitasis.si/",
"email": "info@vitasis.si",
}
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
class TranscribeResponse(BaseModel):
result: str
translateResponseExamples = {
"result: string": {
"value": {
"result": "ogrožanja varnosti ljudi oziroma njihovega življenja"
}
},
}
class Model(BaseModel):
tag: str
workers: Dict[str,Any]
features: Optional[Dict[str,Any]]
info: Optional[Dict[str,Any]]
class HealthCheckResponse(BaseModel):
status: int
start_time: Optional[str]
models: Optional[List[Model]]
num_requests_processed: Optional[int]
healthCheckResponseExamples = {
"serving": {
"value": {
"status": 0,
"start_time": arrow.utcnow().isoformat(),
"models": [
{ "tag": "sl-SI:GEN:nemo-1.5", "workers": { "platform": "gpu", "active": 2 } },
]
}
},
"failed state": {
"value": {
"status": 2,
}
},
}
@app.get(
"/api/healthCheck",
description="Retrieve service health info.",
response_model=HealthCheckResponse,
responses={ 200: { "description": "Success", "content": { "application/json": { "examples": healthCheckResponseExamples } } } }
)
def health_check():
_SERVICE_UNAVAILABLE_ = -1
_PASS_ = 0
_WARN_ = 1
_FAIL_ = 2
status: HealthCheckResponse = {'status': _SERVICE_UNAVAILABLE_}
if not models:
status = {'status': _FAIL_}
else:
status = {'status': _PASS_}
min_workers_available = 1 # min([ workers_info['available'] for workers_info in _response['workers_info'] ]) if len(_response['workers_info']) > 0 else 0
if min_workers_available <= -1: # config['workers']['fail']
status = {'status': _FAIL_}
elif min_workers_available <= 0: # config['workers']['warn']:
status = {'status': _WARN_}
status['models'] = [ { "tag": models[model_tag].tag, "workers": { "platform": models[model_tag].platform, "active": models[model_tag].active } } for model_tag in models ]
status['start_time'] = start_time
status['num_requests_processed'] = num_requests_processed
return status
@app.post(
"/api/transcribe",
description=f"Transcribe audio file. No file format checking is performed. Maximum audio duration is {_AUDIO_DURATION_SECONDS_LIMIT}s.\n\nInput: 16bit 16kHz mono WAV.\n\nOutput: Transcript.",
response_model=TranscribeResponse,
responses={ 200: { "description": "Success", "content": { "application/json": { "examples": translateResponseExamples } } } }
)
def transcribe_file(audio_file: UploadFile = File( ..., title="Audio file", description="WAV, 16bit, 16kHz, mono")):
time0 = time()
session_path = f"/tmp/asr/{petname.generate()}"
while os.path.exists(session_path):
session_path = f"/tmp/asr/{petname.generate()}"
os.makedirs(session_path)
audio_file.filename=f"{session_path}/{audio_file.filename}"
try:
with open(f"{audio_file.filename}", 'wb') as f:
shutil.copyfileobj(audio_file.file, f)
except:
raise HTTPException(status_code=400, detail=f"Bad request.")
finally:
audio_file.file.close()
audio_file_size = os.path.getsize(f"{audio_file.filename}")
if audio_file_size > _AUDIO_FILE_SIZE_LIMIT:
logging.warning(f'{audio_file.filename}, file size exceded {audio_file_size}b [max {_AUDIO_FILE_SIZE_LIMIT}b]')
shutil.rmtree(session_path, ignore_errors=True)
raise HTTPException(status_code=400, detail=f"Bad request.")
audio = AudioSegment.from_file(f"{audio_file.filename}")
if audio.duration_seconds > _AUDIO_DURATION_SECONDS_LIMIT:
logging.warning(f'{audio_file.filename}, audio duration exceded {len(audio)}ms [max {_AUDIO_DURATION_SECONDS_LIMIT}s]')
shutil.rmtree(session_path, ignore_errors=True)
raise HTTPException(status_code=400, detail=f"Bad request.")
if _use_gpu_if_available and torch.cuda.is_available():
models[_model_tag].nemo = models[_model_tag].nemo.cuda()
models[_model_tag].active += 1
try:
with autocast():
with torch.no_grad():
transcriptions = models[_model_tag].nemo.transcribe([audio_file.filename], batch_size=32)
except RuntimeError:
logging.warning("Ran out of memory on device, performing inference on CPU for now")
try:
models[_model_tag].nemo = models[_model_tag].nemo.cpu()
with torch.no_grad():
transcriptions = models[_model_tag].nemo.transcribe([audio_file.filename], batch_size=32)
except Exception as e:
models[_model_tag].active -= 1
logging.error(f"Exception {e} occured while attemting to transcribe audio. Returning error message")
raise HTTPException(status_code=500, detail=f"Exception {e} occured while attemting to transcribe audio. Returning error message")
# If RNNT models transcribe, they return a tuple (greedy, beam_scores)
if type(transcriptions[0]) == list and len(transcriptions) == 2:
# get greedy transcriptions only
transcriptions = transcriptions[0]
logging.debug(f' T: {transcriptions}')
# Remap special chars
for k,v in models[_model_tag].remap.items():
for i in range(len(transcriptions)):
transcriptions[i] = transcriptions[i].replace(k,v)
logging.debug(f' T: {transcriptions}')
models[_model_tag].active -= 1
result: TranscribeResponse = { "result": transcriptions[0] }
# cleanup
shutil.rmtree(session_path, ignore_errors=True)
transcription_duration = time()-time0
logging.info(f' R: {audio_file.filename}, {result}')
logging.debug(f'audio_duration: {round(len(audio)/1000,2)}s, transcription_duration: {round(transcription_duration,2)}s, RT: {round(len(audio)/(transcription_duration*1000),2)}x')
global num_requests_processed
num_requests_processed += 1
if num_requests_processed == 0:
if _use_gpu_if_available and torch.cuda.is_available():
# Force onto CPU
models[_model_tag].nemo = models[_model_tag].nemo.cpu()
torch.cuda.empty_cache()
return result
def initialize():
time0 = time()
models: Dict[str, ASRModel] = {}
for _model_info_path in glob(f"./models/**/model.info",recursive=True):
with open(_model_info_path) as f:
_model_info = yaml.safe_load(f)
global _model_tag
_model_tag = f"{_model_info['language_code']}:{_model_info['domain']}:{_model_info['version']}"
_model_platform = "gpu" if _use_gpu_if_available and torch.cuda.is_available() else "cpu"
am=f"{_model_info['info']['am']['framework'].partition(':')[-1].replace(':','_')}.{_model_info['info']['am']['framework'].partition(':')[0]}"
_model_path=os.path.join(os.path.dirname(_model_info_path),am)
model = ModelPT.restore_from(_model_path,map_location="cuda" if _model_platform == "gpu" else "cpu")
model.freeze()
model.eval()
models[_model_tag] = ASRModel(
tag = _model_tag,
nemo = model,
platform = _model_platform,
active = 0,
remap = _model_info.get('features',[]).get('remap',[])
)
logging.info(f'Loaded models {[ (models[model_tag].tag,models[model_tag].platform) for model_tag in models ]}')
logging.info(f'Initialization finished in {round(time()-time0,2)}s')
start_time = arrow.utcnow().isoformat()
num_requests_processed = 0
return start_time, models, num_requests_processed
def start_service():
uvicorn.run(app, host="0.0.0.0", port=8000)
if __name__ == "__main__":
logging.setLevel(logging.DEBUG)
start_time, models, num_requests_processed = initialize()
start_service()