File size: 1,978 Bytes
d219831
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI,Query
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
import warnings
warnings.filterwarnings('ignore')
import joblib
import uvicorn

app = FastAPI(title="Credit Card Fraud Detection API",
    description="""An API that utilises a Machine Learning model that detects a credit card transaction fraudulent""",
    version="1.0.0", debug=True)
app.mount("/static", StaticFiles(directory="static"), name="static")

xgb_model = joblib.load('./model/xgb_model.pkl')
rf_model = joblib.load('./model/rf_model.pkl')
enc = joblib.load('./Encoder/WOEEncoder.pkl')


@app.get('/',response_class=HTMLResponse)
def running():
    text='''

    <html>

    <head>

    <link rel="icon" type="image/x-icon" href="static/images/api.png">

    <title>Credit Card Fraud Detection API</title>

    </head>

    <body>

    <div>

    <h1>Credit Card Fraud Detection API</h1>

        <a href="https://github.com/Sibikrish3000/">Github repository</a>

    </div>

    </body>

    </html>

    '''
    return text

class fraudinput(BaseModel):
    cc_freq:int
    cc_freq_class:int
    job:str
    age:int
    gender_M:int
    category:str
    distance_km:float
    hour:str
    hours_diff_bet_trans:int
    amt:float
@app.post('/predict')
async def predict(data: fraudinput,model:str =Query(...)):
    print('data: %s' % data)
    data=data.dict()
    enc_data=enc.transform([data])
    print('model:'+model)
    if model == 'xgboost':
        prediction=xgb_model.predict(enc_data)
    elif model == 'randomforest':
        prediction=rf_model.predict(enc_data)
    else:
        return {'error': 'Invalid model selected'}
    print("prediction:",prediction[0])
    return {"prediction":int(prediction[0])}

#if __name__ == '__main__':
    #uvicorn.run(app, host='127.0.0.1', port=8000)



    #uvicorn.run(app, host="0.0.0.0", port=8000)