ntam0001 commited on
Commit
0f34980
·
verified ·
1 Parent(s): 2a4affb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -44
app.py CHANGED
@@ -1,52 +1,68 @@
1
- from flask import Flask, request, jsonify
2
- from flask_cors import CORS
3
  import pickle
4
 
5
- app = Flask(__name__)
6
- CORS(app) # Enable CORS for all routes
7
-
8
- # Load your model
9
  model = pickle.load(open("best_model_catboost.pkl", "rb"))
10
 
11
- @app.route('/predict', methods=['POST'])
12
- def predict():
13
- try:
14
- # Get data from the request
15
- data = request.json
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- # Prepare features for prediction
18
- features = [
19
- data['person_age'],
20
- data['person_income'],
21
- data['person_emp_length'],
22
- data['loan_amnt'],
23
- data['loan_percent_income'],
24
- data['cb_person_default_on_file'],
25
- data['person_home_ownership_OTHER'],
26
- data['person_home_ownership_OWN'],
27
- data['person_home_ownership_RENT'],
28
- data['loan_intent_DEBTCONSOLIDATION'],
29
- data['loan_intent_EDUCATION'],
30
- data['loan_intent_HOMEIMPROVEMENT'],
31
- data['loan_intent_MEDICAL'],
32
- data['loan_intent_PERSONAL'],
33
- data['loan_intent_VENTURE'],
34
- data['loan_grade_A'],
35
- data['loan_grade_B'],
36
- data['loan_grade_C'],
37
- data['loan_grade_D'],
38
- data['loan_grade_E'],
39
- data['loan_grade_F'],
40
- data['loan_grade_G']
41
- ]
42
 
43
- # Make prediction
44
- prediction = model.predict([features])
 
 
 
 
 
 
 
 
 
 
45
 
46
- # Return prediction as JSON
47
- return jsonify({"prediction": prediction.tolist()})
48
- except Exception as e:
49
- return jsonify({"error": str(e)}), 500
50
 
51
- if __name__ == "__main__":
52
- app.run(debug=True)
 
1
+ import gradio as gr
 
2
  import pickle
3
 
4
+ # Load the CatBoost model
 
 
 
5
  model = pickle.load(open("best_model_catboost.pkl", "rb"))
6
 
7
+ def predict_loan_default(
8
+ person_age, person_income, person_emp_length, loan_amnt, loan_percent_income,
9
+ cb_person_default_on_file, person_home_ownership, loan_intent, loan_grade
10
+ ):
11
+ # Map categorical inputs to one-hot encoded features
12
+ home_ownership_map = {
13
+ "OTHER": [1, 0, 0],
14
+ "OWN": [0, 1, 0],
15
+ "RENT": [0, 0, 1]
16
+ }
17
+ intent_map = {
18
+ "DEBTCONSOLIDATION": [1, 0, 0, 0, 0, 0],
19
+ "EDUCATION": [0, 1, 0, 0, 0, 0],
20
+ "HOMEIMPROVEMENT": [0, 0, 1, 0, 0, 0],
21
+ "MEDICAL": [0, 0, 0, 1, 0, 0],
22
+ "PERSONAL": [0, 0, 0, 0, 1, 0],
23
+ "VENTURE": [0, 0, 0, 0, 0, 1]
24
+ }
25
+ grade_map = {
26
+ "A": [1, 0, 0, 0, 0, 0, 0],
27
+ "B": [0, 1, 0, 0, 0, 0, 0],
28
+ "C": [0, 0, 1, 0, 0, 0, 0],
29
+ "D": [0, 0, 0, 1, 0, 0, 0],
30
+ "E": [0, 0, 0, 0, 1, 0, 0],
31
+ "F": [0, 0, 0, 0, 0, 1, 0],
32
+ "G": [0, 0, 0, 0, 0, 0, 1]
33
+ }
34
+
35
+ # Prepare features for prediction
36
+ features = [
37
+ person_age,
38
+ person_income,
39
+ person_emp_length,
40
+ loan_amnt,
41
+ loan_percent_income,
42
+ int(cb_person_default_on_file == "Yes"),
43
+ *home_ownership_map[person_home_ownership],
44
+ *intent_map[loan_intent],
45
+ *grade_map[loan_grade]
46
+ ]
47
 
48
+ # Make prediction
49
+ prediction = model.predict([features])
50
+ return "Default" if prediction[0] == 1 else "No Default"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ # Define Gradio interface
53
+ inputs = [
54
+ gr.Number(label="Person Age"),
55
+ gr.Number(label="Person Income"),
56
+ gr.Number(label="Person Employment Length"),
57
+ gr.Number(label="Loan Amount"),
58
+ gr.Number(label="Loan Percent Income"),
59
+ gr.Radio(["Yes", "No"], label="Default on File"),
60
+ gr.Radio(["OTHER", "OWN", "RENT"], label="Home Ownership"),
61
+ gr.Radio(["DEBTCONSOLIDATION", "EDUCATION", "HOMEIMPROVEMENT", "MEDICAL", "PERSONAL", "VENTURE"], label="Loan Intent"),
62
+ gr.Radio(["A", "B", "C", "D", "E", "F", "G"], label="Loan Grade")
63
+ ]
64
 
65
+ outputs = gr.Textbox(label="Prediction")
 
 
 
66
 
67
+ # Launch Gradio app
68
+ gr.Interface(fn=predict_loan_default, inputs=inputs, outputs=outputs, title="Credit Risk Prediction").launch()