This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed real-time integration with PyCaret for Credit Card Fraud Detect…
…ion (#370)
- Loading branch information
1 parent
fb977de
commit 4a45332
Showing
18 changed files
with
4,799 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...isk-management/operational-risk/credit-card-fraud-detection/atoti-pycaret/.python-version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.8.7 |
53 changes: 53 additions & 0 deletions
53
...management/operational-risk/credit-card-fraud-detection/atoti-pycaret/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Endpoint for Credit Card Fraud prediction | ||
|
||
The package automl consists of machine learning models that we have trained using [PyCaret](https://pycaret.org/). | ||
|
||
By creating a small [Flask application](https://flask.palletsprojects.com/en/2.2.x/), we are able to create an endpoint that takes in the features for the model to perform fraud prediction. | ||
|
||
# Installation | ||
|
||
Set up the virtual environment for the project using the below command: | ||
``` | ||
poetry install | ||
``` | ||
|
||
Refer to the [poetry documentation](https://python-poetry.org/docs/master/#installing-with-the-official-installer) for more information on the package manager. | ||
|
||
|
||
# Runtime | ||
To launch the Flask application, run the following command: | ||
``` | ||
poetry run python .\automl\prediction.py | ||
``` | ||
|
||
You should able to see the following: | ||
|
||
<img src="../img/flask_endpoint.png"> | ||
|
||
We can post requests to the endpoint at http://127.0.0.1:105/predict, e.g. | ||
|
||
``` | ||
def get_prediction(features_df): | ||
url = "http://127.0.0.1:105/predict" | ||
header = {"Content-Type": "application/json"} | ||
payload = { | ||
"features": features_df.to_json(orient="records"), | ||
} | ||
try: | ||
response = requests.post(url, json=payload) | ||
prediction = pd.DataFrame.from_dict(response.json()) | ||
return prediction | ||
except requests.exceptions.HTTPError as e: | ||
print(e.response.text) | ||
``` | ||
|
||
You can verify that the requests are received by the endpoint through the shell running this program: | ||
|
||
<img src="../img/request_received.png"/> | ||
|
||
|
||
The endpoint returns a Pandas Dataframe containing the features and their corresponding prediction. |
Binary file added
BIN
+3.21 MB
...-risk/credit-card-fraud-detection/atoti-pycaret/automl/models/Final_DT_Model_20211130.pkl
Binary file not shown.
Binary file added
BIN
+3.25 MB
...-risk/credit-card-fraud-detection/atoti-pycaret/automl/models/Final_ET_Model_20211130.pkl
Binary file not shown.
Binary file added
BIN
+29 MB
...it-card-fraud-detection/atoti-pycaret/automl/models/Final_IForest_Full_Model_20211201.pkl
Binary file not shown.
Binary file added
BIN
+21.5 MB
.../credit-card-fraud-detection/atoti-pycaret/automl/models/Final_IForest_Model_20211201.pkl
Binary file not shown.
Binary file added
BIN
+3.52 MB
...isk/credit-card-fraud-detection/atoti-pycaret/automl/models/Final_LGBM_Model_20211130.pkl
Binary file not shown.
33 changes: 33 additions & 0 deletions
33
...anagement/operational-risk/credit-card-fraud-detection/atoti-pycaret/automl/prediction.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from flask import Flask, jsonify, request | ||
import pandas as pd | ||
import pycaret.classification as pyc | ||
import pickle | ||
import os | ||
|
||
app = Flask(__name__) | ||
|
||
dir_path = os.path.dirname(os.path.realpath(__file__)) | ||
print(dir_path) | ||
|
||
|
||
def predict(df): | ||
model = pyc.load_model("./automl/models/Final_LGBM_Model_20211130") | ||
return pyc.predict_model(model, data=df) | ||
|
||
|
||
@app.route("/predict", methods=["POST"]) | ||
def predict_model(): | ||
test = request.json | ||
|
||
features_json = test["features"] | ||
features_df = pd.read_json(features_json) | ||
print(f"Features received: {len(features_df)}") | ||
|
||
model_prediction = predict(features_df) | ||
print(f"Prediction completed for {len(model_prediction)}") | ||
|
||
return model_prediction.to_json(orient="records") | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(host="0.0.0.0", port=105) |
Oops, something went wrong.