Skip to content

Commit

Permalink
Added python and testing folders as black
Browse files Browse the repository at this point in the history
  • Loading branch information
axsaucedo committed Oct 22, 2019
1 parent c73fb85 commit 6a85bdf
Show file tree
Hide file tree
Showing 47 changed files with 2,341 additions and 1,295 deletions.
2 changes: 1 addition & 1 deletion python/seldon_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from seldon_core.version import __version__
from .storage import Storage
from .storage import Storage
67 changes: 47 additions & 20 deletions python/seldon_core/api_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,26 @@ def get_seldon_client(args) -> SeldonClient:
else:
seldon_grpc_endpoint = None
seldon_rest_endpoint = endpoint
sc = SeldonClient(gateway="seldon", seldon_rest_endpoint=seldon_rest_endpoint,
seldon_grpc_endpoint=seldon_grpc_endpoint,
oauth_key=args.oauth_key, oauth_secret=args.oauth_secret)
sc = SeldonClient(
gateway="seldon",
seldon_rest_endpoint=seldon_rest_endpoint,
seldon_grpc_endpoint=seldon_grpc_endpoint,
oauth_key=args.oauth_key,
oauth_secret=args.oauth_secret,
)
else:
gateway_endpoint = endpoint
if args.grpc:
transport = "grpc"
else:
transport = "rest"
sc = SeldonClient(gateway="ambassador", gateway_endpoint=gateway_endpoint, transport=transport,
deployment_name=args.deployment, namespace=args.namespace)
sc = SeldonClient(
gateway="ambassador",
gateway_endpoint=gateway_endpoint,
transport=transport,
deployment_name=args.deployment,
namespace=args.namespace,
)
return sc


Expand All @@ -52,7 +61,7 @@ def run_send_feedback(args):
Command line args
"""
contract = json.load(open(args.contract, 'r'))
contract = json.load(open(args.contract, "r"))
contract = unfold_contract(contract)
sc = get_seldon_client(args)
if args.grpc:
Expand All @@ -61,11 +70,15 @@ def run_send_feedback(args):
transport = "rest"

for i in range(args.n_requests):
batch = generate_batch(contract, args.batch_size, 'features')
batch = generate_batch(contract, args.batch_size, "features")
response_predict = sc.predict(data=batch, deployment_name=args.deployment)
response_feedback = sc.feedback(prediction_request=response_predict.request,
prediction_response=response_predict.response, reward=1.0,
deployment_name=args.deployment, transport=transport)
response_feedback = sc.feedback(
prediction_request=response_predict.request,
prediction_response=response_predict.response,
reward=1.0,
deployment_name=args.deployment,
transport=transport,
)
if args.prnt:
print(f"RECEIVED RESPONSE:\n{response_feedback}\n")

Expand All @@ -80,7 +93,7 @@ def run_predict(args):
Command line args
"""
contract = json.load(open(args.contract, 'r'))
contract = json.load(open(args.contract, "r"))
contract = unfold_contract(contract)
feature_names = [feature["name"] for feature in contract["features"]]

Expand All @@ -92,36 +105,50 @@ def run_predict(args):
payload_type = "tensor" if args.tensor else "ndarray"

for i in range(args.n_requests):
batch = generate_batch(contract, args.batch_size, 'features')
batch = generate_batch(contract, args.batch_size, "features")
if args.prnt:
print(f"{'-' * 40}\nSENDING NEW REQUEST:\n")
print(batch)
response_predict = sc.predict(data=batch, deployment_name=args.deployment, names=feature_names, payload_type=payload_type)
response_predict = sc.predict(
data=batch,
deployment_name=args.deployment,
names=feature_names,
payload_type=payload_type,
)
if args.prnt:
print(f"RECEIVED RESPONSE:\n{response_predict.response}\n")


def main():
parser = argparse.ArgumentParser()
parser.add_argument("contract", type=str,
help="File that contains the data contract")
parser.add_argument(
"contract", type=str, help="File that contains the data contract"
)
parser.add_argument("host", type=str)
parser.add_argument("port", type=int)
parser.add_argument("deployment", type=str, nargs='?', default="mymodel")
parser.add_argument("--endpoint", type=str, choices=["predict", "send-feedback"], default="predict")
parser.add_argument("deployment", type=str, nargs="?", default="mymodel")
parser.add_argument(
"--endpoint", type=str, choices=["predict", "send-feedback"], default="predict"
)
parser.add_argument("-b", "--batch-size", type=int, default=1)
parser.add_argument("-n", "--n-requests", type=int, default=1)
parser.add_argument("--grpc", action="store_true")
parser.add_argument("-t", "--tensor", action="store_true")
parser.add_argument("-p", "--prnt", action="store_true", help="Prints requests and responses")
parser.add_argument("--log-level", type=str, choices=["DEBUG", "INFO", "ERROR"], default="ERROR")
parser.add_argument(
"-p", "--prnt", action="store_true", help="Prints requests and responses"
)
parser.add_argument(
"--log-level", type=str, choices=["DEBUG", "INFO", "ERROR"], default="ERROR"
)
parser.add_argument("--namespace", type=str)
parser.add_argument("--oauth-port", type=int)
parser.add_argument("--oauth-key")
parser.add_argument("--oauth-secret")

args = parser.parse_args()
LOG_FORMAT = '%(asctime)s - %(name)s:%(funcName)s:%(lineno)s - %(levelname)s: %(message)s'
LOG_FORMAT = (
"%(asctime)s - %(name)s:%(funcName)s:%(lineno)s - %(levelname)s: %(message)s"
)
if args.log_level == "DEBUG":
log_level = logging.DEBUG
elif args.log_level == "INFO":
Expand Down
37 changes: 25 additions & 12 deletions python/seldon_core/flask_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Dict
import base64


def get_multi_form_data_request() -> Dict:
"""
Parses a request submitted with Content-type:multipart/form-data
Expand All @@ -18,23 +19,24 @@ def get_multi_form_data_request() -> Dict:
"""
req_dict = {}
for key in request.form:
if key == 'strData':
req_dict[key]=request.form.get(key)
if key == "strData":
req_dict[key] = request.form.get(key)
else:
req_dict[key]=json.loads(request.form.get(key))
req_dict[key] = json.loads(request.form.get(key))
for fileKey in request.files:
"""
The bytes data needs to be base64 encode because the protobuf trys to do base64 decode for bytes
"""
if fileKey == 'binData':
req_dict[fileKey]=base64.b64encode(request.files[fileKey].read())
if fileKey == "binData":
req_dict[fileKey] = base64.b64encode(request.files[fileKey].read())
else:
"""
This is the case when strData can be passed as file as well
"""
req_dict[fileKey]=request.files[fileKey].read().decode('utf-8')
req_dict[fileKey] = request.files[fileKey].read().decode("utf-8")
return req_dict


def get_request() -> Dict:
"""
Parse a request to get JSON dict
Expand All @@ -45,14 +47,17 @@ def get_request() -> Dict:
"""

if request.content_type is not None and 'multipart/form-data' in request.content_type:
if (
request.content_type is not None
and "multipart/form-data" in request.content_type
):
return get_multi_form_data_request()

j_str = request.form.get("json")
if j_str:
message = json.loads(j_str)
else:
j_str = request.args.get('json')
j_str = request.args.get("json")
if j_str:
message = json.loads(j_str)
else:
Expand All @@ -67,7 +72,9 @@ def get_request() -> Dict:
class SeldonMicroserviceException(Exception):
status_code = 400

def __init__(self, message, status_code=None, payload=None, reason="MICROSERVICE_BAD_DATA"):
def __init__(
self, message, status_code=None, payload=None, reason="MICROSERVICE_BAD_DATA"
):
Exception.__init__(self)
self.message = message
if status_code is not None:
Expand All @@ -76,10 +83,16 @@ def __init__(self, message, status_code=None, payload=None, reason="MICROSERVICE
self.reason = reason

def to_dict(self):
rv = {"status": {"status": 1, "info": self.message,
"code": -1, "reason": self.reason}}
rv = {
"status": {
"status": 1,
"info": self.message,
"code": -1,
"reason": self.reason,
}
}
return rv


ANNOTATIONS_FILE = "/etc/podinfo/annotations"
ANNOTATION_GRPC_MAX_MSG_SIZE = 'seldon.io/grpc-max-message-size'
ANNOTATION_GRPC_MAX_MSG_SIZE = "seldon.io/grpc-max-message-size"
6 changes: 5 additions & 1 deletion python/seldon_core/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ def validate_metrics(metrics: List[Dict]) -> bool:
for metric in metrics:
if not ("key" in metric and "value" in metric and "type" in metric):
return False
if not (metric["type"] == COUNTER or metric["type"] == GAUGE or metric["type"] == TIMER):
if not (
metric["type"] == COUNTER
or metric["type"] == GAUGE
or metric["type"] == TIMER
):
return False
try:
metric["value"] + 1
Expand Down
Loading

0 comments on commit 6a85bdf

Please sign in to comment.