# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: cub_data_science
#     language: python
#     name: cub_data_science
# ---

# # Test tf serving

import json
import requests
import numpy as np
import tensorflow as tf
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix


# ## Run in terminal docker image

# docker run -p 8501:8501 --mount type=bind,source=/Users/rick/Desktop/Boulder/Spring2022/data_science/workspace/CUB-Data-Science-Team-2022/ml_dev_tutorial/Model/Serving/tf_model,target=/models/bankmodel/ -e MODEL_NAME=bankmodel -t tensorflow/serving 

# ## Load data

# +
# Decoding function
def parse_record(record):
    name_to_features = {
        'features': tf.io.FixedLenFeature([95], tf.float32),
        'label': tf.io.FixedLenFeature([], tf.int64),
    }
    return tf.io.parse_single_example(record, name_to_features)

def decode_record(record):
    features = record['features']
    target = record['label']
    return (features,target)

def prepData(record):
    record = parse_record(record)
    X,y = decode_record(record)
    return X, y


# -

data_path = "/Users/rick/Desktop/Boulder/Spring2022/data_science/workspace/CUB-Data-Science-Team-2022/ml_dev_tutorial/Data"

# +
test_file_name = data_path+"/tfRecord/test.tfrecord"
test_dataset = tf.data.TFRecordDataset(test_file_name)
test_dataset = test_dataset.map(prepData, num_parallel_calls=tf.data.AUTOTUNE)

test_dataset = test_dataset.cache()
test_dataset = test_dataset.shuffle(10000)
test_dataset = test_dataset.batch(3000)
test_dataset = test_dataset.prefetch(tf.data.AUTOTUNE)
# -

iterator = iter(test_dataset)
next_element = iterator.get_next()
X_test, y_test = next_element
print(X_test.shape)
print(y_test.shape)

# ## Call server on test data

#Create JSON Object
data = json.dumps({"signature_name": "serving_default", "instances": X_test.numpy().tolist()})

headers = {"content-type": "application/json"}
json_response = requests.post('http://localhost:8501/v1/models/bankmodel:predict', data=data, headers=headers)
predictions = json.loads(json_response.text)['predictions']

pred_round = [round(pred) for pred in sum(predictions, [])]

target_names = ['no fraud','fraud']
print(classification_report(y_test.numpy().tolist(), pred_round,target_names=target_names))









StarcoderdataJupyterScriptsDedupFiltered