-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
executable file
·297 lines (249 loc) · 9.65 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# silence all tqdm progress bars
from platform import platform
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, Field
from time import time
from glob import glob
from re import findall
import yaml
import os
from fastapi import FastAPI, HTTPException, Body
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
from nltk import download, sent_tokenize
download('punkt')
_TEXT_LEN_LIMIT = 5000
_TEXT_SPLIT_THRESHOLD = 1024
_SPLIT_LEN = 512
_use_gpu_if_available = True
class NMTModel(BaseModel):
class Config:
arbitrary_types_allowed = True
tag: str
nemo: ModelPT
platform: str
active: int
start_time: str = None
models: Dict[str, Dict[str, NMTModel]] = {}
num_requests_processed: int = None
app = FastAPI(
title='NMT API',
version=__version__,
contact={
"name": "Vitasis Inc.",
"url": "https://vitasis.si/",
"email": "[email protected]",
}
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
class TranslateRequest(BaseModel):
src_language: str = Field( ..., title="Source language", description="ISO 639-1 two-letter language code, lowercase")
tgt_language: str = Field( ..., title="Target language", description="ISO 639-1 two-letter language code, lowercase")
text: Union[str,List[str]]
translateRequestExamples = {
"text: string": {
"value": {
"src_language": "sl",
"tgt_language": "en",
"text": "Danes prijetno sneži. Jutri bo pa še lepše."
}
},
"text: [string]": {
"value": {
"src_language": "sl",
"tgt_language": "en",
"text": [ "Danes prijetno sneži.", "Jutri bo pa še lepše." ]
}
},
}
class TranslateResponse(BaseModel):
result: Union[str,List[str]]
translateResponseExamples = {
"result: string": {
"value": {
"result": "It snows well today, and tomorrow it will be even better."
}
},
"result: [string]": {
"value": {
"result": [ "It snows pleasantly today.","Tomorrow will be even better." ]
}
},
}
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": "slen:GEN:nemo-1.2.6", "workers": { "platform": "gpu", "active": 2 } },
{ "tag": "ensl:GEN:nemo-1.2.6", "workers": { "platform": "gpu", "active": 0 } },
]
}
},
"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[src_lang][tgt_lang].tag, "workers": { "platform": models[src_lang][tgt_lang].platform, "active": models[src_lang][tgt_lang].active } } for src_lang in models for tgt_lang in models[src_lang] ]
status['start_time'] = start_time
status['num_requests_processed'] = num_requests_processed
return status
@app.post(
"/api/translate",
description=f"Translate text. Maximum text lenght is {_TEXT_LEN_LIMIT}c.\n\nInput: Text.\n\nOutput: Translation.",
response_model=TranslateResponse,
responses={ 200: { "description": "Success", "content": { "application/json": { "examples": translateResponseExamples } } } }
)
def translate_text(item: TranslateRequest = Body(..., examples=translateRequestExamples)):
time0 = time()
if item.src_language.lower() not in models:
raise HTTPException(status_code=404, detail=f"Source language {item.src_language} unsupported")
if item.tgt_language.lower() not in models[item.src_language.lower()]:
raise HTTPException(status_code=404, detail=f"Target language {item.tgt_language} unsupported")
logging.info(f" Q: {item.text}")
if isinstance(item.text, str):
text = [item.text]
else:
text = item.text
text_len = sum(len(_text) for _text in text)
if text_len > _TEXT_LEN_LIMIT:
logging.warning(f'{text}, text length exceded {text_len}c [max {_TEXT_LEN_LIMIT}c]')
raise HTTPException(status_code=400, detail=f"Bad request.")
text_batch = []
text_batch_split = []
for _text in text:
if len(_text) > _TEXT_SPLIT_THRESHOLD:
_split_start = len(text_batch)
_sent = sent_tokenize(_text)
i = 0
while i < len(_sent):
j = i+1
while j < len(_sent) and len(' '.join(_sent[i:j])) < _SPLIT_LEN: j+=1
if len(' '.join(_sent[i:j])) > _TEXT_SPLIT_THRESHOLD:
_split=findall(rf'(.{{1,{_SPLIT_LEN}}})(?:\s|$)',' '.join(_sent[i:j]))
text_batch.extend(_split)
else:
text_batch.append(' '.join(_sent[i:j]))
i = j
_split_end = len(text_batch)
text_batch_split.append((_split_start,_split_end))
else:
text_batch.append(_text)
logging.debug(f' B: {text_batch}, BS: {text_batch_split}')
if _use_gpu_if_available and torch.cuda.is_available():
models[item.src_language.lower()][item.tgt_language.lower()].nemo = models[item.src_language.lower()][item.tgt_language.lower()].nemo.cuda()
models[item.src_language.lower()][item.tgt_language.lower()].active += 1
translation_batch = models[item.src_language.lower()][item.tgt_language.lower()].nemo.translate(text_batch)
logging.debug(f' BT: {translation_batch}')
models[item.src_language.lower()][item.tgt_language.lower()].active -= 1
translation = []
_start = 0
for _split_start,_split_end in text_batch_split:
if _split_start != _start:
translation.extend(translation_batch[_start:_split_start])
translation.append(' '.join(translation_batch[_split_start:_split_end]))
_start = _split_end
if _start < len(translation_batch):
translation.extend(translation_batch[_start:])
result: TranslateResponse = { "result": ' '.join(translation) if isinstance(item.text, str) else translation }
logging.info(f' R: {result}')
logging.debug(f'text_length: {text_len}c, duration: {round(time()-time0,2)}s')
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[item.src_language.lower()][item.tgt_language.lower()].nemo = models[item.src_language.lower()][item.tgt_language.lower()].nemo.cpu()
torch.cuda.empty_cache()
return result
def initialize():
time0 = time()
models: Dict[str, Dict[str, NMTModel]] = {}
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)
lang_pair = _model_info.get('language_pair', None)
if lang_pair:
_model_tag = f"{_model_info['language_pair']}:{_model_info['domain']}:{_model_info['version']}"
_model_platform = "gpu" if _use_gpu_if_available and torch.cuda.is_available() else "cpu"
_model_path = f"{os.path.dirname(_model_info_path)}/{_model_info['info']['framework'].partition(':')[-1].replace(':','_')}.{_model_info['info']['framework'].partition(':')[0]}"
model = ModelPT.restore_from(_model_path,map_location="cuda" if _model_platform == "gpu" else "cpu")
model.freeze()
model.eval()
if lang_pair != f"{model.src_language.lower()}{model.tgt_language.lower()}":
logging.warning(f"Invalid model.info; language_pair '{lang_pair}', {_model_info['info']['framework'].partition(':')[-1].replace(':','_')}.{_model_info['info']['framework'].partition(':')[0]} '{model.src_language.lower()}{model.tgt_language.lower()}', unloading")
del model
continue
models[model.src_language.lower()] = {}
models[model.src_language.lower()][model.tgt_language.lower()] = NMTModel(
tag = _model_tag,
nemo = model,
platform = _model_platform,
active = 0,
)
logging.info(f'Loaded models {[ (models[src_lang][tgt_lang].tag,models[src_lang][tgt_lang].platform) for src_lang in models for tgt_lang in models[src_lang] ]}')
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=4000)
if __name__ == "__main__":
logging.setLevel(logging.DEBUG)
start_time, models, num_requests_processed = initialize()
start_service()