-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapper.py
188 lines (151 loc) · 5.52 KB
/
wrapper.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
#!/usr/bin/env python
# coding:utf-8
"""
@author: nivic ybyang7
@license: Apache Licence
@file: wrapper.py
@time: 2022/06/16
@contact: [email protected]
@site:
@software: PyCharm
"""
# Copyright (c) 2022. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
# Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
# Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
# Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
# Vestibulum commodo. Ut rhoncus gravida arcu.
import sys
try:
from aiges_embed import ResponseData, Response, DataListNode, DataListCls # c++
except:
from aiges.dto import Response, ResponseData, DataListNode, DataListCls
import hashlib
from aiges.sdk import WrapperBase, \
StringParamField, \
ImageBodyField, \
StringBodyField
from aiges.utils.log import log
'''
定义请求类:
params: params 开头的属性代表最终HTTP协议中的功能参数parameters部分,
params Field支持 StringParamField,
NumberParamField,BooleanParamField,IntegerParamField,每个字段均支持枚举
params 属性多用于协议中的控制字段,请求body字段不属于params范畴
input: input字段多用与请求数据段,即body部分,当前支持 ImageBodyField, StringBodyField, 和AudioBodyField
'''
class UserRequest(object):
params1 = StringParamField(key="p1", enums=["3", "eee"], value='3')
params2 = StringParamField(key="p2", maxLength=44, required=True)
params3 = StringParamField(key="p3", maxLength=44, required=False)
input1 = ImageBodyField(key="data", path="test_data/test.png")
input3 = ImageBodyField(key="data2", path="test_data/test.png")
input2 = StringBodyField(key="switch", value="cc")
'''
定义响应类:
accepts: accepts代表响应中包含哪些字段, 以及数据类型
input: input字段多用与请求数据段,即body部分,当前支持 ImageBodyField, StringBodyField, 和AudioBodyField
'''
class UserResponse(object):
accept1 = StringBodyField(key="boxes")
accept2 = StringBodyField(key="boxes2")
'''
用户实现, 名称必须为Wrapper, 必须继承SDK中的 WrapperBase类
'''
class Wrapper(WrapperBase):
serviceId = "mmocr"
version = "backup.0"
requestCls = UserRequest()
responseCls = UserResponse()
'''
服务初始化
@param config:
插件初始化需要的一些配置,字典类型
key: 配置名
value: 配置的值
@return
ret: 错误码。无错误时返回0
'''
def wrapperInit(cls, config: {}) -> int:
print(config)
print("Initializing ..")
return 0
'''
非会话模式计算接口,对应oneShot请求,可能存在并发调用
@param usrTag 句柄
@param params 功能参数
@param reqData 请求数据实体字段
@param respData 返回结果实体,内存由底层服务层申请维护,通过execFree()接口释放
@param psrIds 需要使用的个性化资源标识列表
@param psrCnt 需要使用的个性化资源个数
@return 接口错误码
reqDat
ret:错误码。无错误码时返回0
'''
def wrapperOnceExec(cls, params: {}, reqData: DataListCls, usrTag: str = "", psrID: int = 0) -> Response:
log.info("got reqdata , %s" % reqData.list)
# print(type(reqData.list[0].data))
# print(type(reqData.list[0].data))
# print(reqData.list[0].len)
for req in reqData.list:
log.info("reqData key: %s , size is %d" % (req.key, len(req.data)))
log.warning("reqData bytes md5sum is %s" % hashlib.md5(reqData.list[0].data).hexdigest())
log.info("I am infer logic...please inplement")
log.info("Testing reqData get: ")
rg = reqData.get("data")
log.info("get key: %s" % rg.key)
log.info("get key: %d" % len(rg.data))
# test not reqdata
k = "dd"
n = reqData.get(k)
if not n:
log.error("reqData not has this key %s" % k)
log.warning("reqData bytes md5sum is %s" % hashlib.md5(reqData.list[0].data).hexdigest())
log.info("I am infer logic...please inplement")
r = Response()
# 错误处理
# return r.response_err(100)
l = ResponseData()
l.key = "ccc"
l.status = 1
d = open("test_data/test.png", "rb").read()
l.len = len(d)
l.data = d
l.type = 0
r.list = [l, l, l]
return r
'''
服务逆初始化
@return
ret:错误码。无错误码时返回0
'''
def wrapperFini(cls) -> int:
return 0
def wrapperError(cls, ret: int) -> str:
if ret == 100:
return "user error defined here"
return ""
def wrapperCreate(cls, params: {}, errNum: int, sid: str, persId: int = 0) -> str:
errNum = 5
print(params)
handle = "ccch"
return handle
def wrapperLoadRes(self, reqData: DataListCls, resId: int) -> int:
return 0
def wrapperUnloadRes(self, resId: int) -> int:
return 0
def wrapperTestFunc(cls, data: [], respData: []):
r = Response()
l = ResponseData()
l.key = "ccc"
l.status = 1
d = open("pybind11/docs/pybind11-logo.png", "rb").read()
l.len = len(d)
l.data = d
r.list = [l, l, l]
print(r.list)
print(444)
return r
if __name__ == '__main__':
m = Wrapper()
# m.schema()
m.run()