-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
89 lines (78 loc) · 2.36 KB
/
main.py
File metadata and controls
89 lines (78 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# First, we will need to import the library and initialize the main application object:
import joblib
import uvicorn
from fastapi import FastAPI,Request
from pydantic import BaseModel
import pandas as pd
#import nest_asyncio
from typing import List
#from typing import Any, Dict,List,Enum
# import numpy as np
## API INSTANTIATION
## ----------------------------------------------------------------
app = FastAPI(
title="Fraud Detection API",
description="A simple API that use Ml model to predict fraud ",
version="0.1",
)
# Creating the data model for data validation
class ClientData(BaseModel):
step: List[int]
type: List[str]
amount: List[float]
nameOrig: List[str]
oldbalanceOrig: List[float]
newbalanceOrig: List[float]
nameDest: List[str]
oldbalanceDest: List[float]
newbalanceDest: List[float]
# Load the model a serialized .joblib file
joblib_filename="models/lgbm_localV1.joblib"
model = joblib.load(joblib_filename)
## API ENDPOINTS
## ----------------------------------------------------------------
##################
@app.get('/')
def index():
'''
This is a first docstring.
'''
return {'message': 'This is a Fraud Classification API!'}
# Tester
@app.get('/ping')
def ping():
'''
This is a first docstring.
'''
return ('pong', 200)
# Defining the prediction endpoint without data validation
@app.post('/basic_predict')
async def basic_predict(request: Request):
'''
This is a first docstring.
'''
# Getting the JSON from the body of the request
input_data = await request.json()
# Converting JSON to Pandas DataFrame
input_df = pd.DataFrame([input_data])
# Getting the prediction
pred = model.predict(input_df)[0]
return pred
# We now define the function that will be executed for each URL request and return the value:
@app.post("/predict-fraud")
async def predict_fraud(item :ClientData):
"""
A simple function that receive a client data and predict Fraud.
:param client_data:
:return: prediction, probabilities
"""
# perform prediction
#df =pd.DataFrame([item])
h=item.dict()
df=pd.DataFrame.from_dict(h, orient="columns")
prediction = model.predict(df)
prediction_final=["Fraud" if (x > 0.5) else "Not Fraud" for x in prediction ]
return prediction_final
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=5000)
# uvicorn app:app --reload