LazyBoss commited on
Commit
af76fde
·
verified ·
1 Parent(s): b638393

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +29 -59
main.py CHANGED
@@ -1,60 +1,30 @@
1
- import joblib
2
- from pydantic import BaseModel
3
- from fastapi import FastAPI
4
- import uvicorn
5
- import logging
6
  import pickle
7
- import numpy
8
-
9
-
10
- # Save using pickle
11
-
12
- logging.basicConfig(level = logging.INFO)
13
- # 1. Load the trained model
14
- model = joblib.load('frauddetection.pkl')
15
- # 2. Define the input data schema using Pydantic BaseModel
16
- class InputData(BaseModel):
17
- Year:int
18
- Month:int
19
- UseChip:int
20
- Amount:int
21
- MerchantName:int
22
- MerchantCity:int
23
- MerchantState:int
24
- mcc:int
25
- # Add the rest of the input features (feature4, feature5, ..., feature12)
26
-
27
- # 3. Create a FastAPI app
28
- app = FastAPI()
29
- @app.get('/')
30
- def welcome():
31
- return {"Welcome": "This is the home page of the API"}
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)