Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,73 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
import openai
|
4 |
-
import os
|
5 |
-
from dotenv import load_dotenv
|
6 |
-
|
7 |
-
# Load API key from .env
|
8 |
-
load_dotenv()
|
9 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
10 |
-
|
11 |
-
# Load CSV file
|
12 |
-
df = pd.read_csv(
|
13 |
-
|
14 |
-
# Streamlit UI
|
15 |
-
st.set_page_config(page_title="Customer Churn Chatbot")
|
16 |
-
st.title("Customer Churn Chatbot")
|
17 |
-
st.markdown("Ask if a customer is predicted to churn and get GPT-based suggestions.")
|
18 |
-
|
19 |
-
# Input fields
|
20 |
-
customer_id = st.text_input("Enter Customer ID (optional):")
|
21 |
-
customer_name = st.text_input("Enter Customer Name (optional):")
|
22 |
-
|
23 |
-
if st.button("Check Churn"):
|
24 |
-
result = pd.DataFrame()
|
25 |
-
|
26 |
-
# Search by ID
|
27 |
-
if customer_id:
|
28 |
-
try:
|
29 |
-
customer_id = int(customer_id)
|
30 |
-
result = df[df["CustomerId"] == customer_id]
|
31 |
-
except ValueError:
|
32 |
-
st.error("Customer ID must be an integer.")
|
33 |
-
|
34 |
-
# Search by name
|
35 |
-
elif customer_name:
|
36 |
-
result = df[df["Customer"].str.lower() == customer_name.lower()]
|
37 |
-
|
38 |
-
if not result.empty:
|
39 |
-
row = result.iloc[0]
|
40 |
-
churn = row["PredictedChurn"]
|
41 |
-
|
42 |
-
# Basic suggestion
|
43 |
-
suggestion = (
|
44 |
-
"High risk of churn. Consider giving discounts, loyalty rewards."
|
45 |
-
if churn == 1 else
|
46 |
-
"Customer is stable. Maintain good service."
|
47 |
-
)
|
48 |
-
|
49 |
-
# GPT prompt
|
50 |
-
prompt = (
|
51 |
-
f"Customer: {row['Customer']}\n"
|
52 |
-
f"Churn Prediction: {'Yes' if churn else 'No'}\n"
|
53 |
-
"What actions should be taken to reduce churn or maintain retention?"
|
54 |
-
)
|
55 |
-
|
56 |
-
with st.spinner("Asking GPT..."):
|
57 |
-
try:
|
58 |
-
response = openai.ChatCompletion.create(
|
59 |
-
model="gpt-4", # or "gpt-3.5-turbo" if needed
|
60 |
-
messages=[{"role": "user", "content": prompt}]
|
61 |
-
)
|
62 |
-
gpt_reply = response.choices[0].message["content"]
|
63 |
-
except Exception as e:
|
64 |
-
gpt_reply = f" GPT request failed: {e}"
|
65 |
-
|
66 |
-
# Display output
|
67 |
-
st.success(f"Churn: {'Yes' if churn else 'No'}")
|
68 |
-
st.info(f"Suggestion: {suggestion}")
|
69 |
-
st.markdown(" GPT's Advice:")
|
70 |
-
st.write(gpt_reply)
|
71 |
-
|
72 |
-
else:
|
73 |
-
st.warning("Customer not found. Please check your input.")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import openai
|
4 |
+
import os
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
# Load API key from .env
|
8 |
+
load_dotenv()
|
9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
10 |
+
|
11 |
+
# Load CSV file
|
12 |
+
df = pd.read_csv('churn_predictions.csv')
|
13 |
+
|
14 |
+
# Streamlit UI
|
15 |
+
st.set_page_config(page_title="Customer Churn Chatbot")
|
16 |
+
st.title("Customer Churn Chatbot")
|
17 |
+
st.markdown("Ask if a customer is predicted to churn and get GPT-based suggestions.")
|
18 |
+
|
19 |
+
# Input fields
|
20 |
+
customer_id = st.text_input("Enter Customer ID (optional):")
|
21 |
+
customer_name = st.text_input("Enter Customer Name (optional):")
|
22 |
+
|
23 |
+
if st.button("Check Churn"):
|
24 |
+
result = pd.DataFrame()
|
25 |
+
|
26 |
+
# Search by ID
|
27 |
+
if customer_id:
|
28 |
+
try:
|
29 |
+
customer_id = int(customer_id)
|
30 |
+
result = df[df["CustomerId"] == customer_id]
|
31 |
+
except ValueError:
|
32 |
+
st.error("Customer ID must be an integer.")
|
33 |
+
|
34 |
+
# Search by name
|
35 |
+
elif customer_name:
|
36 |
+
result = df[df["Customer"].str.lower() == customer_name.lower()]
|
37 |
+
|
38 |
+
if not result.empty:
|
39 |
+
row = result.iloc[0]
|
40 |
+
churn = row["PredictedChurn"]
|
41 |
+
|
42 |
+
# Basic suggestion
|
43 |
+
suggestion = (
|
44 |
+
"High risk of churn. Consider giving discounts, loyalty rewards."
|
45 |
+
if churn == 1 else
|
46 |
+
"Customer is stable. Maintain good service."
|
47 |
+
)
|
48 |
+
|
49 |
+
# GPT prompt
|
50 |
+
prompt = (
|
51 |
+
f"Customer: {row['Customer']}\n"
|
52 |
+
f"Churn Prediction: {'Yes' if churn else 'No'}\n"
|
53 |
+
"What actions should be taken to reduce churn or maintain retention?"
|
54 |
+
)
|
55 |
+
|
56 |
+
with st.spinner("Asking GPT..."):
|
57 |
+
try:
|
58 |
+
response = openai.ChatCompletion.create(
|
59 |
+
model="gpt-4", # or "gpt-3.5-turbo" if needed
|
60 |
+
messages=[{"role": "user", "content": prompt}]
|
61 |
+
)
|
62 |
+
gpt_reply = response.choices[0].message["content"]
|
63 |
+
except Exception as e:
|
64 |
+
gpt_reply = f" GPT request failed: {e}"
|
65 |
+
|
66 |
+
# Display output
|
67 |
+
st.success(f"Churn: {'Yes' if churn else 'No'}")
|
68 |
+
st.info(f"Suggestion: {suggestion}")
|
69 |
+
st.markdown(" GPT's Advice:")
|
70 |
+
st.write(gpt_reply)
|
71 |
+
|
72 |
+
else:
|
73 |
+
st.warning("Customer not found. Please check your input.")
|