Update main.py
Browse files
main.py
CHANGED
|
@@ -1,60 +1,30 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
| 3 |
-
from fastapi import FastAPI
|
| 4 |
-
import uvicorn
|
| 5 |
-
import logging
|
| 6 |
import pickle
|
| 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 |
-
# 4. Define the prediction route
|
| 35 |
-
@app.post('/predict/')
|
| 36 |
-
async def predict(data: InputData):
|
| 37 |
-
# Convert the input data to a dictionary
|
| 38 |
-
input_data = data.dict()
|
| 39 |
-
|
| 40 |
-
# Extract the input features from the dictionary
|
| 41 |
-
feature1 = input_data['Year']
|
| 42 |
-
feature2=input_data['Month']
|
| 43 |
-
feature3=input_data['UseChip']
|
| 44 |
-
feature4=input_data['Amount']
|
| 45 |
-
feature5=input_data['MerchantName']
|
| 46 |
-
feature6=input_data['MerchantCity']
|
| 47 |
-
feature7=input_data['MerchantState']
|
| 48 |
-
feature8=input_data['mcc']
|
| 49 |
-
# Extract the rest of the input features (feature4, feature5, ..., feature12)
|
| 50 |
-
|
| 51 |
-
# Perform the prediction using the loaded model
|
| 52 |
-
prediction = model.predict([[feature1, feature2, feature3,feature4,feature5,feature6,feature7,feature8]]) # Replace ... with the rest of the features
|
| 53 |
-
# Convert the prediction to a string (or any other format you prefer)
|
| 54 |
-
result = "Fraud" if prediction[0] == 1 else "Not a Fraud"
|
| 55 |
-
# result = prediction
|
| 56 |
-
return {"prediction": result}
|
| 57 |
-
# 4. Run the API with uvicorn
|
| 58 |
-
# Will run on http://127.0.0.1:8000
|
| 59 |
-
if __name__ == '__main__':
|
| 60 |
-
uvicorn.run(app, port=8080)
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from flask import Flask, request, render_template
|
|
|
|
|
|
|
|
|
|
| 3 |
import pickle
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
model = pickle.load(open('model.pkl', 'rb'))
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@app.route('/')
|
| 11 |
+
def home():
|
| 12 |
+
return render_template('index.html')
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@app.route('/predict', methods=['POST', 'GET'])
|
| 16 |
+
def predict():
|
| 17 |
+
|
| 18 |
+
int_features = [float(x) for x in request.form.values()]
|
| 19 |
+
final_features = [np.array(int_features)]
|
| 20 |
+
prediction = model.predict(final_features)
|
| 21 |
+
|
| 22 |
+
if prediction == 0:
|
| 23 |
+
return render_template('Low chances of transaction being fraud'),
|
| 24 |
+
)
|
| 25 |
+
else:
|
| 26 |
+
return render_template('High chances of transaction being fraud')
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|