-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
167 lines (129 loc) · 4.15 KB
/
app.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
import warnings
from time import time
import base64
from bottle import route, run, request, response
import spacy
import socket
import sign2img
from pose_format.pose_visualizer import PoseVisualizer
import gloss2pose
POSE_LOOKUP = gloss2pose.PoseLookup("gloss2pose", "asl")
warnings.filterwarnings("ignore")
try:
import gloss2sign
except Exception as e:
print("gloss2sign", e)
############ for pickle ############
try:
class PreSettings:
lang_src = "en"
lang_trg = "en"
save_data = "text2gloss_data.pkl"
data_src = None
data_trg = None
max_len = 100
min_word_count = 3
keep_case = False
share_vocab = True
preOpt = PreSettings()
src_lang_model = spacy.load(preOpt.lang_src)
trg_lang_model = spacy.load(preOpt.lang_trg)
STOP_WORDS = ['X-', 'DESC-']
MAX_LEN = preOpt.max_len
MIN_FREQ = preOpt.min_word_count
def tokenize_src(text):
for w in STOP_WORDS:
text = text.replace(w, '')
return [tok.text for tok in src_lang_model.tokenizer(text)]
def tokenize_trg(text):
for w in STOP_WORDS:
text = text.replace(w, '')
return [tok.text for tok in trg_lang_model.tokenizer(text)]
def filter_examples_with_length(x):
return len(vars(x)['src']) <= MAX_LEN and len(vars(x)['trg']) <= MAX_LEN
except Exception as e:
print("text2gloss", e)
try:
import text2gloss
except Exception as e:
print("text2gloss", e)
####################
@route("/text2gloss")
def translate_gloss():
text = request.query.get("text", None)
if not text:
response.status = 400
return {"error": "No text provided"}
try:
start = time()
gloss = text2gloss.translate(text)
return {"gloss": gloss, "time-taken": time() - start}
except Exception as e:
response.status = 404
return {"error": str(e)}
@route("/gloss2sign")
def translate_text():
gloss = request.query.get("gloss", None)
if not gloss:
response.status = 400
return {"error": "No gloss provided"}
try:
start = time()
symbol = gloss2sign.translate(gloss)
return {"sign": symbol, "time-taken": time() - start}
except Exception as e:
response.status = 404
return {"error": str(e)}
@route("/sign2img")
def convert_text():
sign = request.query.get("sign", None)
line_color = request.query.get("line_color", None)
if not sign:
response.status = 400
return {"error": "No sign provided"}
if line_color:
line_color = tuple([int(x) for x in line_color.split(",")])
try:
start = time()
img = sign2img.convert(sign, line_color)
img_base64 = base64.b64encode(img).decode('utf-8')
return {"img": img_base64, "time-taken": time() - start}
except Exception as e:
response.status = 404
return {"error": str(e)}
@route("/gloss2pose")
def make_pose():
gloss = request.query.get("gloss", None)
if not gloss:
response.status = 400
return {"error": "No gloss provided"}
try:
start = time()
glosses = gloss2pose.prepare_glosses(gloss)
print(glosses)
if not glosses:
response.status = 404
return {"error": "No gloss found"}
pose, words = POSE_LOOKUP.gloss_to_pose(glosses)
if not pose:
response.status = 404
return {"error": "No pose found"}
gloss2pose.scale_down(pose, 512)
p = PoseVisualizer(pose, thickness=2)
img = p.save_png(None, p.draw(transparency=True))
img_base64 = base64.b64encode(img).decode('utf-8')
return {"img": img_base64, "words": words, "time-taken": time() - start}
except Exception as e:
response.status = 404
return {"error": str(e)}
@route("/")
def index():
return {"signbridge-server": "OK"}
if __name__ == "__main__":
def print_local_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
print(f"http://{s.getsockname()[0]}:7860/")
s.close()
print_local_ip()
run(host="0.0.0.0", port=7860)