-
Notifications
You must be signed in to change notification settings - Fork 3
/
serve.py
28 lines (23 loc) · 873 Bytes
/
serve.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
import fasttext
import jieba
from constant import STOPWORDS
import re
def get_model_api():
model = fasttext.load_model("fasttext.model")
def model_api(input_data):
query = preprocessed_text(input_data)
preds_label = model.predict(query)
print(preds_label)
LABEL_2_CATE = {"__label__1": 'technology',
"__label__2": 'car',
"__label__3": 'entertainment',
"__label__4": 'military',
"__label__5": 'sports'}
output = LABEL_2_CATE[str(preds_label[0][0])]
return output
def preprocessed_text(text):
segs = jieba.lcut(re.sub("\n", "", text))
segs = list(filter(lambda x: len(x) > 1, segs))
segs = list(filter(lambda x: x not in STOPWORDS, segs))
return " ".join(segs)
return model_api