File size: 3,157 Bytes
fcbe7bd
 
 
362c1c1
fcbe7bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8f19f3
 
fcbe7bd
0edfce7
fcbe7bd
 
 
 
 
 
 
 
f8f19f3
8c68a87
0edfce7
8fa2c71
0edfce7
 
 
 
 
8fa2c71
 
 
 
 
 
fcbe7bd
0edfce7
fcbe7bd
 
88f6cc6
fcbe7bd
 
0edfce7
77cf765
 
0edfce7
88f6cc6
4b7ee08
0edfce7
 
 
 
fcbe7bd
 
 
0edfce7
 
 
 
 
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
import streamlit as st
import pandas as pd
import requests

# Streamlit UI for Customer Churn Prediction
st.title("Telecom Customer Churn Prediction App")
st.write("This tool predicts customer churn risk based on their details. Enter the required information below.")

# Collect user input based on dataset columns
CustomerID = st.number_input("Customer ID", min_value=10000000, max_value=99999999)
SeniorCitizen = st.selectbox("Senior citizen", ["Yes", "No"])
Partner = st.selectbox("Does the customer have a partner?", ["Yes", "No"])
Dependents = st.selectbox("Does the customer have dependents?", ["Yes", "No"])
PhoneService = st.selectbox("Does the customer have phone service?", ["Yes", "No"])
InternetService = st.selectbox("Type of Internet Service", ["DSL", "Fiber optic", "No"])
Contract = st.selectbox("Type of Contract", ["Month-to-month", "One year", "Two year"])
PaymentMethod = st.selectbox("Payment Method", ["Electronic check", "Mailed check", "Bank transfer", "Credit card"])
tenure = st.number_input("Tenure (Months with the company)", min_value=0, value=12)
MonthlyCharges = st.number_input("Monthly Charges", min_value=0.0, value=50.0)
TotalCharges = st.number_input("Total Charges", min_value=0.0, value=600.0)

# Convert categorical inputs to match model training
customer_data = {
    'SeniorCitizen': 1 if SeniorCitizen == "Yes" else 0,
    'Partner': Partner,
    'Dependents': Dependents,
    'tenure': tenure,
    'PhoneService': PhoneService,
    'InternetService': InternetService,
    'Contract': Contract,
    'PaymentMethod': PaymentMethod,
    'MonthlyCharges': MonthlyCharges,
    'TotalCharges': TotalCharges
}

# Single Prediction
if st.button("Predict", type='primary'):
    response = requests.post(
        "https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customer",
        json=customer_data
    )  # Send data to Flask API
    
    if response.status_code == 200:
        result = response.json()
        churn_prediction = result["Prediction"]  # Extract only the value
        st.write(f"Based on the information provided, the customer with ID {CustomerID} is likely to {churn_prediction}.")
    else:
        st.error("Error in API request")

# Batch Prediction Section
st.subheader("Batch Prediction")
file = st.file_uploader("Upload CSV file", type=["csv"])

if file is not None:
    if st.button("Predict for Batch", type='primary'):
        try:
            file_data = {"file": ("batch.csv", file.getvalue(), "text/csv")
            }  
            response = requests.post(
                "https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customerbatch",
                files=file_data
            )

            response.raise_for_status()  # Raise error if request fails

            result = response.json()
            st.header("Batch Prediction Results")
            st.write(result)

        except requests.exceptions.RequestException as e:
            st.error(f"Error in API request: {e}")
        except Exception as e:
            st.error(f"Unexpected error: {e}")