From eb7d9635b544805e9c253f92e7f9e458e5c7f5cd Mon Sep 17 00:00:00 2001 From: Kamila Date: Sun, 15 May 2022 12:41:28 +0200 Subject: [PATCH] mlflow attempt task 2 prediction --- Dockerfile | 1 + Jenkinsfile_pred | 41 +++++++++++++++++++++++++++++++++++++++++ predict.py | 18 ++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 Jenkinsfile_pred create mode 100644 predict.py diff --git a/Dockerfile b/Dockerfile index 4cefcff..c16b2e4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,6 +18,7 @@ RUN pip3 install sklearn RUN pip3 install pymongo RUN pip3 install sacred RUN pip3 install mlflow +RUN pip3 install tarfile CMD python3 data_expl.py CMD python3 nn_train.py \ No newline at end of file diff --git a/Jenkinsfile_pred b/Jenkinsfile_pred new file mode 100644 index 0000000..5df0726 --- /dev/null +++ b/Jenkinsfile_pred @@ -0,0 +1,41 @@ +pipeline { + agent { + dockerfile true + } + parameters { + buildSelector( + defaultSelector: lastSuccessful(), + description: 'Which build to use for copying artifacts', + name: 'BUILD_SELECTOR' + ) + string( + defaultValue: 'input_example.json', + description: 'Input file name', + name: 'INPUT_FILE_NAME', + trim: false + ) + } + + stages { + stage('Stage 1') { + steps { + echo 'Hello world!' + } + } + + stage('Copy from different Pipeline') { + steps { + copyArtifacts fingerprintArtifacts: false, projectName: 's449288-training/master', selector: buildParameter('BUILD_SELECTOR') + } + } + + stage('Prediction') { + steps { + sh 'python3 predict.py $INPUT_FILE_NAME' + sh 'rm -r ml' + + } + } + } +} + diff --git a/predict.py b/predict.py new file mode 100644 index 0000000..d80f183 --- /dev/null +++ b/predict.py @@ -0,0 +1,18 @@ +import json +import mlflow +import numpy as np +import sys +import tarfile + +file = tarfile.open('mlruns.tar.gz') +file.extractall('./ml') + +input = str((sys.argv[1:])[0]) +PATH = "ml/mlruns/1/f65f936936024133a2c03e1e486ba9cf/artifacts/model/" +model = mlflow.pytorch.load_model(f"{PATH}/MLmodel") + +with open(f'[PATH]/{input}', 'r') as file: + json_data = json.load(file) + + print(f"Input: {json_data['inputs'][0]}") + print(f"Prediction: {model.predict(np.array([json_data['inputs'][0]], dtype=np.float32))}") \ No newline at end of file