Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app.py +3 -68
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
|
| 6 |
# Streamlit UI for Customer Churn Prediction
|
| 7 |
st.title("Telecom Customer Churn Prediction App")
|
| 8 |
st.write("This tool predicts customer churn risk based on their details. Enter the required information below.")
|
|
@@ -20,8 +19,8 @@ tenure = st.number_input("Tenure (Months with the company)", min_value=0, value=
|
|
| 20 |
MonthlyCharges = st.number_input("Monthly Charges", min_value=0.0, value=50.0)
|
| 21 |
TotalCharges = st.number_input("Total Charges", min_value=0.0, value=600.0)
|
| 22 |
|
| 23 |
-
# Convert
|
| 24 |
-
customer_data =
|
| 25 |
'SeniorCitizen': 1 if SeniorCitizen == "Yes" else 0,
|
| 26 |
'Partner':Partner,
|
| 27 |
'Dependents': Dependents,
|
|
@@ -32,10 +31,9 @@ customer_data = pd.DataFrame([{
|
|
| 32 |
'PaymentMethod': PaymentMethod,
|
| 33 |
'MonthlyCharges': MonthlyCharges,
|
| 34 |
'TotalCharges': TotalCharges
|
| 35 |
-
}
|
| 36 |
|
| 37 |
|
| 38 |
-
# Single prediction section
|
| 39 |
if st.button("Predict", type='primary'):
|
| 40 |
response = requests.post("https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customer", json=customer_data) # enter user name and space name before running the cell
|
| 41 |
if response.status_code == 200:
|
|
@@ -45,69 +43,6 @@ if st.button("Predict", type='primary'):
|
|
| 45 |
else:
|
| 46 |
st.error("Error in API request")
|
| 47 |
|
| 48 |
-
|
| 49 |
-
# Single prediction section
|
| 50 |
-
# if st.button("Predict"):
|
| 51 |
-
# try:
|
| 52 |
-
# print(customer_data)
|
| 53 |
-
# response = requests.post(
|
| 54 |
-
# "https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customer",
|
| 55 |
-
# json=customer_data.to_dict(orient='records')[0]
|
| 56 |
-
# ) # Send data to Flask API
|
| 57 |
-
# response.raise_for_status() # Raise an error if the request fails
|
| 58 |
-
# result = response.json()
|
| 59 |
-
# churn_prediction = result["Prediction"] # Extract only the value
|
| 60 |
-
# st.write(f"Based on the information provided, the customer with ID {CustomerID} is likely to {churn_prediction}.")
|
| 61 |
-
# except requests.exceptions.RequestException as e:
|
| 62 |
-
# st.error(f"Error making prediction: {e}")
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# # Function for handling retries with exponential backoff
|
| 67 |
-
# def make_request_with_backoff(url, payload, max_retries=5):
|
| 68 |
-
# retry_delay = 2 # Initial retry delay (in seconds)
|
| 69 |
-
|
| 70 |
-
# for attempt in range(max_retries):
|
| 71 |
-
# try:
|
| 72 |
-
# response = requests.post(url, json=payload)
|
| 73 |
-
# response.raise_for_status() # Ensure successful response
|
| 74 |
-
|
| 75 |
-
# return response.json() # Return data if request succeeds
|
| 76 |
-
|
| 77 |
-
# except requests.exceptions.RequestException as e:
|
| 78 |
-
# if response.status_code == 429: # Too many requests
|
| 79 |
-
# st.warning(f"Rate limit exceeded. Retrying in {retry_delay} seconds...")
|
| 80 |
-
# time.sleep(retry_delay) # Wait before retrying
|
| 81 |
-
# retry_delay *= 2 # Exponential backoff (2s, 4s, 8s, etc.)
|
| 82 |
-
# else:
|
| 83 |
-
# st.error(f"Request failed: {e}")
|
| 84 |
-
# return None
|
| 85 |
-
|
| 86 |
-
# st.error("Max retries reached. Try again later.")
|
| 87 |
-
# return None
|
| 88 |
-
|
| 89 |
-
# # Single prediction section with retry mechanism
|
| 90 |
-
# if st.button("Predict"):
|
| 91 |
-
# try:
|
| 92 |
-
# print(customer_data) # Display input data for debugging
|
| 93 |
-
|
| 94 |
-
# result = make_request_with_backoff(
|
| 95 |
-
# "https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customer",
|
| 96 |
-
# customer_data.to_dict(orient='records')[0]
|
| 97 |
-
# )
|
| 98 |
-
|
| 99 |
-
# if result:
|
| 100 |
-
# churn_prediction = result.get("Prediction", "Unknown") # Safe key lookup
|
| 101 |
-
# customer_id = customer_data.get("CustomerID", "Unknown") # Prevent undefined errors
|
| 102 |
-
# st.write(f"Based on the information provided, the customer with ID {customer_id} is likely to {churn_prediction}.")
|
| 103 |
-
|
| 104 |
-
# except KeyError as e:
|
| 105 |
-
# st.error(f"Unexpected response format: Missing key {e}")
|
| 106 |
-
# except Exception as e:
|
| 107 |
-
# st.error(f"Unexpected error: {e}")
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
# Batch Prediction
|
| 112 |
st.subheader("Batch Prediction")
|
| 113 |
|
|
|
|
| 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.")
|
|
|
|
| 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,
|
|
|
|
| 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-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customer", json=customer_data) # enter user name and space name before running the cell
|
| 39 |
if response.status_code == 200:
|
|
|
|
| 43 |
else:
|
| 44 |
st.error("Error in API request")
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# Batch Prediction
|
| 47 |
st.subheader("Batch Prediction")
|
| 48 |
|
requirements.txt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
pandas==2.2.2
|
| 2 |
requests==2.28.1
|
| 3 |
-
streamlit==1.43.2
|
|
|
|
| 1 |
pandas==2.2.2
|
| 2 |
requests==2.28.1
|
| 3 |
+
streamlit==1.43.2
|