Shell Script to Export ML Model to Stakeholders
One challenge that we have particularly faced in our career is being able to efficiently send ML model results to business stakeholders. Although many companies/teams will have their internal tools or mechanisms to distribute ML model results to business stakeholders, many startups or new teams lack such mechanisms and need a way to bootstrap a process for immediate impact.
In this article, we will see how to export machine learning model results to stakeholders using a Shell script.
Stepwise implementation
Step 1: Setting up a shell script
Open terminal on your system and run the following
vi shell_script.sh
Then copy paste the below code. This code essentially reads all the records in your model output file row by row and sends an email to business stakeholders for every record in the model output file and then sends a collective report to all the emails to the email specified
- The first line #!/bin/bash which tells the terminal to use bash to execute the script
- Then the second line if [ -e $1 ] checks if the model output file exists
- If the model output file exists then every record in the file is read line by line and emailed. while IFS = read -r line; do reads records line by line.
- Once all the lines are read and emailed then $(cat $1 | xargs | sed -e ‘s/ /./g’) sends a notification to the email of all records sent to stakeholders.
- If the Model output file does not exist then there is a notification through email that there is no file/results are delayed
#!bin/bash
if [ -e $1 ]
then
while IFS = read -r line; do
echo “Hello, record $line has been generated from the ML model” | mailx -s “ML Model daily results” business@xyz.com
done < $1
echo “Cases sent by ML model $(cat $1 | xargs | sed -e ‘s/ /./g’)” | mailx -s “Results sent from ML model to business stakeholders” username@xyz.com
else
echo “ML model results have been delayed” | mailx -s “ML Model execution has been delayed” username@xyz.com
Save the above file by pressing esc followed by “:wq!” To save changes made to the file.
Step 2: Executing the above script
You can run the above shell script by specifying the model output file location and running the following command
sh shell_script.sh model_results/daily_run_01172022.csv
Step 3: Automating the script using a cronjob
Open the crontab on your machine by running the following command
crontab -e
Now, you can create a cadence at which you want to execute the shell script.
0 1 * * 0 – 5 sh shell_script.sh model_results/daily_run_01172022.csv
With the above command, we are scheduling the script to execute 6 days in a week at 1 am.
Output:
Contact Us