MainiSandeep1987 commited on
Commit
78be154
·
verified ·
1 Parent(s): 58cd446

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +58 -73
  2. requirements.txt +1 -8
app.py CHANGED
@@ -1,73 +1,58 @@
1
- import joblib
2
- import pandas as pd ## To Read around the CSV Files.
3
- from flask import Flask, request, jsonify ## Fask to create the backwend API using FLASK, request to use and cater the influx requests,\
4
- ## and jsonify to convert the python object to JSON String,as Python
5
- ## object CANNOT be transmitted over HTTP CALLS.
6
-
7
- # Initialize Flask app with a name
8
- app = Flask("Telecom Customer Churn Predictor")
9
-
10
- # Load the trained churn prediction model
11
- model = joblib.load("churn_prediction_model_v1_0.joblib")
12
-
13
- # Define a route for the home page
14
- @app.get('/')
15
- def home():
16
- return "Welcome to the Telecom Customer Churn Prediction API"
17
-
18
- # Define an endpoint to predict churn for a single customer
19
- @app.post('/v1/customer')
20
- def predict_churn():
21
- # Get JSON data from the request
22
- customer_data = request.get_json()
23
-
24
- # Extract relevant customer features from the input data
25
- sample = {
26
- 'SeniorCitizen': customer_data['SeniorCitizen'],
27
- 'Partner': customer_data['Partner'],
28
- 'Dependents': customer_data['Dependents'],
29
- 'tenure': customer_data['tenure'],
30
- 'PhoneService': customer_data['PhoneService'],
31
- 'InternetService': customer_data['InternetService'],
32
- 'Contract': customer_data['Contract'],
33
- 'PaymentMethod': customer_data['PaymentMethod'],
34
- 'MonthlyCharges': customer_data['MonthlyCharges'],
35
- 'TotalCharges': customer_data['TotalCharges']
36
- }
37
-
38
- # Convert the extracted data into a DataFrame
39
- input_data = pd.DataFrame([sample])
40
-
41
- # Make a churn prediction using the trained model
42
- prediction = model.predict(input_data).tolist()[0]
43
-
44
- # Map prediction result to a human-readable label
45
- prediction_label = "churn" if prediction == 1 else "not churn"
46
-
47
- # Return the prediction as a JSON response
48
- return jsonify({'Prediction': prediction_label})
49
-
50
- # Define an endpoint to predict churn for a batch of customers
51
- @app.post('/v1/customerbatch')
52
- def predict_churn_batch():
53
- # Get the uploaded CSV file from the request
54
- file = request.files['file']
55
-
56
- # Read the file into a DataFrame
57
- input_data = pd.read_csv(file)
58
-
59
- # Make predictions for the batch data and convert raw predictions into a readable format
60
- predictions = [
61
- 'Churn' if x == 1
62
- else "Not Churn"
63
- for x in model.predict(input_data.drop("customerID",axis=1)).tolist()
64
- ]
65
-
66
- cust_id_list = input_data.customerID.values.tolist()
67
- output_dict = dict(zip(cust_id_list, predictions))
68
-
69
- return output_dict
70
-
71
- # Run the Flask app in debug mode
72
- if __name__ == '__main__':
73
- app.run(debug=True)
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests
4
+
5
+ # Streamlit UI for Customer Churn Prediction
6
+ st.title("Telecom Customer Churn Prediction App")
7
+ st.write("This tool predicts customer churn risk based on their details. Enter the required information below.")
8
+
9
+ # Collect user input based on dataset columns
10
+ CustomerID = st.number_input("Customer ID", min_value=10000000, max_value=99999999)
11
+ SeniorCitizen = st.selectbox("Senior citizen", ["Yes", "No"])
12
+ Partner = st.selectbox("Does the customer have a partner?", ["Yes", "No"])
13
+ Dependents = st.selectbox("Does the customer have dependents?", ["Yes", "No"])
14
+ PhoneService = st.selectbox("Does the customer have phone service?", ["Yes", "No"])
15
+ InternetService = st.selectbox("Type of Internet Service", ["DSL", "Fiber optic", "No"])
16
+ Contract = st.selectbox("Type of Contract", ["Month-to-month", "One year", "Two year"])
17
+ PaymentMethod = st.selectbox("Payment Method", ["Electronic check", "Mailed check", "Bank transfer", "Credit card"])
18
+ tenure = st.number_input("Tenure (Months with the company)", min_value=0, value=12)
19
+ MonthlyCharges = st.number_input("Monthly Charges", min_value=0.0, value=50.0)
20
+ TotalCharges = st.number_input("Total Charges", min_value=0.0, value=600.0)
21
+
22
+ # Convert categorical inputs to match model training
23
+ customer_data = {
24
+ 'SeniorCitizen': 1 if SeniorCitizen == "Yes" else 0,
25
+ 'Partner':Partner,
26
+ 'Dependents': Dependents,
27
+ 'tenure': tenure,
28
+ 'PhoneService': PhoneService,
29
+ 'InternetService': InternetService,
30
+ 'Contract': Contract,
31
+ 'PaymentMethod': PaymentMethod,
32
+ 'MonthlyCharges': MonthlyCharges,
33
+ 'TotalCharges': TotalCharges
34
+ }
35
+
36
+
37
+ if st.button("Predict", type='primary'):
38
+ response = requests.post("https://MainiSandeep1987-FLASK_API_Telecom_Churn_Prediction.hf.space/v1/customer", json=customer_data) # enter user name and space name before running the cell
39
+ if response.status_code == 200:
40
+ result = response.json()
41
+ churn_prediction = result["Prediction"] # Extract only the value
42
+ st.write(f"Based on the information provided, the customer with ID {CustomerID} is likely to {churn_prediction}.")
43
+ else:
44
+ st.error("Error in API request")
45
+
46
+ # Batch Prediction
47
+ st.subheader("Batch Prediction")
48
+
49
+ file = st.file_uploader("Upload CSV file", type=["csv"])
50
+ if file is not None:
51
+ if st.button("Predict for Batch", type='primary'):
52
+ response = requests.post("https://MainiSandeep1987-FLASK_API_Telecom_Churn_Prediction.hf.space/v1/customerbatch", files={"file": file}) # enter user name and space name before running the cell
53
+ if response.status_code == 200:
54
+ result = response.json()
55
+ st.header("Batch Prediction Results")
56
+ st.write(result)
57
+ else:
58
+ st.error("Error in API request")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,10 +1,3 @@
1
  pandas==2.2.2
2
- numpy==2.0.2
3
- scikit-learn==1.6.1
4
- xgboost==2.1.4
5
- joblib==1.4.2
6
- Werkzeug==2.2.2
7
- flask==2.2.2
8
- gunicorn==20.1.0
9
  requests==2.28.1
10
- uvicorn[standard]
 
1
  pandas==2.2.2
 
 
 
 
 
 
 
2
  requests==2.28.1
3
+ streamlit==1.43.2