Package: train-and-deploy
TanD - Train and DeployTanD is a simple, no-code, flexible and customizable framework to automatize the Machine Learning workflow.With TanD you can go through the whole ML workflow without writing a single line of code (for bothsklearnandtorchbased models): by creating a project template and setting some configurations on a.jsonfile you are able to train a ML model of your choice, store it tomlflowto control its lifecycle and create a ready-to-deploy API to serve your it.Although TanD lets you run your workflows (from train to deploy) with no code at all, it is highly customizable, letting you introduce your chunks of code to enhance your modelling pipelines in anyway you want.Our mission is to let you avoid repetitive tasks so you can focus on what matters. TanD brings Machine-Learning laziness to a whole new level.RodamapThe project's roadmap (which is not defined in order of priority) is:Create project templates (torchandsklearn) for regression tasks in structured data;Create aDockerfilein project templates to ease deploymentOK;Create acronjob in Docker to update model parametersOK;Create tutorials for train and deploy withtand;Create project templates (torch/transformers) for classification tasks in text data;Create project templates (torch) for classification in image data;Createdocumentationfor the projectIndexInstallDocumentationQuick startInstallTo installtandyou can use pip command:pip install train-and-deployYou can also clone the repo andpip install .it locally:git clone https://github.com/piEsposito/TanD.git
cd TanD
pip install .DocumentationDocumentation fortand.utiland explanation of project templates:utilDocumentation fortand.deploymentwith utils to ease deployment to cloud:deploymentDocumentation for project templates:PyTorch structured data classification taskPyTorch structured data regression taskSklearn structured data regression taskQuick startAfter installingtandyou can train and deploy on a sample project using theUCI heart disease dataset. Notice that you can perform the process on your own datasets by only changing the.csvfile and setting some configurations. By following this steps, this tutorial will let you:Train atorchmodel with a dataset and log all of its metrics tomlflow;Automatically generate afastapibased API service for serving the model (which receives a json with the features);Deploy the model to AWS ElasticBeanstalk with two lines of code.To create the project with atorchbased model, on an empty folder, type:tand-create-project --template pytorch-structured-classificationThat will create all the needed files on the folder. We should first checkconfig.json:{"train":{"__help":"Configurations for the training of the project (of the model, etc...)","data_path":"data/data.csv","labels_column":"target","log_every":250,"epochs":50,"hidden_dim":256,"batch_size":32,"device":"cpu","labels":["no_heart_disease","heart_disease"],"to_drop":[]},"test":{"__help":"Configurations for the testing of this project (train + app)"},"app":{"__help":"Configurations for the service generated by this project","token":null},"mlflow":{"__help":"Configurations for the mlflow model manager of the project","model_name":"pytorch-classifier-nn-heart-disease","experiment_name":"heart-disease-experiment"}}The project is all set, but is important to check:If thedata_pathattribute oftrainis set properly;If thelabels_columnattribute oftrainis set according to the dataset label column;If thelabelsattribute oftrainis set in proper order with the names.We should also see themlflowpathes for both database and model logging. As we want to keep it simple, we will usesqliteand a local storage, but you can set it to remote buckets and database in a production environment. They are set in all theenv_filesfolder files as:MLFLOW_TRACKING_URI=sqlite:///database.db
MLFLOW_DEFAULT_ARTIFACT_ROOT=./mlruns/But feel free to change it, according tomlflowdocumentation.To train the model is as easy as running:source env_files/train.env
python train.pyAnd you can see lots of metrics for the model atmlflow:bash mlflow-server.shIf you are running anmlflowexperiment for first time on a project, it will be automatically set for production. If you rerun the experiment with a different dataset or parameters, you can set the production model atmlflowfollowint thedocumentation. That will be useful once you deploy it.That command also createsrequest_model.json, which is used both to validate request bodies for the API service and reordering it to comply with model. This file will be also used for unit-testing the API (this is also automatically generated).{"age":1,"sex":1,"cp":1,"trestbps":1,"chol":1,"fbs":1,"restecg":1,"thalach":1,"exang":1,"oldpeak":1,"slope":1,"ca":1,"thal":1}Moving to the API creationIf you want to generate value with a ML model, you should deploy it.tandhelps you with creating the API, aDockerfileand all the configurations files needed to deploy the model with no code. Notice that the API is protected with a token, which defaults toTOKEN123but you can change it onenv_files/app.envandenv_files/docker_app.env.The API contains simple authentication token, a/route for health checking,/update-modelfor model uptading (POST at it with proper credentials and it fetches the latest production model atmlflow), and/predict, which grabs the features from the request body and returns the prediction.To test the API, just run:source env_files/app.env
pytestIt should pass everything.You now have some options for deployment. You can, using some arbitrary VM, run the app, build and run the image generated byDockerfileor usetandfeatures designed for AWS ElasticBeanstalk, which lets your model to be deployed cheap and scalable. We will cover both:To run the app, just type:uvicorn app:app --reload --host 0.0.0.0 --port 8000You can test it with:curl --header "Content-Type: application/json" \
  --header "TOKEN: $API_TOKEN" \
  --request POST \
  --data '{"age":1,"sex":1,"cp":1,"trestbps":1,"chol":1,"fbs":1,"restecg":1,"thalach":1,"exang":1,"oldpeak":1,"slope":1,"ca":1,"thal":1}' \
  http://localhost:8000/predictRemember tosource env_files/app.envbefore performing the request or else it will return status 401 Unauthorized.You can build the Docker image with:docker build . -t tand-app:v1And run it with:docker run -p 8000:8000 --env-file env_files/docker_app.env tand-app:v1You can test it with:curl --header "Content-Type: application/json" \
  --header "TOKEN: $API_TOKEN" \
  --request POST \
  --data '{"age":1,"sex":1,"cp":1,"trestbps":1,"chol":1,"fbs":1,"restecg":1,"thalach":1,"exang":1,"oldpeak":1,"slope":1,"ca":1,"thal":1}' \
  http://localhost:8000/predictRemember tosource env_files/app.envbefore performing the request or else it will return status 401 Unauthorized.Last, we can deploy it to AWS ElasticBeanstalk. To do that, first youshould set your AWS credentialsat your machine and theninstallebCLI. That should be done at your root environment, notcondaorvirtualenv.You can generate the configurations with:tand-prepare-aws-eb-deployment --init-gitWe pass this--init-gitflag becauseebCLI uses some files from.gitrepository to upload the files to be deployed.That will generatedeploy-aws-eb.sh, which will be run for deployment. It will also generate.ebextensionscontaining:cron.config- which runs, on each instance, a daily task to update the instance ML model by fetching the last production one frommlflow(which is properly used when we set cloud-basedmlflowbackend);options.config- which sets the API token andmlflowbackend env variables for the deployment; andscaling.config- which sets the scalability configurations for the deployment, including the maximum and minimum number of replicas and criteria for scaling (defaults to latency)To finally deploy it to AWS, run:bash deploy-aws.eb.shIt takes about 5 minutes, after what you caneb opento get the link and then try it withcurl --header "Content-Type: application/json" \
  --header "TOKEN: $API_TOKEN" \
  --request POST \
  --data '{"age":1,"sex":1,"cp":1,"trestbps":1,"chol":1,"fbs":1,"restecg":1,"thalach":1,"exang":1,"oldpeak":1,"slope":1,"ca":1,"thal":1}' \
  http://YOUR_LINK_GOES_HERE/predictRemember to properly set the token for testing.And with that, we showed how can we train and deploy a model withtandwith a couple terminal commands and no coding at all.Made by Pi Esposito