HOLYBOY commited on
Commit
eac22b0
·
1 Parent(s): a123582
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import gradio as gr
3
+ from sklearn.model_selection import train_test_split
4
+ from sklearn.preprocessing import LabelEncoder
5
+ from sklearn.ensemble import AdaBoostClassifier
6
+ from PIL import Image
7
+ import pickle
8
+ # Load the dataset
9
+ df = pd.read_csv("Telco_Customer.csv")
10
+
11
+ # Separate the features and target variable
12
+ X = df.drop("Churn", axis=1)
13
+ y = df["Churn"]
14
+
15
+ # Encode the categorical variables
16
+ categorical_vars = X.select_dtypes(include="object").columns.tolist()
17
+ encoders = {}
18
+ for var in categorical_vars:
19
+ encoder = LabelEncoder()
20
+ X[var] = encoder.fit_transform(X[var])
21
+ encoders[var] = encoder
22
+
23
+ # Split the data into training and testing sets
24
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
25
+
26
+ # Define and train the AdaBoost classifier
27
+ adaboost = AdaBoostClassifier()
28
+ adaboost.fit(X_train, y_train)
29
+
30
+ # Save the trained model using pickle
31
+ model_filename = "adaboost_model.pkl"
32
+ with open(model_filename, "wb") as file:
33
+ pickle.dump(adaboost, file)
34
+
35
+ # Define prediction function using the loaded model
36
+ def predict_churn(*data):
37
+ # Load the saved model
38
+ with open(model_filename, "rb") as file:
39
+ loaded_model = pickle.load(file)
40
+
41
+ # Encode the inputs
42
+ encoded_data = []
43
+ for i, var in enumerate(categorical_vars):
44
+ encoder = encoders[var]
45
+ encoded_value = encoder.transform([data[i]])[0]
46
+ encoded_data.append(encoded_value)
47
+
48
+ # Make predictions on the encoded data
49
+ encoded_df = pd.DataFrame([encoded_data], columns=X.columns)
50
+ prediction = loaded_model.predict(encoded_df)
51
+
52
+ # Save inputs and output to CSV
53
+ input_data = pd.DataFrame([data], columns=X.columns)
54
+ input_data["Churn Prediction"] = prediction[0]
55
+ input_data.to_csv("history.csv", index=False)
56
+
57
+ return "Churn Prediction: " + prediction[0]
58
+
59
+ # Create the dropdown choices using raw data
60
+ dropdown_choices = {}
61
+ for var in categorical_vars:
62
+ dropdown_choices[var] = list(df[var].unique())
63
+
64
+
65
+ # Create the input interfaces using Gradio
66
+ input_interfaces = [gr.inputs.Dropdown(choices=dropdown_choices[col], label=col) for col in X.columns]
67
+ output_interface = gr.outputs.Textbox()
68
+
69
+ # Create the Gradio interface
70
+ iface = gr.Interface(fn=predict_churn, inputs=input_interfaces, outputs=output_interface, title = "Customer Churn Prediction App")
71
+
72
+
73
+ # Run the Gradio interface
74
+ iface.launch(share =True)