-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
45 lines (34 loc) · 1.03 KB
/
app.py
File metadata and controls
45 lines (34 loc) · 1.03 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
44
45
# -*- coding: utf-8 -*-
import os
from flask import Flask, request, current_app, Response
from ckip_transformers.nlp import CkipWordSegmenter
app = Flask(__name__)
app.config |= {
'CKIP_DEVICE': int(os.environ.get('CKIP_DEVICE') or -1),
'CKIP_TRANSFORMER_MODEL': os.environ.get('CKIP_TRANSFORMER_MODEL') or 'bert-base',
}
app.config |= {
'CKIP_DRIVERS': {
name: Cls(
device=app.config['CKIP_DEVICE'],
model=app.config['CKIP_TRANSFORMER_MODEL'],
) for name, Cls in {
'ws': CkipWordSegmenter,
}.items()
},
}
@app.cli.command('touch')
def touch():
pass
@app.route('/healthz', methods=['GET'], endpoint='healthz')
def healthz():
return Response()
@app.route('/tokenize', methods=['GET'], endpoint='tokenize')
def tokenize():
tokens = []
if input_text := request.args.getlist('text'):
tokens = current_app.config['CKIP_DRIVERS']['ws'](
input_text=input_text,
show_progress=False,
)
return {'tokens': tokens}