Spaces:
Sleeping
Sleeping
| 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() | |