|
import streamlit as st
|
|
import pandas as pd
|
|
import numpy as np
|
|
import random
|
|
import matplotlib.pyplot as plt
|
|
from sklearn.preprocessing import MinMaxScaler
|
|
from backend.src.dqn_agent import AdvancedDQNAgent
|
|
|
|
st.set_page_config(page_title="Next (AI): Customer", layout="wide")
|
|
st.title("Next AI: Product Recommendation Agent")
|
|
st.markdown("Chat with nExT(AI) to get real-time customer targeting recommendations. Type a query (e.g., 'Show me discount recommendations') and nExT(AI) will reply with the corresponding customer table.")
|
|
|
|
|
|
if "chat_history" not in st.session_state:
|
|
st.session_state.chat_history = []
|
|
|
|
|
|
@st.cache_data(show_spinner=False)
|
|
def load_data():
|
|
df = pd.read_csv("data\\customers.csv")
|
|
return df
|
|
|
|
def preprocess_data(df):
|
|
churn_mapping = {"Low": 0, "Medium": 1, "High": 2}
|
|
df['ChurnRiskEncoded'] = df['ChurnRisk'].map(churn_mapping)
|
|
features = df[['Age', 'Income', 'PurchaseFrequency', 'AvgSpend', 'ChurnRiskEncoded']].values
|
|
scaler = MinMaxScaler()
|
|
features = scaler.fit_transform(features)
|
|
return features
|
|
|
|
|
|
df = load_data()
|
|
states = preprocess_data(df)
|
|
|
|
|
|
state_size = states.shape[1]
|
|
action_size = 3
|
|
agent = AdvancedDQNAgent(state_size, action_size)
|
|
|
|
|
|
recommendations = [agent.act(state) for state in states]
|
|
df['Recommendation'] = recommendations
|
|
|
|
|
|
discount_df = df[df['Recommendation'] == 0]
|
|
product_df = df[df['Recommendation'] == 1]
|
|
no_action_df = df[df['Recommendation'] == 2]
|
|
|
|
|
|
def process_query(query):
|
|
query_lower = query.lower()
|
|
if "discount" in query_lower:
|
|
response = "Here are the customers recommended for a discount (Action 0):"
|
|
table = discount_df[['CustomerID', 'Age', 'Income', 'PurchaseFrequency', 'AvgSpend', 'ChurnRisk']]
|
|
elif "product" in query_lower:
|
|
response = "Here are the customers recommended for a product suggestion (Action 1):"
|
|
table = product_df[['CustomerID', 'Age', 'Income', 'PurchaseFrequency', 'AvgSpend', 'ChurnRisk']]
|
|
elif "no action" in query_lower:
|
|
response = "Here are the customers for whom no specific action is recommended (Action 2):"
|
|
table = no_action_df[['CustomerID', 'Age', 'Income', 'PurchaseFrequency', 'AvgSpend', 'ChurnRisk']]
|
|
elif "all" in query_lower or "recommendation" in query_lower:
|
|
response = "Here are all customer recommendations:"
|
|
table = df[['CustomerID', 'Age', 'Income', 'PurchaseFrequency', 'AvgSpend', 'ChurnRisk', 'Recommendation']]
|
|
else:
|
|
response = "I'm sorry, I didn't understand that. Please ask for discount, product, or no action recommendations."
|
|
table = None
|
|
return response, table
|
|
|
|
|
|
for chat in st.session_state.chat_history:
|
|
if chat["role"] == "user":
|
|
st.markdown(f"**User:** {chat['message']}")
|
|
else:
|
|
st.markdown(f"**nExT(AI):** {chat['message']}")
|
|
if chat.get("table") is not None:
|
|
st.table(chat["table"])
|
|
|
|
|
|
user_input = st.text_input("Type your message here and press Enter:")
|
|
|
|
if user_input:
|
|
|
|
st.session_state.chat_history.append({"role": "user", "message": user_input})
|
|
|
|
reply_text, reply_table = process_query(user_input)
|
|
st.session_state.chat_history.append({"role": "agent", "message": reply_text, "table": reply_table})
|
|
|
|
|
|
|