Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for flatbuffers python wrappers #205

Merged
merged 2 commits into from
Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions wrappers/python/model_microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,31 +134,31 @@ def __init__(self,user_model):
super(SeldonFlatbuffersServer, self).__init__()
self.user_model = user_model

async def handle_stream(self, stream, address):
@gen.coroutine
def handle_stream(self, stream, address):
while True:
try:
data = await stream.read_bytes(4)
data = yield stream.read_bytes(4)
obj = struct.unpack('<i',data)
len_msg = obj[0]
data = await stream.read_bytes(len_msg)
data = yield stream.read_bytes(len_msg)
try:
features,names = SeldonRPCToNumpyArray(data)
predictions = np.array(predict(self.user_model,features,names))
if len(predictions.shape)>1:
print(predictions.shape)
class_names = get_class_names(self.user_model, predictions.shape[1])
else:
class_names = []
outData = NumpyArrayToSeldonRPC(predictions,class_names)
await stream.write(outData)
yield stream.write(outData)
except StreamClosedError:
print("Stream closed during processing:",address)
break
except Exception:
tb = traceback.format_exc()
print("Caught exception during processing:",address,tb)
outData = CreateErrorMsg(tb)
await stream.write(outData)
yield stream.write(outData)
stream.close()
break;
except StreamClosedError:
Expand Down
5 changes: 2 additions & 3 deletions wrappers/python/seldon_flatbuffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def SeldonRPCToNumpyArray(data):
shape = []
for i in range(tensor.ShapeLength()):
shape.append(tensor.Shape(i))
print(shape)
values = tensor.ValuesAsNumpy()
values = values.reshape(shape)
return (values,names)
Expand Down Expand Up @@ -130,7 +129,7 @@ def CreateNumpyVector(builder, x):
if not isinstance(x, np.ndarray):
raise TypeError("non-numpy-ndarray passed to CreateNumpyVector")

if x.data.ndim > 1:
if x.ndim > 1:
raise TypeError("multidimensional-ndarray passed to CreateNumpyVector")

builder.StartVector(x.itemsize, x.size, x.dtype.alignment)
Expand All @@ -139,7 +138,7 @@ def CreateNumpyVector(builder, x):
if x.dtype.str[0] == "<":
x_lend = x
else:
x_lend = x.byteswap()
x_lend = x.byteswap(inplace=False)

# Calculate total length
l = UOffsetTFlags.py_type(x_lend.itemsize * x_lend.size)
Expand Down
1 change: 1 addition & 0 deletions wrappers/s2i/python/build_python2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
make build
4 changes: 4 additions & 0 deletions wrappers/s2i/python/test_fbs/.s2i/environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MODEL_NAME=Test
API_TYPE=FBS
SERVICE_TYPE=MODEL
PERSISTENCE=0
1 change: 1 addition & 0 deletions wrappers/s2i/python/test_fbs/READNE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A simple dummy model to test wrapping.
23 changes: 23 additions & 0 deletions wrappers/s2i/python/test_fbs/Test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Test(object):
"""
Model template. You can load your model parameters in __init__ from a location accessible at runtime
"""

def __init__(self):
"""
Add any initialization parameters. These will be passed at runtime from the graph definition parameters defined in your seldondeployment kubernetes resource manifest.
"""
print("Initializing")

def predict(self,X,features_names):
"""
Return a prediction.

Parameters
----------
X : array-like
feature_names : array of feature names (optional)
"""
print("Predict called - will run identity function")
print(X)
return X
39 changes: 39 additions & 0 deletions wrappers/s2i/python/test_fbs/contract.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"features":[
{
"name":"sepal_length",
"dtype":"FLOAT",
"ftype":"continuous",
"range":[4,8]
},
{
"name":"sepal_width",
"dtype":"FLOAT",
"ftype":"continuous",
"range":[2,5]
},
{
"name":"petal_length",
"dtype":"FLOAT",
"ftype":"continuous",
"range":[1,10]
},
{
"name":"petal_width",
"dtype":"FLOAT",
"ftype":"continuous",
"range":[0,3]
}
],
"targets":[
{
"name":"class",
"dtype":"FLOAT",
"ftype":"continuous",
"range":[0,1],
"repeat":3
}
]
}


1 change: 1 addition & 0 deletions wrappers/s2i/python/test_fbs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
numpy
6 changes: 6 additions & 0 deletions wrappers/s2i/python/test_fbs/run_test_python2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

s2i build . seldonio/seldon-core-s2i-python2:0.2-SNAPSHOT test-wrapper-py2:0.1
docker run --name "test_predictor" --rm -d -p 5000:5000 test-wrapper-py2:0.1
sleep 2
python ../../../testing/tester.py contract.json 0.0.0.0 5000 -p --fbs
docker rm -f test_predictor
6 changes: 6 additions & 0 deletions wrappers/s2i/python/test_fbs/run_test_python3
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

s2i build . seldonio/seldon-core-s2i-python3:0.2-SNAPSHOT test-wrapper-py3:0.1
docker run --name "test_predictor" --rm -d -p 5000:5000 test-wrapper-py3:0.1
sleep 2
python ../../../testing/tester.py contract.json 0.0.0.0 5000 -p --fbs
docker rm -f test_predictor
1 change: 0 additions & 1 deletion wrappers/testing/tester_flatbuffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def SeldonRPCToNumpyArray(data):
shape = []
for i in range(tensor.ShapeLength()):
shape.append(tensor.Shape(i))
print(shape)
values = tensor.ValuesAsNumpy()
values = values.reshape(shape)
return (values,names)
Expand Down