-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
51 lines (43 loc) · 1.63 KB
/
app.py
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
import joblib
from pydantic import BaseModel
from fastapi import FastAPI
import uvicorn
# 1. Load the trained model
model = joblib.load('frauddetection.pkl')
# 2. Define the input data schema using Pydantic BaseModel
class InputData(BaseModel):
Year:int
Month:int
UseChip:int
Amount:int
MerchantName:int
MerchantCity:int
MerchantState:int
mcc:int
# Add the rest of the input features (feature4, feature5, ..., feature12)
# 3. Create a FastAPI app
app = FastAPI()
# 4. Define the prediction route
@app.post('/predict/')
def predict(data: InputData):
# Convert the input data to a dictionary
input_data = data.dict()
# Extract the input features from the dictionary
feature1 = input_data['Year']
feature2=input_data['Month']
feature3=input_data['UseChip']
feature4=input_data['Amount']
feature5=input_data['MerchantName']
feature6=input_data['MerchantCity']
feature7=input_data['MerchantState']
feature8=input_data['mcc']
# Extract the rest of the input features (feature4, feature5, ..., feature12)
# Perform the prediction using the loaded model
prediction = model.predict([[feature1, feature2, feature3,feature4,feature5,feature6,feature7,feature8]]) # Replace ... with the rest of the features
# Convert the prediction to a string (or any other format you prefer)
result = "Fraud" if prediction[0] == 1 else "Not a Fraud"
return {"prediction": result}
# 4. Run the API with uvicorn
# Will run on http://127.0.0.1:8000
if __name__ == '__main__':
uvicorn.run(app, host='127.0.0.1', port=7000)