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

Allow submodules to be imported in python module #503

Merged
merged 1 commit into from Apr 11, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions python/seldon_core/microservice.py
Expand Up @@ -184,8 +184,15 @@ def main():
annotations = load_annotations()
logger.info("Annotations: %s", annotations)

interface_file = importlib.import_module(args.interface_name)
user_class = getattr(interface_file, args.interface_name)
parts = args.interface_name.rsplit(".", 1)
if len(parts) == 1:
logger.info("Importing ",args.interface_name)
interface_file = importlib.import_module(args.interface_name)
user_class = getattr(interface_file, args.interface_name)
else:
logger.info("Importing submodule %s",parts)
interface_file = importlib.import_module(args.interface_name)
user_class = getattr(interface_file, parts[1])

if args.persistence:
logger.info('Restoring persisted component')
Expand Down
4 changes: 4 additions & 0 deletions python/tests/model-template-app2/.s2i/environment
@@ -0,0 +1,4 @@
MODEL_NAME=mymodule.MyModel
API_TYPE=REST
SERVICE_TYPE=MODEL
PERSISTENCE=0
39 changes: 39 additions & 0 deletions python/tests/model-template-app2/mymodule/MyModel.py
@@ -0,0 +1,39 @@
import logging
logger = logging.getLogger(__name__)

class MyModel(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.
"""
logger.info("Initializing model")

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

Parameters
----------
X : array-like
feature_names : array of feature names (optional)
"""
logger.info("Predict called - will run idenity function")
return X

def send_feedback(self, features, feature_names, reward, truth, routing=None):
"""
Handle feedback

Parameters
----------
features : array - the features sent in the original predict request
feature_names : array of feature names. May be None if not available.
reward : float - the reward
truth : array with correct value (optional)
"""
logger.info("Send feedback called")
return []
Empty file.
22 changes: 22 additions & 0 deletions python/tests/test_microservice.py
Expand Up @@ -87,6 +87,28 @@ def test_model_template_app_rest(tracing):
response.raise_for_status()
assert response.json() == {'data': {'ndarray': []}, 'meta': {}}


@pytest.mark.parametrize(
'tracing', [(False), (True)]
)
def test_model_template_app_rest_submodule(tracing):
with start_microservice(join(dirname(__file__), "model-template-app2"),tracing=tracing):
data = '{"data":{"names":["a","b"],"ndarray":[[1.0,2.0]]}}'
response = requests.get(
"http://127.0.0.1:5000/predict", params="json=%s" % data)
response.raise_for_status()
assert response.json() == {
'data': {'names': ['t:0', 't:1'], 'ndarray': [[1.0, 2.0]]}, 'meta': {}}

data = ('{"request":{"data":{"names":["a","b"],"ndarray":[[1.0,2.0]]}},'
'"response":{"meta":{"routing":{"router":0}},"data":{"names":["a","b"],'
'"ndarray":[[1.0,2.0]]}},"reward":1}')
response = requests.get(
"http://127.0.0.1:5000/send-feedback", params="json=%s" % data)
response.raise_for_status()
assert response.json() == {'data': {'ndarray': []}, 'meta': {}}


@pytest.mark.parametrize(
'tracing', [(False), (True)]
)
Expand Down