roseyshi commited on
Commit
45a9a09
·
verified ·
1 Parent(s): dcd714c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -83
app.py CHANGED
@@ -1,83 +1,39 @@
1
- from flask import Flask, request, render_template
2
- import pandas as pd
3
- import numpy as np
4
- from sklearn.ensemble import RandomForestClassifier
5
- from sklearn.model_selection import train_test_split
6
- from joblib import dump, load
7
- import os
8
-
9
- # Existing app initialization
10
- app = Flask(__name__)
11
-
12
-
13
- # Function to load and preprocess the data
14
- def load_and_train_model():
15
- # Load and process data
16
- data = pd.read_csv("clean_train.csv")
17
- X = data.drop('Credit_Score', axis=1)
18
- y = data['Credit_Score']
19
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
20
-
21
- # Train the model
22
- rf = RandomForestClassifier(random_state=42)
23
- rf.fit(X_train, y_train)
24
-
25
- # Save the model to a file
26
- dump(rf, 'credit_classifier.joblib', compress=('gzip', 9))
27
- return rf
28
-
29
- # Load the trained model (or train it if necessary)
30
- def load_model():
31
- if os.path.exists('credit_classifier.joblib'):
32
- return load('credit_classifier.joblib')
33
- else:
34
- return load_and_train_model()
35
-
36
- loaded_model = load_model()
37
-
38
- # Define target names
39
- target_names = {0: "Good", 1: "Poor", 2: "Standard"}
40
-
41
- # Flask routes
42
- @app.route("/")
43
- def home():
44
- return render_template("index.html")
45
-
46
- @app.route('/predict', methods=['POST'])
47
- def predict():
48
- try:
49
- # Extract input values from the form
50
- outstanding_debt = float(request.form['outstanding_debt'])
51
- credit_mix = int(request.form['credit_mix'])
52
- credit_history_age = float(request.form['credit_history_age'])
53
- monthly_balance = float(request.form['monthly_balance'])
54
- payment_behaviour = float(request.form['payment_behaviour'])
55
- annual_income = float(request.form['annual_income'])
56
- delayed_payments = int(request.form['delayed_payments'])
57
-
58
- # Prepare input data for prediction
59
- input_data = [[
60
- outstanding_debt, credit_mix, credit_history_age,
61
- monthly_balance, payment_behaviour, annual_income,
62
- delayed_payments
63
- ]]
64
-
65
- # Predict using the loaded model
66
- prediction = loaded_model.predict(input_data)[0] # Use loaded_model here
67
-
68
- # Map the prediction to the category name
69
- target_names = {0: "Good", 1: "Poor", 2: "Standard"}
70
- prediction_text = target_names.get(prediction, "Unknown")
71
-
72
- # Render result.html with the prediction
73
- return render_template('result.html', prediction=prediction_text)
74
- except Exception as e:
75
- return f"An error occurred: {str(e)}"
76
-
77
-
78
- #if __name__ == "__main__":
79
- #app.run(debug=True)
80
-
81
- if __name__ == "__main__":
82
- app.run(host="0.0.0.0", port=8080)
83
-
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ from joblib import load
5
+
6
+ # Load the trained model
7
+ model = load("credit_classifier.joblib")
8
+
9
+ # Prediction function
10
+ def predict_credit_score(outstanding_debt, credit_mix, credit_history_age, monthly_balance,
11
+ payment_behaviour, annual_income, delayed_payments):
12
+ input_data = [[
13
+ outstanding_debt, credit_mix, credit_history_age,
14
+ monthly_balance, payment_behaviour, annual_income,
15
+ delayed_payments
16
+ ]]
17
+ prediction = model.predict(input_data)[0]
18
+
19
+ target_names = {0: "Good", 1: "Poor", 2: "Standard"}
20
+ return target_names.get(prediction, "Unknown")
21
+
22
+ # Gradio interface
23
+ demo = gr.Interface(
24
+ fn=predict_credit_score,
25
+ inputs=[
26
+ gr.Number(label="Outstanding Debt"),
27
+ gr.Number(label="Credit Mix"),
28
+ gr.Number(label="Credit History Age"),
29
+ gr.Number(label="Monthly Balance"),
30
+ gr.Number(label="Payment Behaviour"),
31
+ gr.Number(label="Annual Income"),
32
+ gr.Number(label="Delayed Payments"),
33
+ ],
34
+ outputs="text",
35
+ title="Credit Scoring Classifier",
36
+ description="Enter your credit-related information to classify your credit score as Good, Standard, or Poor.",
37
+ )
38
+
39
+ demo.launch()