Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from sklearn.svm import SVC
|
3 |
+
from sklearn.preprocessing import LabelEncoder
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
import joblib
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
# Load data function
|
9 |
+
def load_data():
|
10 |
+
data = pd.read_excel('cleaned1.xlsx')
|
11 |
+
label = LabelEncoder()
|
12 |
+
for col in data.columns:
|
13 |
+
data[col] = label.fit_transform(data[col])
|
14 |
+
return data
|
15 |
+
|
16 |
+
# Split data into features and target
|
17 |
+
def split(df):
|
18 |
+
y = df.Current_Stage
|
19 |
+
x = df.drop(columns=['Current_Stage'])
|
20 |
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)
|
21 |
+
return x_train, x_test, y_train, y_test
|
22 |
+
|
23 |
+
# Train the model
|
24 |
+
def train_model():
|
25 |
+
df = load_data()
|
26 |
+
x_train, x_test, y_train, y_test = split(df)
|
27 |
+
model = SVC(C=0.1, kernel='linear', gamma='scale')
|
28 |
+
model.fit(x_train, y_train)
|
29 |
+
accuracy = model.score(x_test, y_test)
|
30 |
+
print(f'Model accuracy: {accuracy * 100}%')
|
31 |
+
|
32 |
+
# Save the model using joblib
|
33 |
+
joblib.dump(model, 'svm_model.pkl')
|
34 |
+
print('Model saved as svm_model.pkl')
|
35 |
+
|
36 |
+
# Load the saved model
|
37 |
+
def load_model():
|
38 |
+
return joblib.load('svm_model.pkl')
|
39 |
+
|
40 |
+
# Function to preprocess the input and predict
|
41 |
+
def preprocess_inputs(inputs):
|
42 |
+
# Ensure that all columns required during training are present
|
43 |
+
feature_columns = [
|
44 |
+
'Credit Expiration', 'DPD', 'FS', 'CDR', 'SICR', 'Follow Up',
|
45 |
+
'Rescheduled ', 'Restructuring', 'Covenant', 'Turnover', 'Group Reason',
|
46 |
+
' Stage As last Month'
|
47 |
+
]
|
48 |
+
|
49 |
+
# Convert "Yes" -> 1 and "No" -> 0 for encoding the radio inputs
|
50 |
+
encoded_inputs = [1 if i == 'Yes' else 0 if i == 'No' else i for i in inputs[:11]]
|
51 |
+
|
52 |
+
# Convert "Stage As last Month" to an integer (choices 1, 2, or 3)
|
53 |
+
stage_as_last_month = int(inputs[11])
|
54 |
+
|
55 |
+
# Append the selected "Stage As last Month" to the list of inputs
|
56 |
+
encoded_inputs.append(stage_as_last_month)
|
57 |
+
|
58 |
+
# Ensure the inputs match the feature columns order
|
59 |
+
return pd.DataFrame([encoded_inputs], columns=feature_columns)
|
60 |
+
|
61 |
+
# Prediction function for Gradio interface
|
62 |
+
def predict(*inputs):
|
63 |
+
model = load_model()
|
64 |
+
|
65 |
+
# Preprocess the inputs to match the training format
|
66 |
+
input_data = preprocess_inputs(inputs)
|
67 |
+
|
68 |
+
# Make prediction
|
69 |
+
y_pred = model.predict(input_data)
|
70 |
+
return f"The predicted class is: {y_pred[0]}"
|
71 |
+
|
72 |
+
# Train the model and save it (only run once)
|
73 |
+
train_model()
|
74 |
+
|
75 |
+
# Build the Gradio interface with radio buttons and textboxes
|
76 |
+
gr.Interface(
|
77 |
+
fn=predict,
|
78 |
+
inputs=[
|
79 |
+
gr.Textbox(label="Credit Expiration"),
|
80 |
+
gr.Textbox(label="DPD"),
|
81 |
+
gr.Radio(choices=["Yes", "No"], label="FS"),
|
82 |
+
gr.Radio(choices=["Yes", "No"], label="CDR"),
|
83 |
+
gr.Radio(choices=["Yes", "No"], label="SICR"),
|
84 |
+
gr.Radio(choices=["Yes", "No"], label="Follow Up"),
|
85 |
+
gr.Radio(choices=["Yes", "No"], label="Rescheduled"),
|
86 |
+
gr.Radio(choices=["Yes", "No"], label="Restructuring"),
|
87 |
+
gr.Radio(choices=["Yes", "No"], label="Covenant"),
|
88 |
+
gr.Radio(choices=["Yes", "No"], label="Turnover"),
|
89 |
+
gr.Radio(choices=["Yes", "No"], label="Group Reason"),
|
90 |
+
gr.Radio(choices=["1", "2", "3"], label="Stage As last Month") # Updated with choices 1, 2, 3
|
91 |
+
],
|
92 |
+
outputs="text"
|
93 |
+
).launch()
|