-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun.py
83 lines (57 loc) · 2.32 KB
/
run.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
#!/usr/bin/env python
# coding:utf-8
# Author: David Zhuang --<i@cnbeining.com>
# Purpose: XMLRPC Server for punctuation restoration and sentence segmentation.
# File must be compatible with Python 3.6
from socketserver import ThreadingMixIn
from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler
import logging
from deepcorrect_model import DeepCorrect
from deepsegment_model import DeepSegment
class ThreadXMLRPCServer(ThreadingMixIn, SimpleXMLRPCServer): pass
# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
def ping():
return 'pong!'
def correct_sentence(sentence):
"""str->str
correct single line.
Note this line should not be too long"""
global deepcorrect_model
return deepcorrect_model.correct(sentence)[0]['sequence']
def segment_sentence(sentence):
"""str, bool->list of str
Segment longer sentences into shorter ones.
Note this will strip down all the punctuations:
use correct arg to call DeepCorrect on segmented sentences.
Don't pass in sentences that's too long.
"""
global deepsegment_model
results = deepsegment_model.segment(sentence)
results = [correct_sentence(i) for i in results]
return results
if __name__ == '__main__':
# If running by CLI, create RPC server that will listen on
server = ThreadXMLRPCServer(("0.0.0.0", 8000),
requestHandler = RequestHandler)
# Enabling this will allow XML-RPC introspection functions - only use in trusted environment!
server.register_introspection_functions()
server.register_function(ping)
global deepcorrect_model
deepcorrect_model = DeepCorrect('/app/deeppunct_params_en', '/app/deeppunct_checkpoint_google_news')
global deepsegment_model
deepsegment_model = DeepSegment()
# pre-heat models
logging.warning(deepcorrect_model.correct("hi"))
logging.warning('DeepCorrect is loaded!')
server.register_function(correct_sentence)
logging.warning(deepsegment_model.segment("Hi how are you? I am fine thank you."))
logging.warning('DeepSegment is loaded!')
server.register_function(segment_sentence)
# allow Ctrl+C exit
try:
server.serve_forever()
except KeyboardInterrupt:
exit()