{"id":19986518,"url":"https://github.com/bentoml/fraud-detection-model-serving","last_synced_at":"2025-08-07T02:18:55.127Z","repository":{"id":173597420,"uuid":"632285342","full_name":"bentoml/Fraud-Detection-Model-Serving","owner":"bentoml","description":"Online model serving with Fraud Detection model trained with XGBoost on IEEE-CIS dataset","archived":false,"fork":false,"pushed_at":"2023-06-26T19:51:41.000Z","size":48,"stargazers_count":18,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-08T05:41:23.298Z","etag":null,"topics":["ai-applications","fraud-detection","model-deployment","model-serving"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bentoml.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-25T05:15:14.000Z","updated_at":"2025-01-03T13:33:01.000Z","dependencies_parsed_at":null,"dependency_job_id":"f13a47c3-6681-490f-9cf6-f82ed891ee53","html_url":"https://github.com/bentoml/Fraud-Detection-Model-Serving","commit_stats":null,"previous_names":["bentoml/fraud-detection-model-serving"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bentoml%2FFraud-Detection-Model-Serving","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bentoml%2FFraud-Detection-Model-Serving/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bentoml%2FFraud-Detection-Model-Serving/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bentoml%2FFraud-Detection-Model-Serving/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bentoml","download_url":"https://codeload.github.com/bentoml/Fraud-Detection-Model-Serving/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252304727,"owners_count":21726610,"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":["ai-applications","fraud-detection","model-deployment","model-serving"],"created_at":"2024-11-13T04:29:28.553Z","updated_at":"2025-05-04T07:31:28.219Z","avatar_url":"https://github.com/bentoml.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n    \u003ch1 align=\"center\"\u003eFraud Detection Models Serving\u003c/h1\u003e\n    \u003cbr\u003e\n    \u003cstrong\u003eOnline model serving with Fraud Detection model trained with XGBoost on IEEE-CIS dataset\u003cbr\u003e\u003c/strong\u003e\n    \u003ci\u003ePowered by BentoML 🍱\u003c/i\u003e\n    \u003cbr\u003e\n\u003c/div\u003e\n\u003cbr\u003e\n\n## 📖 Introduction 📖\nThis project demonstrates how to serve a fraud detection model trained with [XGBoost]() on the \ndataset from the [IEEE-CIS Fraud Detection competition](https://www.kaggle.com/competitions/ieee-fraud-detection/data).\n\n\n## 🏃‍♂️ Getting Started 🏃‍♂️\n\n### 0. Clone the Repository:\n\n```bash\ngit clone git@github.com:bentoml/Fraud-Detection-Model-Serving.git\ncd Fraud-Detection-Model-Serving\n```\n\n### 1. Install Dependencies:\n```bash\npip install -r ./dev-requirements.txt\n```\n\n### 2. Download dataset\n\nBefore downloading, set up your Kaggle API Credentials following instructions \n[here](https://github.com/Kaggle/kaggle-api#api-credentials) and accept the [dataset \nrules on Kaggle](https://www.kaggle.com/competitions/ieee-fraud-detection/data)\n\n```bash\n./download_data.sh\n```\n\n### 3. Model Training\n\nExecute the `./IEEE-CIS-Fraud-Detection.ipynb` notebook with the `train.sh` script:\n```bash\n./train.sh\n```\n\nThis will create 3 variations of the model, you can view and manage those models via the \n`bentoml models`  CLI commnad:\n\n```bash\n$ bentoml models list\n\nTag                                         Module           Size        Creation Time\nieee-fraud-detection-tiny:qli6n3f6jcta3uqj  bentoml.xgboost  141.40 KiB  2023-03-08 23:03:36\nieee-fraud-detection-lg:o7wqb5f6jcta3uqj    bentoml.xgboost  18.07 MiB   2023-03-08 23:03:17\nieee-fraud-detection-sm:5yblgmf6i2ta3uqj    bentoml.xgboost  723.00 KiB  2023-03-08 22:52:16\n```\n\nSaved models can also be accessed via the BentoML Python API, e.g.:\n\n```python\nimport bentoml\nimport pandas as pd\nimport numpy as np\n\nmodel_ref = bentoml.xgboost.get(\"ieee-fraud-detection-sm:latest\")\nmodel_runner = model_ref.to_runner()\nmodel_runner.init_local()\nmodel_preprocessor = model_ref.custom_objects[\"preprocessor\"]\n\ntest_transactions = pd.read_csv(\"./data/test_transaction.csv\")[0:500]\ntest_transactions = model_preprocessor.transform(test_transactions)\nresult = model_runner.predict_proba.run(test_transactions)\nprint(np.argmax(result, axis=1))\n```\n\n\n### 4. Serving the model\n\nThe `service.py` file contains the source code for defining an ML service:\n\n```python\nimport numpy as np\nimport pandas as pd\nfrom sample import sample_input\n\nimport bentoml\nfrom bentoml.io import JSON\nfrom bentoml.io import PandasDataFrame\n\nmodel_ref = bentoml.xgboost.get(\"ieee-fraud-detection-lg:latest\")\npreprocessor = model_ref.custom_objects[\"preprocessor\"]\nfraud_model_runner = model_ref.to_runner()\n\nsvc = bentoml.Service(\"fraud_detection\", runners=[fraud_model_runner])\n\ninput_spec = PandasDataFrame.from_sample(sample_input)\n\n@svc.api(input=input_spec, output=JSON())\nasync def is_fraud(input_df: pd.DataFrame):\n    input_df = input_df.astype(sample_input.dtypes)\n    input_features = preprocessor.transform(input_df)\n    results = await fraud_model_runner.predict_proba.async_run(input_features)\n    predictions = np.argmax(results, axis=1)  # 0 is not fraud, 1 is fraud\n    return {\"is_fraud\": list(map(bool, predictions)), \"is_fraud_prob\": results[:, 1]}\n```\n\n\nRun `bentoml serve` command to launch the server locally:\n\n```bash\nbentoml serve\n```\n\n## 🌐 Interacting with the Service 🌐\nThe default mode of BentoML's model serving is via HTTP server. Here, we showcase a few examples of how one can interact with the service:\n### Swagger UI\nVisit `http://localhost:3000/` in a browser and send test requests via the UI.\n\n### cURL\nVia the command `curl`, you can:\n```bash\nhead --lines=200 ./data/test_transaction.csv | curl -X POST -H 'Content-Type: text/csv' --data-binary @- http://0.0.0.0:3000/is_fraud\n```\n\n### Via BentoClient 🐍\n```python\nimport pandas as pd\nfrom bentoml.client import Client\n\ntest_transactions = pd.read_csv(\"./data/test_transaction.csv\")[0:500]\nclient = Client.from_url('localhost:3000')\n\nresults = client.is_fraud(test_transaction)\nprint(results)\n```\n\n\n## 🚀 Deploying to Production 🚀\nEffortlessly transition your project into a production-ready application using [BentoCloud](https://www.bentoml.com/bento-cloud/), the production-ready platform for managing and deploying machine learning models.\n\nStart by creating a BentoCloud account. Once you've signed up, log in to your BentoCloud account using the command:\n\n```bash\nbentoml cloud login --api-token \u003cyour-api-token\u003e --endpoint \u003cbento-cloud-endpoint\u003e\n```\n\u003e Note: Replace `\u003cyour-api-token\u003e` and `\u003cbento-cloud-endpoint\u003e` with your specific API token and the BentoCloud endpoint respectively.\n\nNext, build your BentoML service using the `build` command:\n\n```bash\nbentoml build\n```\n\nThen, push your freshly-built Bento service to BentoCloud using the `push` command:\n\n```bash\nbentoml push \u003cname:version\u003e\n```\n\nLastly, deploy this application to BentoCloud with a single `bentoml deployment create` command following the [deployment instructions](https://docs.bentoml.org/en/latest/reference/cli.html#bentoml-deployment-create).\n\nBentoML offers a number of options for deploying and hosting online ML services into production, learn more at [Deploying a Bento](https://docs.bentoml.org/en/latest/concepts/deploy.html).\n\n---\n\nIn this README, we will also go over a basic deployment strategy with Docker containers.\n\n\n### 1. Build a docker image\n\nBuild a Bento to lock the model version and dependency tree:\n```bash\nbentoml build\n```\n\nEnsure docker is installed and running, build a docker image with `bentoml containerize`\n```bash\nbentoml containerize fraud_detection:latest\n```\n\nTest out the docker image built:\n\n```bash\ndocker run -it --rm -p 3000:3000 fraud_detection:{YOUR BENTO VERSION}\n```\n\n### 2. Inference on GPU\n\nUse `bentofile-gpu.yaml` to build a new Bento, which adds the following two lines to the YAML.\nThis ensures the docker image comes with GPU libraries installed and BentoML will automatically\nload models on GPU when running the docker image with GPU devices available.\n\n```yaml\ndocker:\n  cuda_version: \"11.6.2\"\n```\n\nBuild Bento with GPU support:\n```bash\nbentoml build -f ./bentofile-gpu.yaml\n```\n\nBuild and run docker image with GPU enabled:\n```bash\nbentoml containerize fraud_detection:latest\n\ndocker run --gpus all --device /dev/nvidia0 \\\n           --device /dev/nvidia-uvm --device /dev/nvidia-uvm-tools \\\n           --device /dev/nvidia-modeset --device /dev/nvidiactl \\\n           fraud_detection:{YOUR BENTO VERSION}\n```\n\n### 3. Multi-model Inference Pipeline/Graph\n\nBentoML makes it efficient to create ML service with multiple ML models, which is often used for combining\nmultiple fraud detection models and getting an aggregated result. With BentoML, users can choose to run\nmodels sequentially or in parallel using the Python AsyncIO APIs along with Runners APIs. This makes\nit possible create inference graphes or multi-stage inference pipeline all from Python APIs.\n\nAn example can be found under `inference_graph_demo` that runs all three models simutaneously and \naggregate their results:\n\n```bash\ncd inference_graph_demo\n\nbentoml serve\n```\n\nLearn more about BentoML Runner usage [here](https://docs.bentoml.org/en/latest/concepts/runner.html)\n\n\n### 4. Benchmark Testing\n\nVisit the `/benchmark/README.md` for how to run benchmark tests on your fraud detection service and \nunderstanding its throughput and latency on your deployment target.\n\n\n## 👥Join our Community 👥\n\nBentoML has a thriving open source community where thousands of ML/AI practitioners are contributing to the project, helping other users and discussing the future of AI. [👉 Join us on slack today!](https://l.bentoml.com/join-slack)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbentoml%2Ffraud-detection-model-serving","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbentoml%2Ffraud-detection-model-serving","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbentoml%2Ffraud-detection-model-serving/lists"}