async def run(request):
    """This function is called every time your webservice receives a request.

    Notice you need to know the names and data types of the model inputs and
    outputs. You can get these values by reading the model configuration file
    or by querying the model metadata endpoint.
    """

    if request.method == "POST":
        outputs = []

        for output in session.get_outputs():
            outputs.append(output.name)

        input_name = session.get_inputs()[0].name

        reqBody = await request.get_data()
        img = Image.open(io.BytesIO(reqBody))
        image_data = preprocess(img, scaling="INCEPTION")

        res = session.run(outputs, {input_name: image_data})

        result = postprocess(output_array=res)

        return AMLResponse(result, 200)
    else:
        return AMLResponse("bad request", 500)
Beispiel #2
0
def run(request):
    """
    Perform inference on a single image, using the model.

    :param data:    Binary representation of the image
    :returns:       AMLResponse
    """

    if request.method == 'POST':
        data = request.get_data(False)

        # Open file and write binary data to file:
        Path('tmp').mkdir(parents=True, exist_ok=True)
        fname = 'tmp/' + \
            ''.join(random.choice(string.ascii_lowercase) for i in range(32))

        with open(fname, 'wb') as f:
            f.write(data)

        result = model.predict(fname)

        os.remove(fname)

        return AMLResponse(result, 200)
    else:
        return AMLResponse("Method not allowed", 405)
Beispiel #3
0
def run(request):
    if request.method == 'POST':
        reqBody = request.get_data(False)
        resp = score(reqBody)
        return AMLResponse(resp, 200)
    if request.method == 'GET':
        respBody = str.encode("GET is not supported")
        return AMLResponse(respBody, 405)
    return AMLResponse("bad request", 500)
Beispiel #4
0
def default_response(request) -> AMLResponse:
    """

    :param request:
    :return:
    """
    if request.method == "GET":
        return AMLResponse({"azEnvironment": "Azure"}, 201)
    return AMLResponse("bad request", 500)
Beispiel #5
0
def predictions(request):
    raw_images = []
    images = request.files.getlist("images")
    image_names = []
    for image in images:
        image_name = image.filename
        image_names.append(image_name)
        image.save(os.path.join(os.getcwd(), image_name))
        img_raw = tf.image.decode_image(open(image_name, 'rb').read(),
                                        channels=3)
        raw_images.append(img_raw)

    num = 0

    # create list for final response
    response = []

    for j in range(len(raw_images)):
        # create list of responses for current image
        responses = []
        raw_img = raw_images[j]
        num += 1
        img = tf.expand_dims(raw_img, 0)
        img = transform_images(img, size)

        t1 = time.time()
        boxes, scores, classes, nums = yolo(img)
        t2 = time.time()
        print('time: {}'.format(t2 - t1))

        print('detections:')
        for i in range(nums[0]):
            print('\t{}, {}, {}'.format(class_names[int(classes[0][i])],
                                        np.array(scores[0][i]),
                                        np.array(boxes[0][i])))
            responses.append({
                "class":
                class_names[int(classes[0][i])],
                "confidence":
                float("{0:.2f}".format(np.array(scores[0][i]) * 100))
            })
        response.append({"image": image_names[j], "detections": responses})
        img = cv2.cvtColor(raw_img.numpy(), cv2.COLOR_RGB2BGR)
        img = draw_outputs(img, (boxes, scores, classes, nums), class_names)
        cv2.imwrite(output_path + 'detection' + str(num) + '.jpg', img)
        print('output saved to: {}'.format(output_path + 'detection' +
                                           str(num) + '.jpg'))

    #remove temporary images
    for name in image_names:
        os.remove(name)
    try:
        return AMLResponse(json.dumps(response), 200)
        #return jsonify({"response":response}), 200
    except FileNotFoundError:
        return AMLResponse("bad request", 500)
Beispiel #6
0
def run(request):
    print("Processing request...")
    print(f"POST Request: {request}")
    df = pd.DataFrame(request)
    image = get_base64image_from_request(df)
    if image:
        prediction = get_prediction(image, session, signature)
        return AMLResponse(json.dumps([prediction]), 200)
    else:
        return AMLResponse("bad request", 400)
Beispiel #7
0
def run(request):
    if request.method == 'POST':
        reqBody = request.get_data(False)
        resp = score(reqBody)

        headers = {'Content-Type': 'image/png'}

        return AMLResponse(resp, 200, headers)
    if request.method == 'GET':
        respBody = str.encode("GET is not supported")
        return AMLResponse(respBody, 405)
    return AMLResponse("bad request", 500)
Beispiel #8
0
def run(request):
    global testType
    global testType2
    try:
        if (request.method == 'POST'):
            #file_bytes = request.files['image']
            file_bytes = request.data

            my_image = Image.open(BytesIO(file_bytes)).convert('RGB')
            testType = type(my_image)
            #image = (Image.open(file_bytes.stream))
            #image.save('api_call_image\testphoto.png', 'PNG')
            #my_image = load_img('api_call_image\testphoto.png', target_size=(224, 224))
            my_image = my_image.resize((224, 224))

            my_image = my_image.convert('L')
            testType2 = type(my_image)
            my_image = image.img_to_array(my_image)
            my_image /= 255
            my_image = np.expand_dims(my_image, axis=0)
            prediction = model.predict(my_image)[0][0]  #

            confidence = abs(.5 - prediction) * 2
            if (prediction > .5):
                answer = "positive"
            else:
                answer = "negative"

            #print("prediction is ", prediction)
            #print("confidence is ", confidence)
            #print("diagnosis is ", answer)
            dict1 = {
                "diagnosis": str(answer),
                "confidence": str(confidence),
                "probability": str(prediction)
            }

            return AMLResponse(json.dumps(dict1), 200)

        else:
            return AMLResponse(json.dumps("bad request"), 500)

    except Exception as e:
        dict2 = {
            "exception": "error is",
            "error": str(e),
            "tb": traceback.format_exc(),
            "type": str(testType),
            "type2": str(testType2)
        }

        return AMLResponse(json.dumps(dict2), 200)
Beispiel #9
0
def run(request):
    print("This is run()")
    print("Request: [{0}]".format(request))
    if request.method == 'GET':
        respBody = str.encode(request.full_path)
        return AMLResponse(respBody, 200)
    elif request.method == 'POST':
        data = request.get_data(False)
        print("Data Length:[{}]".format(len(data)))
        labels = label_image(data)
        print("Labels:[{}]".format(json.dumps(labels)))
        respBody = json.dumps(labels)
        return AMLResponse(respBody, 200, json_str=True)
    else:
        return AMLResponse("bad request", 500)
Beispiel #10
0
def run(request):
    if request.method == 'POST':
        try:
            data = np.expand_dims(np.array(request.json['data']), axis=0)
            yres = model.predict(data)
            response = {
                'data': yres.tolist(),
                'message': "Successfully  classified"
            }
            return AMLResponse(json.dumps(response), 200)
        except Exception as e:
            print(e)
            return AMLResponse("Server error", 500)
    else:
        return AMLResponse("Unsopported method", 405)
Beispiel #11
0
def unhandled_exception(error):
    main.stop_hooks()
    main.logger.debug("Unhandled exception generated")
    error_message = "Encountered Exception: {0}".format(traceback.format_exc())
    main.logger.error(error_message)
    internal_error = "An unexpected internal error occurred. {0}".format(error_message)
    return AMLResponse(internal_error, 500, json_str=False)
def run(request):
    if request.method == 'GET':
        respBody = str.encode(request.full_path)
        return AMLResponse(respBody, 200)
    elif request.method == 'POST':
        try:
            reqBody = request.get_data(False)
            raw_data = reqBody.decode("utf-8")
            data = json.loads(raw_data)['data']
            data = numpy.array(data)
            result = model.predict(data)
            result_string = json.dumps(result.tolist())
            return AMLResponse(result_string, 200)
        except Exception as e:
            error = str(e)
            return AMLResponse(error, 500)
    else:
        return AMLResponse("bad request", 500)
Beispiel #13
0
def handle_exception(error):
    main.logger.debug("Run function timeout caught")
    main.stop_hooks()
    main.logger.error("Encountered Exception: {0}".format(
        traceback.format_exc()))
    return AMLResponse(error.message,
                       error.status_code,
                       json_str=False,
                       run_function_failed=True)
Beispiel #14
0
def run(request):
    """
    Perform inference on a single image, using the model.

    :param data:    Binary representation of the image
    :returns:       AMLResponse
    """
    # data is a binary representation of an image.
    #
    # TODO: If necessary, manipulate data into the correct form. Examples are:
    # - Open file and write binary data to file:
    #   ```
    #   Path('tmp').mkdir(parents=True, exist_ok=True)
    #   fname = 'tmp/' + \
    #       ''.join(random.choice(string.ascii_lowercase) for i in range(32))
    #   with open(fname, 'wb') as f:
    #       f.write(data)
    #   ```
    # - Create a OpenCV image in memory:
    #   ```
    #   import numpy as np
    #   import cv2
    #
    #   nparr = np.frombuffer(data, np.uint8)
    #   original_image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    #   ```
    # - Create a PIL image in memory:
    #   ```
    #   from PIL import Image
    #   import io
    #
    #   image = Image.open(io.BytesIO(data))
    if request.method == 'POST':
        data = request.get_data(False)

        # TODO: Optionally, do something with the data here to create the
        # correct form of image

        # TODO implement actual model prediction
        result = model.predict(data)
        return AMLResponse(result, 200)
    else:
        return AMLResponse("Method not allowed", 405)
Beispiel #15
0
def run(request):
    """
    Perform inference on a single image, using the model.

    :param data:    Binary representation of the image
    :returns:       AMLResponse
    """

    if request.method == 'POST':
        data = request.get_data(False)
        if len(data) == 0:
            return AMLResponse(
                "No data received - please provide an image as body", 400)

        nparr = np.frombuffer(data, np.uint8)
        original_image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        image_data = image_preprocess(np.copy(original_image), [input_size, input_size])
        image_data = image_data[np.newaxis, ...].astype(np.float32)
        pred_bbox = yolo.predict(image_data)
        pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox]
        pred_bbox = tf.concat(pred_bbox, axis=0)

        bboxes = postprocess_boxes(pred_bbox, original_image, input_size, score_threshold)
        bboxes = nms(bboxes, iou_threshold, method='nms')

        print(bboxes)

        result = []
        for box in bboxes:
            result.append({
                'xmin': int(box[0]),
                'xmax': int(box[2]),
                'ymin': int(box[1]),
                'ymax': int(box[3]),
                'score': float(box[4]),
                'label': labels[int(box[5])]
            })

        return AMLResponse(json.dumps(result), 200)
    else:
        return AMLResponse("Method not allowed", 405)
def run(request):
    if request.method == 'GET':
        try:
            data = validate_input_args(request.args)
            array_data = np.expand_dims(np.array(data), axis=0)
            yres = model.predict(array_data)
            response = {
                'data': yres.tolist(),
                'message': "Successfully  classified"
            }
            return AMLResponse(json.dumps(response), 200)
        except ValueError as e:
            print(e)
            error_json = {'error': 'missing_parameters', 'detail': e.args}
            return AMLResponse(json.dumps(error_json), 404)
        except Exception as e:
            print(e)
            return AMLResponse("Server error", 500)
    else:
        return AMLResponse("Unsopported method", 405)
Beispiel #17
0
def run(request):
    """This function is called every time your webservice receives a request.

    Notice you need to know the names and data types of the model inputs and
    outputs. You can get these values by reading the model configuration file
    or by querying the model metadata endpoint.
    """

    if request.method == "POST":
        model_name = "densenet_onnx"
        input_meta, input_config, output_meta, output_config = parse_model_http(
            model_name=model_name)

        input_name = input_meta[0]["name"]
        input_dtype = input_meta[0]["datatype"]
        output_name = output_meta[0]["name"]

        reqBody = request.get_data(False)
        img = Image.open(io.BytesIO(reqBody))
        image_data = preprocess(img, scaling="INCEPTION", dtype=input_dtype)

        mapping = {input_name: image_data}

        res = triton_infer(
            model_name=model_name,
            input_mapping=mapping,
            binary_data=True,
            binary_output=True,
            class_count=1,
        )

        result = postprocess(results=res,
                             output_name=output_name,
                             batch_size=1,
                             batching=False)

        return AMLResponse(result, 200)
    else:
        return AMLResponse("bad request", 500)
Beispiel #18
0
def run(request):
    print("Request: [{0}]".format(request))
    
    
    if request.method == 'POST':
        file = request.files['file']
#         file.save('temp.wav')
#         reqBody = request.get_data(False)
#         # reqBody to sample_audio
#         sample_audio = reqBody
        
        features = extract_features(file, bands=n_bands, frames=n_frames)
        y_prob = cnn.predict(features, verbose=0)
        y_pred = np.argmax(y_prob, axis=-1)
        defect_code = {2: "Pass", 3: "Noise", 4: "Rpm", 5: "Vibration"}
        # For a real-world solution, you would load the data from reqBody
        # and send it to the model. Then return the response.
#         os.remove('temp.wav')

        # For demonstration purposes, this example just returns the posted data as the response.
        return AMLResponse(defect_code[int(y_pred)], 200)
    else:
        return AMLResponse("bad request", 500)
Beispiel #19
0
def score_realtime():
    flask.g.apiName = "/score"

    # always enforce content-type json as either the sdk or the user code is expected to json deserialize this
    main.logger.info("Validation Request Content-Type")
    if 'Content-Type' not in request.headers or parse_options_header(
            request.headers['Content-Type'])[0] != 'application/json':
        return AMLResponse(
            {"message": "Expects Content-Type to be application/json"}, 415)

    # always expects the response to be utf-8 encodeable
    service_input = request.data.decode("utf-8")

    # run the user-provided run function
    return run_scoring(service_input, request.headers)
Beispiel #20
0
def run(request):
    if request.method == 'POST':
        body = request.json

        activity = Activity().deserialize(body)
        auth_header = request.headers[
            'Authorization'] if 'Authorization' in request.headers else ''

        async def aux_func(turn_context):
            LOOP.create_task(asyncio.wait([BOT.on_turn(turn_context)]))

        try:
            task = LOOP.create_task(
                ADAPTER.process_activity(activity, auth_header, aux_func))
            LOOP.run_until_complete(task)
            return AMLResponse("", 201)
        except Exception as message_error:
            return AMLResponse(message_error, 417)

    if request.method == 'GET':
        respBody = str.encode("GET is supported")
        return AMLResponse(respBody, 201)

    return AMLResponse("bad request", 500)
Beispiel #21
0
def run_scoring(service_input, request_headers):
    main.logger.info("Headers passed in (total {0}):".format(
        len(request_headers)))
    for k, v in request_headers.items():
        main.logger.info("\t{0}: {1}".format(k, v))

    main.start_hooks(
        request.environ.get('REQUEST_ID',
                            '00000000-0000-0000-0000-000000000000'))

    try:
        response = invoke_user_with_timer(service_input, request_headers)
    except ClientSideException:
        raise
    except ServerSideException:
        raise
    except TimeoutException:
        main.stop_hooks()
        main.send_exception_to_app_insights(request)
        raise
    except Exception as exc:
        main.stop_hooks()
        main.send_exception_to_app_insights(request)
        raise RunFunctionException(str(exc))
    finally:
        main.stop_hooks()

    response_headers = {}
    response_body = response
    response_status_code = 200

    if isinstance(response, dict):
        if 'aml_response_headers' in response:
            main.logger.info(
                "aml_response_headers are available from run() output")
            response_body = None
            response_headers = response['aml_response_headers']

        if 'aml_response_body' in response:
            main.logger.info(
                "aml_response_body is available from run() output")
            response_body = response['aml_response_body']

    return AMLResponse(response_body,
                       response_status_code,
                       response_headers,
                       json_str=True)
Beispiel #22
0
def run(request):
    """ Make a prediction based on the data passed in using the preloaded model
    """
    if request.method == 'POST':
        return process_and_score(request.files)
    if request.method == 'GET':
        resp_body = {
            "azEnvironment": "Azure",
            "location": "westus2",
            "osType": "Ubuntu 16.04",
            "resourceGroupName": "",
            "resourceId": "",
            "sku": "",
            "subscriptionId": "",
            "uniqueId": "PythonMLRST",
            "vmSize": "",
            "zone": "",
            "isServer": False,
            "version": ""
        }
        return resp_body
    return AMLResponse("bad request", 500)
Beispiel #23
0
def handle_exception(error):
    main.logger.debug("Server side exception caught")
    main.stop_hooks()
    main.logger.error("Encountered Exception: {0}".format(
        traceback.format_exc()))
    return AMLResponse(error.to_dict(), error.status_code)
Beispiel #24
0
def get_swagger_specification():
    flask.g.apiName = "/swagger.json"
    if main.swagger:
        return AMLResponse(main.swagger, 200, json_str=True)
    main.logger.info("Swagger file not present")
    return AMLResponse("Swagger not found", 404, json_str=True)
Beispiel #25
0
def handle_exception(error):
    main.logger.debug("Client request exception caught")
    main.stop_hooks()
    main.logger.error("Encountered Exception: {0}".format(
        traceback.format_exc()))
    return AMLResponse(error.to_dict(), error.status_code, json_str=True)