Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import joblib
|
| 6 |
+
from io import StringIO
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Load model and columns
|
| 10 |
+
MODEL_PATH = "xgboost_credit_model.joblib"
|
| 11 |
+
COLS_PATH = "train_features_columns.joblib"
|
| 12 |
+
METRICS_PATH = "evaluation_metrics.joblib"
|
| 13 |
+
|
| 14 |
+
model = joblib.load(MODEL_PATH)
|
| 15 |
+
train_features_columns = joblib.load(COLS_PATH)
|
| 16 |
+
if os.path.exists(METRICS_PATH):
|
| 17 |
+
evaluation_metrics = joblib.load(METRICS_PATH)
|
| 18 |
+
else:
|
| 19 |
+
evaluation_metrics = {}
|
| 20 |
+
|
| 21 |
+
app = Flask(__name__)
|
| 22 |
+
CORS(app)
|
| 23 |
+
|
| 24 |
+
def preprocess_user_data(user_df, train_columns):
|
| 25 |
+
# One-hot encode categorical columns
|
| 26 |
+
categorical_cols = user_df.select_dtypes(include=['object']).columns.tolist()
|
| 27 |
+
user_df = pd.get_dummies(user_df, columns=categorical_cols, drop_first=True)
|
| 28 |
+
# Add missing columns
|
| 29 |
+
missing_cols = set(train_columns) - set(user_df.columns)
|
| 30 |
+
for c in missing_cols:
|
| 31 |
+
user_df[c] = 0
|
| 32 |
+
# Remove extra columns
|
| 33 |
+
extra_cols = set(user_df.columns) - set(train_columns)
|
| 34 |
+
user_df = user_df.drop(columns=list(extra_cols), errors='ignore')
|
| 35 |
+
# Reorder
|
| 36 |
+
user_df = user_df[train_columns]
|
| 37 |
+
return user_df
|
| 38 |
+
|
| 39 |
+
@app.route('/predict', methods=['POST'])
|
| 40 |
+
def predict():
|
| 41 |
+
try:
|
| 42 |
+
user_input = request.json
|
| 43 |
+
user_df = pd.DataFrame([user_input])
|
| 44 |
+
user_features_processed = preprocess_user_data(user_df.copy(), train_features_columns)
|
| 45 |
+
prediction = model.predict(user_features_processed)
|
| 46 |
+
result = "Eligible" if prediction[0] == 1 else "Not Eligible"
|
| 47 |
+
return jsonify({
|
| 48 |
+
'prediction': result,
|
| 49 |
+
'metrics': evaluation_metrics
|
| 50 |
+
})
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return jsonify({'error': str(e)}), 500
|
| 53 |
+
|
| 54 |
+
@app.route('/predict_csv', methods=['POST'])
|
| 55 |
+
def predict_csv():
|
| 56 |
+
try:
|
| 57 |
+
if 'file' not in request.files:
|
| 58 |
+
return jsonify({'error': 'No file part in the request'}), 400
|
| 59 |
+
file = request.files['file']
|
| 60 |
+
if file.filename == '':
|
| 61 |
+
return jsonify({'error': 'No selected file'}), 400
|
| 62 |
+
csv_data = StringIO(file.read().decode('utf-8'))
|
| 63 |
+
input_df = pd.read_csv(csv_data)
|
| 64 |
+
# Remove Creditworthy if present
|
| 65 |
+
if 'Creditworthy' in input_df.columns:
|
| 66 |
+
input_df = input_df.drop(columns=['Creditworthy'])
|
| 67 |
+
input_df = input_df.dropna(axis=1, how='all')
|
| 68 |
+
user_features_processed = preprocess_user_data(input_df.copy(), train_features_columns)
|
| 69 |
+
predictions = model.predict(user_features_processed)
|
| 70 |
+
input_df['Creditworthy_Prediction'] = np.where(predictions == 1, 'Eligible', 'Not Eligible')
|
| 71 |
+
results = input_df.to_dict('records')
|
| 72 |
+
return jsonify({
|
| 73 |
+
'predictions': results,
|
| 74 |
+
'metrics': evaluation_metrics,
|
| 75 |
+
'fairness_metrics': {},
|
| 76 |
+
'fairness_observation': "Fairness metrics require ground truth labels and are not available for this upload."
|
| 77 |
+
})
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return jsonify({'error': str(e)}), 500
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
app.run(host="0.0.0.0", port=7860)
|