File size: 3,250 Bytes
910ee10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import pandas as pd
from sklearn.svm import SVC
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
import joblib
import gradio as gr

# Load data function
def load_data():
    data = pd.read_excel('cleaned1.xlsx')
    label = LabelEncoder()
    for col in data.columns:
        data[col] = label.fit_transform(data[col])
    return data

# Split data into features and target
def split(df):
    y = df.Current_Stage
    x = df.drop(columns=['Current_Stage'])
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)
    return x_train, x_test, y_train, y_test

# Train the model
def train_model():
    df = load_data()
    x_train, x_test, y_train, y_test = split(df)
    model = SVC(C=0.1, kernel='linear', gamma='scale')
    model.fit(x_train, y_train)
    accuracy = model.score(x_test, y_test)
    print(f'Model accuracy: {accuracy * 100}%')
    
    # Save the model using joblib
    joblib.dump(model, 'svm_model.pkl')
    print('Model saved as svm_model.pkl')

# Load the saved model
def load_model():
    return joblib.load('svm_model.pkl')

# Function to preprocess the input and predict
def preprocess_inputs(inputs):
    # Ensure that all columns required during training are present
    feature_columns = [
        'Credit Expiration', 'DPD', 'FS', 'CDR', 'SICR', 'Follow Up', 
        'Rescheduled ', 'Restructuring', 'Covenant', 'Turnover', 'Group Reason',
        ' Stage As last Month'
    ]
    
    # Convert "Yes" -> 1 and "No" -> 0 for encoding the radio inputs
    encoded_inputs = [1 if i == 'Yes' else 0 if i == 'No' else i for i in inputs[:11]]
    
    # Convert "Stage As last Month" to an integer (choices 1, 2, or 3)
    stage_as_last_month = int(inputs[11])
    
    # Append the selected "Stage As last Month" to the list of inputs
    encoded_inputs.append(stage_as_last_month)
    
    # Ensure the inputs match the feature columns order
    return pd.DataFrame([encoded_inputs], columns=feature_columns)

# Prediction function for Gradio interface
def predict(*inputs):
    model = load_model()
    
    # Preprocess the inputs to match the training format
    input_data = preprocess_inputs(inputs)
    
    # Make prediction
    y_pred = model.predict(input_data)
    return f"The predicted class is: {y_pred[0]}"

# Train the model and save it (only run once)
train_model()

# Build the Gradio interface with radio buttons and textboxes
gr.Interface(
    fn=predict,
    inputs=[
        gr.Textbox(label="Credit Expiration"),
        gr.Textbox(label="DPD"),
        gr.Radio(choices=["Yes", "No"], label="FS"),
        gr.Radio(choices=["Yes", "No"], label="CDR"),
        gr.Radio(choices=["Yes", "No"], label="SICR"),
        gr.Radio(choices=["Yes", "No"], label="Follow Up"),
        gr.Radio(choices=["Yes", "No"], label="Rescheduled"),
        gr.Radio(choices=["Yes", "No"], label="Restructuring"),
        gr.Radio(choices=["Yes", "No"], label="Covenant"),
        gr.Radio(choices=["Yes", "No"], label="Turnover"),
        gr.Radio(choices=["Yes", "No"], label="Group Reason"),
        gr.Radio(choices=["1", "2", "3"], label="Stage As last Month")  # Updated with choices 1, 2, 3
    ],
    outputs="text"
).launch()