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

Seldon Components Usage Examples #1036

Merged
merged 86 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
4f77321
adding seldon inference graph that uses input transformer for spam cl…
Nov 3, 2019
2bd2c88
adding seldon inference graph that uses input transformer for spam cl…
Nov 3, 2019
e242062
commiting contents of classifier
Nov 3, 2019
c728c71
updated readme
Nov 3, 2019
1ab253c
deleted
Nov 3, 2019
e7735f0
Delete .DS_Store
Nov 3, 2019
e6d473a
updated readme
Nov 3, 2019
6e42521
Merge branch 'seldon_component_example' of https://github.com/Sandhya…
Nov 3, 2019
5029dce
updated readme
Nov 3, 2019
587ef22
example for Seldon combiner component
Nov 3, 2019
a9309bd
Delete .DS_Store
Nov 3, 2019
ed509f7
more descriptive names for classifiers
Nov 3, 2019
1e61251
Merge branch 'seldon_component_example' of https://github.com/Sandhya…
Nov 3, 2019
4aea4eb
Delete deploy.yaml
Nov 3, 2019
826281d
adding Translator
Nov 3, 2019
7c08658
Merge branch 'seldon_component_example' of https://github.com/Sandhya…
Nov 3, 2019
88b8e73
adding readme files
Nov 3, 2019
eceab69
updating readme files
Nov 3, 2019
f2091cb
upating readme
Nov 3, 2019
dc0d5d2
adding inference graph image
Nov 3, 2019
f494156
updated README
Nov 3, 2019
2be50df
adding model pipeline image and updating readme"
Nov 3, 2019
127a2e0
adding model pipeline image and updating readme"
Nov 3, 2019
6079e24
README updated"
Nov 3, 2019
4b5f299
graph explanation pic
Nov 3, 2019
ab50759
updated readme
Nov 3, 2019
61c2c87
updated readme
Nov 3, 2019
a1b8a7a
graph explanation pic
Nov 3, 2019
2538351
updated readme
Nov 3, 2019
32c049c
added dockerhub path for docker images
Nov 3, 2019
e42f630
updated deploy.yaml
Nov 3, 2019
f368c1b
updated README
Nov 4, 2019
8131877
updated Combiner.py
Nov 4, 2019
005c611
updated Combiner.py
Nov 4, 2019
9724d33
updated Combiner.py
Nov 4, 2019
71e3204
adding graph explanation
Nov 4, 2019
12ff049
updated README
Nov 4, 2019
9039d8c
updated README
Nov 4, 2019
3ec7f10
updated README
Nov 4, 2019
24c2a7f
updated README
Nov 4, 2019
1ba5b1e
updated README
Nov 4, 2019
a9c7b04
updated README files
Nov 4, 2019
0521475
updated README files
Nov 4, 2019
38842db
updated README files
Nov 4, 2019
52aa5b3
working example with correct model file and vectorizer
Nov 4, 2019
4df63b6
Delete .DS_Store
Nov 4, 2019
6c3df8d
Delete Untitled.ipynb
Nov 4, 2019
ea9f710
Delete .DS_Store
Nov 4, 2019
a0ad806
added example of how to test the image
Nov 4, 2019
ea20f34
Merge branch 'seldon_component_example' of https://github.com/Sandhya…
Nov 4, 2019
fc8149e
added example of how to test the image
Nov 4, 2019
4ec47fc
updated code and README for Translator image
Nov 4, 2019
2a663a5
updated skl-spam-classifier
Nov 4, 2019
eea2ee9
added keras model architecture files along with working python code
Nov 4, 2019
59577a2
Delete .DS_Store
Nov 4, 2019
cebfc84
Delete .DS_Store
Nov 4, 2019
c1c5e92
Delete .DS_Store
Nov 4, 2019
f74c1f1
updated code for Translator plus added requirements
Nov 4, 2019
992fc5d
updated code for Translator plus added requirements
Nov 4, 2019
efb3a91
resolved conflict
Nov 4, 2019
59f7d4c
updated readme
Nov 4, 2019
2a7683f
updated readme
Nov 4, 2019
7d5ce26
added .gitignore
Nov 4, 2019
c030093
added .gitignore
Nov 4, 2019
65ea8af
Delete .DS_Store
Nov 4, 2019
7b5a6a7
tested combiner image
Nov 4, 2019
df08915
Merge branch 'seldon_component_example' of https://github.com/Sandhya…
Nov 4, 2019
b3ec36e
updated requirements
Nov 5, 2019
9b7798b
updated src code
Nov 5, 2019
48b5c73
deployment files to work as in on k8s cluster assuming seldon-core is…
Nov 5, 2019
027e2be
updated Readme
Nov 5, 2019
678fbad
updated Readme
Nov 5, 2019
b5b7a8d
Delete .DS_Store
Nov 5, 2019
3dd1d4d
notebook file to generate the model binaries
Nov 5, 2019
3dcb5e6
Merge branch 'seldon_component_example' of https://github.com/Sandhya…
Nov 5, 2019
d7ca6d3
notebook file to generate the model binaries
Nov 5, 2019
3f580bf
notebook files to train the model locally and build docker images usi…
Nov 7, 2019
0b877a9
renamed notebook files"
Nov 7, 2019
f8e6dd8
renamed notebook file
Nov 7, 2019
0572205
renamed notebook file
Nov 7, 2019
2cd0032
nblink for combiner example
Nov 7, 2019
48e468b
nblink for transformer example
Nov 7, 2019
d5d401f
Delete input_transformer_spam.nblink
Nov 7, 2019
ddf00bd
added path to new nblink files
Nov 7, 2019
0527a67
deleted model pkl files
Nov 7, 2019
3fec8c8
deleted model pkl files
Nov 7, 2019
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
Binary file not shown.
3 changes: 3 additions & 0 deletions doc/source/examples/combiner_spam_model.nblink
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"path": "../../../examples/combiners/spam_clf_combiner/spam-classification.ipynb"
}
2 changes: 2 additions & 0 deletions doc/source/examples/notebooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ Notebooks
Sklearn, Xgboost, Tensorflow Server Examples <server_examples>
Tensorflow MNIST <deep_mnist>
TFserving MNIST <tfserving_mnist>
Spam Classification TRANSFORMER component <transformer_spam_model>
Spam Classification TRANSFORMER + COMBINER Component <combiner_spam_model>

3 changes: 3 additions & 0 deletions doc/source/examples/transformer_spam_model.nblink
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"path": "../../../examples/input_tranformer/spam-classification.ipynb"
}
Binary file added examples/.DS_Store
Binary file not shown.
Binary file added examples/combiners/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions examples/combiners/spam_clf_combiner/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MODEL_NAME=Combiner
API_TYPE=REST
SERVICE_TYPE=COMBINER
PERSISTENCE=0
22 changes: 22 additions & 0 deletions examples/combiners/spam_clf_combiner/Combiner/Combiner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import numpy as np

class Combiner(object):

def aggregate(self, Xs, features_names=None):
"""average out the probabilities from multiple classifier and return that as a result"""
return np.mean([float(x[0]) for x in Xs])



# if __name__== "__main__":
# clf1_res = np.array(['0.80', 'Spam'])
# clf2_res = np.array(['0.9959868467126312e-04', 'Spam'])

# example = np.array([clf1_res, clf2_res])
# combine = Combiner()
# res = combine.aggregate(example, features_names=None)
# print(res)




33 changes: 33 additions & 0 deletions examples/combiners/spam_clf_combiner/Combiner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# **Combiner**



#### Activate Virtual Env

Create a Python virtual environment:

```
$ python3 -m venv venv
$ source venv/bin/activate
```

#### Install s2i


linux: https://computingforgeeks.com/install-source-to-image-toolkit-on-linux/
mac: brew install source-to-image


#### Build

```
$ s2i build Combiner/ seldonio/seldon-core-s2i-python3:0.7 combiner:1.0.0.1
$ docker push <yourdockerhubusername>/combiner:1.0.0.1
```

#### Test

```
$ docker run --name "Combiner" --rm <yourdockerhubusername>/combiner:1.0.0.1

```
23 changes: 23 additions & 0 deletions examples/combiners/spam_clf_combiner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#### Model Deployment using Seldon Transformer + Combiner Component

Consider, we want to predict whether a text message is spam or not. The text data may contain multiple languages as well as each individual text may also have words from different languages. As per your needs, you are only interested in building a model on English language. So, you want to first translate the text message in English and then pass it to model to classify whether it is a spam or not. Moreover, we want to train two models: one that uses Neural network architecture using keras library and other that uses traditional machine learning classifier from scikit-learn. At runtime, we want to run both mdoels on a given input and output the average of the both predictions:

Example: <br/>
If classifier 1 outputs the probability of 0.7 for the message to be spam <br/>
Classifier 2 outputs the probability of 0.8 for the message to be spam <br/>
The final model using seldon-core graph would output the average probability of both classifiers: 0.75



![Model Pipeline](https://github.com/SandhyaaGopchandani/seldon-core/blob/seldon_component_example/examples/combiners/spam_clf_combiner/seldon_inference_graph.png)


Each component in Seldon Inference graph is a microservice – specifically a docker container. The Seldon graph links those docker containers (in deploy.yaml file) to result in an end output.


![Graph Explanation](https://github.com/SandhyaaGopchandani/seldon-core/blob/seldon_component_example/examples/combiners/spam_clf_combiner/graph_explained.png)

The repository contains the folders for for each docker image needed for seldon graph to work. The images used for this example are already published in public dockerhub. So, deploy the model on k8s cluster using:

kubectl apply -f deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MODEL_NAME=Translator
API_TYPE=REST
SERVICE_TYPE=TRANSFORMER
PERSISTENCE=0
36 changes: 36 additions & 0 deletions examples/combiners/spam_clf_combiner/Translator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# **Translator**



#### Activate Virtual Env

Create a Python virtual environment:

```
$ python3 -m venv venv
$ source venv/bin/activate
```

#### Install s2i


linux: https://computingforgeeks.com/install-source-to-image-toolkit-on-linux/
mac: brew install source-to-image


#### Build

```
$ s2i build . seldonio/seldon-core-s2i-python3:0.7 translator:1.0.0.1
$ docker push <yourdockerhubusername>/translator:1.0.0.1
```

#### Test

```
$ docker run --name "translator" --rm <yourdockerhubusername>/translator:1.0.0.1

curl -g http://localhost:5000/transform_input --data-urlencode 'json={"data": {"names": ["message"], "ndarray": ["Wie läuft dein Tag"]}}'


```
26 changes: 26 additions & 0 deletions examples/combiners/spam_clf_combiner/Translator/Translator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import goslate
import numpy as np

class Translator():
def __init__(self):
self.gs = goslate.Goslate()



def transform_input(self, text_msg, feature_names=None):

"""the translator logic will go here. This shows a use of simple library. But translator service can be a Machine Learning model itself"""
translated = self.gs.translate(text_msg[0], 'en')
return np.array([translated])



# if __name__== "__main__":
# t = Translator()
# example = np.array(['Wie läuft dein Tag'])
# translated = t.transform_input(example)
# print(translated)




Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
goslate
numpy
58 changes: 58 additions & 0 deletions examples/combiners/spam_clf_combiner/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
apiVersion: machinelearning.seldon.io/v1alpha2
kind: SeldonDeployment
metadata:
labels:
app: seldon
name: seldon-spam-classifier
namespace: default
spec:
annotations:
project_name: seldon-spam-classifier
deployment_version: v1
seldon.io/rest-read-timeout: '100000'
seldon.io/rest-connection-timeout: '100000'
seldon.io/grpc-read-timeout: '100000'
name: seldon-spam-classifier
predictors:
- componentSpecs:
- spec:
containers:
- name: combiner
image: sandhya1594/combiner:1.0.0.1
imagePullPolicy: IfNotPresent
- name: spam-classifier
image: sandhya1594/spam-classifier:1.0.0.1
imagePullPolicy: IfNotPresent
- name: keras-spam-classifier
image: sandhya1594/keras-spam-classifier:1.0.0.1
imagePullPolicy: IfNotPresent
- name: translator
image: sandhya1594/translator:1.0.0.1
imagePullPolicy: IfNotPresent
terminationGracePeriodSeconds: 20
graph:
children:
- name: combiner
endpoint:
type: REST
type: COMBINER
children:
- name: keras-spam-classifier
endpoint:
type: REST
type: MODEL
children: []
- name: spam-classifier
endpoint:
type: REST
type: MODEL
children: []
name: translator
endpoint:
type: REST
type: TRANSFORMER
name: seldon-combiner
replicas: 1
annotations:
predictor_version: v1
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MODEL_NAME=KerasSpamClassifier
API_TYPE=REST
SERVICE_TYPE=MODEL
PERSISTENCE=0
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from pathlib import Path
import numpy as np
import pickle
from sklearn.externals import joblib
from keras.preprocessing import sequence
from keras.engine.saving import model_from_json

class KerasSpamClassifier():

def __init__(self, model_path = Path('./model')):
self._architecture_path = model_path.joinpath('architecture.json')
self._weights_path = model_path.joinpath('weights.h5')
tokenizer_path = model_path.joinpath('tokenizer.pkl')

self.max_len = 150
# loading of trained tokenizer
with tokenizer_path.open('rb') as handle:
self.tokenizer = joblib.load(handle)

with self._architecture_path.open() as f:
self.model = model_from_json(f.read())
self.model.load_weights(self._weights_path.as_posix())
self.model._make_predict_function()



def predict(self, text, feature_names=None): #List[Tuple[float, float]]:
"""
Predict on a english text you got from translator service. The output returns the probability of text being spam
"""
seq= self.tokenizer.texts_to_sequences([text])
padded = sequence.pad_sequences(seq, maxlen=self.max_len)
prob = self.model.predict(padded)
prob = prob[0][0]
return np.array([prob, "spam"])


# if __name__== "__main__":

# text = 'please click this link to win the price'
# #vect_path=Path('/Users/sandhya.sandhya/Desktop/data/doc/data'),
# sc = KerasSpamClassifier(model_path=Path('./model'))
# res = sc.predict(text, feature_names=None)
# print(res)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# **Spam Classifier**



#### Activate Virtual Env

Create a Python virtual environment:

```
$ python3 -m venv venv
$ source venv/bin/activate
```

#### Install s2i


linux: https://computingforgeeks.com/install-source-to-image-toolkit-on-linux/
mac: brew install source-to-image


#### Build

```
$ s2i build . seldonio/seldon-core-s2i-python3:0.7 keras-spam-classifier:1.0.0.1
$ docker push <yourdockerhubusername>/keras-spam-classifier:1.0.0.1
```

#### Test

```
$ docker run --name "spam-classifier" --rm -d -p 5000:5000 keras-spam-classifier:1.0.0.1

curl -g http://localhost:5000/predict --data-urlencode 'json={"data": {"names": ["message"], "ndarray": ["please click this link to win the price"]}}'

Result:

{"data":{"ndarray":["0.91942614","spam"]},"meta":{}}


```





Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
scikit-learn==0.21.2
numpy>=1.9.2
keras
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MODEL_NAME=SpamClassifier
API_TYPE=REST
SERVICE_TYPE=MODEL
PERSISTENCE=0
43 changes: 43 additions & 0 deletions examples/combiners/spam_clf_combiner/skl-spam-classifier/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# **Spam Classifier**



#### Activate Virtual Env

Create a Python virtual environment:

```
$ python3 -m venv venv
$ source venv/bin/activate
```

#### Install s2i


linux: https://computingforgeeks.com/install-source-to-image-toolkit-on-linux/
mac: brew install source-to-image


#### Build

```
$ s2i build . seldonio/seldon-core-s2i-python3:0.7 spam-classifier:1.0.0.1
$ docker push <yourdockerhubusername>/spam-classifier:1.0.0.1
```

#### Test

```
$ docker run --name "spam-classifier" --rm -d -p 5000:5000 spam-classifier:1.0.0.1

curl -g http://localhost:5000/predict --data-urlencode 'json={"data": {"names": ["message"], "ndarray": ["click here to win the price"]}}'

Result:

{"data":{"ndarray":["0.9785519250237192","spam"]},"meta":{}}

```




Loading