Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import requests | |
| import time | |
| # 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 user input into a DataFrame | |
| customer_data = pd.DataFrame([{ | |
| '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 section | |
| if st.button("Predict", type='primary'): | |
| response = requests.post("https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customer", json=customer_data) # enter user name and space name before running the cell | |
| 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") | |
| # Single prediction section | |
| # if st.button("Predict"): | |
| # try: | |
| # print(customer_data) | |
| # response = requests.post( | |
| # "https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customer", | |
| # json=customer_data.to_dict(orient='records')[0] | |
| # ) # Send data to Flask API | |
| # response.raise_for_status() # Raise an error if the request fails | |
| # 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}.") | |
| # except requests.exceptions.RequestException as e: | |
| # st.error(f"Error making prediction: {e}") | |
| # # Function for handling retries with exponential backoff | |
| # def make_request_with_backoff(url, payload, max_retries=5): | |
| # retry_delay = 2 # Initial retry delay (in seconds) | |
| # for attempt in range(max_retries): | |
| # try: | |
| # response = requests.post(url, json=payload) | |
| # response.raise_for_status() # Ensure successful response | |
| # return response.json() # Return data if request succeeds | |
| # except requests.exceptions.RequestException as e: | |
| # if response.status_code == 429: # Too many requests | |
| # st.warning(f"Rate limit exceeded. Retrying in {retry_delay} seconds...") | |
| # time.sleep(retry_delay) # Wait before retrying | |
| # retry_delay *= 2 # Exponential backoff (2s, 4s, 8s, etc.) | |
| # else: | |
| # st.error(f"Request failed: {e}") | |
| # return None | |
| # st.error("Max retries reached. Try again later.") | |
| # return None | |
| # # Single prediction section with retry mechanism | |
| # if st.button("Predict"): | |
| # try: | |
| # print(customer_data) # Display input data for debugging | |
| # result = make_request_with_backoff( | |
| # "https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customer", | |
| # customer_data.to_dict(orient='records')[0] | |
| # ) | |
| # if result: | |
| # churn_prediction = result.get("Prediction", "Unknown") # Safe key lookup | |
| # customer_id = customer_data.get("CustomerID", "Unknown") # Prevent undefined errors | |
| # st.write(f"Based on the information provided, the customer with ID {customer_id} is likely to {churn_prediction}.") | |
| # except KeyError as e: | |
| # st.error(f"Unexpected response format: Missing key {e}") | |
| # except Exception as e: | |
| # st.error(f"Unexpected error: {e}") | |
| # Batch Prediction | |
| 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'): | |
| response = requests.post("https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customerbatch", files={"file": file}) # enter user name and space name before running the cell | |
| if response.status_code == 200: | |
| result = response.json() | |
| st.header("Batch Prediction Results") | |
| st.write(result) | |
| else: | |
| st.error("Error in API request") | |