Rename app.py to main,py
Browse files- app.py → main,py +49 -1
app.py → main,py
RENAMED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import main
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
-
from main import clf_rf, clf_log, accuracy_score_rf, accuracy_score_lr, brier_score_rf, brier_score_lr, roc_rf, roc_lr
|
| 6 |
|
| 7 |
def eda(Graphs):
|
| 8 |
match Graphs:
|
|
@@ -143,6 +143,39 @@ def metrics(Algorithms):
|
|
| 143 |
)
|
| 144 |
return df_clf, df_acc
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
with gr.Blocks() as Output:
|
| 147 |
gr.Markdown("View Exploratory data Analysis and Output")
|
| 148 |
with gr.Tab("EDA Graphs"):
|
|
@@ -165,5 +198,20 @@ with gr.Blocks() as Output:
|
|
| 165 |
|
| 166 |
algorithm.change(fn = metrics, inputs = algorithm, outputs = metrics_output)
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
Output.launch()
|
|
|
|
| 2 |
import main
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
+
from main import clf_rf, clf_log, accuracy_score_rf, accuracy_score_lr, brier_score_rf, brier_score_lr, roc_rf, roc_lr, logistic, rf_clf, encoder, scaler
|
| 6 |
|
| 7 |
def eda(Graphs):
|
| 8 |
match Graphs:
|
|
|
|
| 143 |
)
|
| 144 |
return df_clf, df_acc
|
| 145 |
|
| 146 |
+
def predictChurn(model, filename):
|
| 147 |
+
dataset = pd.read_csv(filename)
|
| 148 |
+
|
| 149 |
+
customers = dataset["customerID"]
|
| 150 |
+
dataset.drop(columns=['customerID'], inplace=True)
|
| 151 |
+
|
| 152 |
+
for column in dataset.select_dtypes(include=['int64', 'float64']).columns:
|
| 153 |
+
dataset[column] = scaler.fit_transform(dataset[column].values.reshape(-1, 1))
|
| 154 |
+
for column in dataset.select_dtypes(include=['object']).columns:
|
| 155 |
+
dataset[column] = encoder.fit_transform(dataset[column])
|
| 156 |
+
|
| 157 |
+
dataset.drop(columns=['gender', 'PhoneService', 'MultipleLines', 'InternetService', 'StreamingTV', 'StreamingMovies', 'TotalCharges'], inplace=True)
|
| 158 |
+
|
| 159 |
+
match model:
|
| 160 |
+
case "Logistic Regression":
|
| 161 |
+
model = logistic
|
| 162 |
+
case "Random Forest":
|
| 163 |
+
model = rf_clf
|
| 164 |
+
|
| 165 |
+
y_predictions = model.predict(dataset)
|
| 166 |
+
print(y_predictions)
|
| 167 |
+
d = {0: "No Churn", 1: "Churn"}
|
| 168 |
+
|
| 169 |
+
l = zip(customers, y_predictions)
|
| 170 |
+
|
| 171 |
+
df = pd.DataFrame(l, columns= ["Customer ID", "Output"])
|
| 172 |
+
|
| 173 |
+
for i in range(len(df["Customer ID"])):
|
| 174 |
+
output = d[y_predictions[i]]
|
| 175 |
+
df["Output"][i] = output
|
| 176 |
+
|
| 177 |
+
return gr.DataFrame(value = df)
|
| 178 |
+
|
| 179 |
with gr.Blocks() as Output:
|
| 180 |
gr.Markdown("View Exploratory data Analysis and Output")
|
| 181 |
with gr.Tab("EDA Graphs"):
|
|
|
|
| 198 |
|
| 199 |
algorithm.change(fn = metrics, inputs = algorithm, outputs = metrics_output)
|
| 200 |
|
| 201 |
+
with gr.Tab("Predict Live"):
|
| 202 |
+
gr.Markdown("# Predict Churn")
|
| 203 |
+
model = gr.Radio(["Logistic Regression", "Random Forest"], show_label = False)
|
| 204 |
+
file = gr.File()
|
| 205 |
+
dataset = gr.UploadButton("Upload Dataset(as CSV file)", file_count = "single")
|
| 206 |
+
predict = gr.Button("Predict")
|
| 207 |
+
|
| 208 |
+
op_df = gr.DataFrame(headers = ["Customer ID", "Output"])
|
| 209 |
+
op_md = gr.Markdown("# Predicted Churns")
|
| 210 |
+
|
| 211 |
+
clear = gr.ClearButton(components = [model, file, dataset, op_df])
|
| 212 |
+
|
| 213 |
+
dataset.upload(lambda file: file, dataset, file)
|
| 214 |
+
predict.click(fn = predictChurn, inputs = [model, dataset], outputs = op_df)
|
| 215 |
+
clear.click()
|
| 216 |
|
| 217 |
Output.launch()
|