{"id":14958380,"url":"https://github.com/piesposito/tand","last_synced_at":"2025-10-24T14:31:55.768Z","repository":{"id":49207499,"uuid":"288190689","full_name":"piEsposito/TanD","owner":"piEsposito","description":"TanD - Train and Deploy is a no-code framework to automatize the Machine Learning workflow.","archived":false,"fork":false,"pushed_at":"2021-06-23T15:31:20.000Z","size":111,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-29T23:33:33.026Z","etag":null,"topics":["data-science","fastapi","machine-learning","mlflow","pytorch","sklearn","workflow-automation"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/piEsposito.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-17T13:41:25.000Z","updated_at":"2024-02-25T15:14:32.000Z","dependencies_parsed_at":"2022-09-09T06:50:48.145Z","dependency_job_id":null,"html_url":"https://github.com/piEsposito/TanD","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piEsposito%2FTanD","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piEsposito%2FTanD/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piEsposito%2FTanD/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piEsposito%2FTanD/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/piEsposito","download_url":"https://codeload.github.com/piEsposito/TanD/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237990581,"owners_count":19398453,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-science","fastapi","machine-learning","mlflow","pytorch","sklearn","workflow-automation"],"created_at":"2024-09-24T13:16:54.540Z","updated_at":"2025-10-24T14:31:51.744Z","avatar_url":"https://github.com/piEsposito.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TanD - Train and Deploy\n\nTanD is a simple, no-code, flexible and customizable framework to automatize the Machine Learning workflow. \n\nWith TanD you can go through the whole ML workflow without writing a single line of code (for both `sklearn` and `torch` based models): by creating a project template and setting some configurations on a `.json` file you are able to train a ML model of your choice, store it to `mlflow` to control its lifecycle and create a ready-to-deploy API to serve your it.\n\nAlthough 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.\n\nOur 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.\n\n## Rodamap \nThe project's roadmap (which is not defined in order of priority) is:\n * Create project templates (`torch` and `sklearn`) for regression tasks in structured data;\n * ~Create a `Dockerfile` in project templates to ease deployment~ OK;\n * ~Create a `cron` job in Docker to update model parameters~ OK;\n * Create tutorials for train and deploy with `tand`;\n * Create project templates (`torch` / `transformers`) for classification tasks in text data;\n * Create project templates (`torch`) for classification in image data;\n * Create `documentation` for the project\n \n # Index\n * [Install](#Install)\n * [Documentation](#Documentation)\n * [Quick start](#Quick-start)\n \n ## Install\n\nTo install `tand` you can use pip command:\n\n```\npip install train-and-deploy\n```\n\nYou can also clone the repo and `pip install .` it locally:\n\n```\ngit clone https://github.com/piEsposito/TanD.git\ncd TanD\npip install .\n```\n\n ## Documentation\nDocumentation for `tand.util` and explanation of project templates:\n * [util](doc/util.md)\n \nDocumentation for `tand.deployment` with utils to ease deployment to cloud:\n * [deployment](doc/deployment.md)\n \nDocumentation for project templates:\n * [PyTorch structured data classification task](doc/pytorch-structured-classification.md)\n * [PyTorch structured data regression task](doc/pytorch-structured-regression.md)\n * [Sklearn structured data regression task](doc/sklearn-structured-classification.md)\n\n \n---\n\n## Quick start\n\nAfter installing `tand` you can train and deploy on a sample project using the [UCI heart disease dataset](https://www.kaggle.com/ronitf/heart-disease-uci). Notice that you can perform the process on your own datasets by only changing the `.csv` file and setting some configurations. By following this steps, this tutorial will let you:\n\n * Train a `torch` model with a dataset and log all of its metrics to `mlflow`;\n * Automatically generate a `fastapi` based API service for serving the model (which receives a json with the features);\n * Deploy the model to AWS ElasticBeanstalk with two lines of code.\n \n\nTo create the project with a `torch` based model, on an empty folder, type:\n\n```\ntand-create-project --template pytorch-structured-classification\n```\n\nThat will create all the needed files on the folder. We should first check `config.json`:\n\n```json\n{\n  \"train\": {\n    \"__help\": \"Configurations for the training of the project (of the model, etc...)\",\n    \"data_path\": \"data/data.csv\",\n    \"labels_column\": \"target\",\n\n    \"log_every\": 250,\n    \"epochs\": 50,\n\n    \"hidden_dim\": 256,\n    \"batch_size\": 32,\n    \"device\": \"cpu\",\n    \"labels\": [\"no_heart_disease\", \"heart_disease\"],\n    \"to_drop\": []\n\n  },\n\n  \"test\": {\n    \"__help\": \"Configurations for the testing of this project (train + app)\"\n  },\n\n  \"app\": {\n    \"__help\": \"Configurations for the service generated by this project\",\n    \"token\": null\n  },\n\n  \"mlflow\": {\n    \"__help\": \"Configurations for the mlflow model manager of the project\",\n    \"model_name\": \"pytorch-classifier-nn-heart-disease\",\n    \"experiment_name\": \"heart-disease-experiment\"\n  }\n}\n```\n\nThe project is all set, but is important to check:\n * If the `data_path` attribute of `train` is set properly;\n * If the `labels_column` attribute of `train` is set according to the dataset label column;\n * If the `labels` attribute of `train` is set in proper order with the names.\n\nWe should also see the `mlflow` pathes for both database and model logging. As we want to keep it simple, we will use `sqlite` and a local storage, but you can set it to remote buckets and database in a production environment. They are set in all the `env_files` folder files as:\n\n```\nMLFLOW_TRACKING_URI=sqlite:///database.db\nMLFLOW_DEFAULT_ARTIFACT_ROOT=./mlruns/\n```\n\nBut feel free to change it, according to [`mlflow` documentation](https://www.mlflow.org/docs/latest/tracking.html).\n\nTo train the model is as easy as running:\n\n```\nsource env_files/train.env\npython train.py\n```\n\nAnd you can see lots of metrics for the model at `mlflow`:\n\n```\nbash mlflow-server.sh\n```\n\nIf you are running an `mlflow` experiment 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 at `mlflow` followint the [documentation](https://www.mlflow.org/docs/latest/model-registry.html). That will be useful once you deploy it.\n\nThat command also creates `request_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).\n\n```json\n{\n    \"age\": 1,\n    \"sex\": 1,\n    \"cp\": 1,\n    \"trestbps\": 1,\n    \"chol\": 1,\n    \"fbs\": 1,\n    \"restecg\": 1,\n    \"thalach\": 1,\n    \"exang\": 1,\n    \"oldpeak\": 1,\n    \"slope\": 1,\n    \"ca\": 1,\n    \"thal\": 1\n}\n```\n\n### Moving to the API creation\n\nIf you want to generate value with a ML model, you should deploy it. `tand` helps you with creating the API, a `Dockerfile` and all the configurations files needed to deploy the model with no code. Notice that the API is protected with a token, which defaults to `TOKEN123` but you can change it on `env_files/app.env` and `env_files/docker_app.env`.\n\nThe API contains simple authentication token, a `/` route for health checking, `/update-model` for model uptading (POST at it with proper credentials and it fetches the latest production model at `mlflow`), and `/predict`, which grabs the features from the request body and returns the prediction.\n\nTo test the API, just run:\n\n```\nsource env_files/app.env\npytest\n```\n\nIt should pass everything.\n\nYou now have some options for deployment. You can, using some arbitrary VM, run the app, build and run the image generated by `Dockerfile` or use `tand` features designed for AWS ElasticBeanstalk, which lets your model to be deployed cheap and scalable. We will cover both:\n\nTo run the app, just type:\n\n```\nuvicorn app:app --reload --host 0.0.0.0 --port 8000\n```\n\nYou can test it with:\n\n```\ncurl --header \"Content-Type: application/json\" \\\n  --header \"TOKEN: $API_TOKEN\" \\\n  --request POST \\\n  --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}' \\\n  http://localhost:8000/predict\n```\n\nRemember to `source env_files/app.env` before performing the request or else it will return status 401 Unauthorized.\n\nYou can build the Docker image with:\n\n```\ndocker build . -t tand-app:v1\n```\n\nAnd run it with:\n\n```\ndocker run -p 8000:8000 --env-file env_files/docker_app.env tand-app:v1\n```\n\nYou can test it with:\n\n```\ncurl --header \"Content-Type: application/json\" \\\n  --header \"TOKEN: $API_TOKEN\" \\\n  --request POST \\\n  --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}' \\\n  http://localhost:8000/predict\n```\n\nRemember to `source env_files/app.env` before performing the request or else it will return status 401 Unauthorized.\n\nLast, we can deploy it to AWS ElasticBeanstalk. To do that, first you [should set your AWS credentials](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html) at your machine and then [install `eb` CLI](https://docs.amazonaws.cn/en_us/elasticbeanstalk/latest/dg/eb-cli3-install-linux.html). That should be done at your root environment, not `conda` or `virtualenv`.\n\nYou can generate the configurations with:\n\n```\ntand-prepare-aws-eb-deployment --init-git\n```\n\nWe pass this `--init-git` flag because `eb` CLI uses some files from `.git` repository to upload the files to be deployed.\n\nThat will generate `deploy-aws-eb.sh`, which will be run for deployment. It will also generate `.ebextensions` containing:\n * `cron.config` - which runs, on each instance, a daily task to update the instance ML model by fetching the last production one from `mlflow` (which is properly used when we set cloud-based `mlflow` backend);\n * `options.config` - which sets the API token and `mlflow` backend env variables for the deployment; and \n * `scaling.config` - which sets the scalability configurations for the deployment, including the maximum and minimum number of replicas and criteria for scaling (defaults to latency)\n \nTo finally deploy it to AWS, run:\n\n```\nbash deploy-aws.eb.sh\n```\n\nIt takes about 5 minutes, after what you can `eb open` to get the link and then try it with\n\n```\ncurl --header \"Content-Type: application/json\" \\\n  --header \"TOKEN: $API_TOKEN\" \\\n  --request POST \\\n  --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}' \\\n  http://YOUR_LINK_GOES_HERE/predict\n```\n\nRemember to properly set the token for testing.\n\nAnd with that, we showed how can we train and deploy a model with `tand` with a couple terminal commands and no coding at all.\n\n---\n\n###### Made by Pi Esposito\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiesposito%2Ftand","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiesposito%2Ftand","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiesposito%2Ftand/lists"}