import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import gradio as gr
import re
custom_theme = gr.themes.Base().set(
body_background_fill="linear-gradient(120deg, #f9fcff 0%, #fef6ff 100%)",
block_background_fill="white",
block_shadow="0px 5px 15px rgba(0,0,0,0.06)",
block_border_width="1px",
block_border_color="#e6e6e6",
)
# Run predictions
results_df = run_batch_predictions(df, pipe)
# Create a new DataFrame without 'Clusters' and 'Recommendation'
export_df = results_df.drop(columns=[col for col in ['Clusters', 'Recommendation'] if col in results_df.columns])
# Save to CSV
export_df.to_csv('results_summary.csv', index=False)
# Add the constant column
results_df["RecordCount"] = 1
# Extract actions
''''
def extract_actions(html_text):
if not isinstance(html_text, str):
return ["No action"]
text = html_text.replace("
", "\n")
actions = re.findall(r"• (.+)", text)
return actions if actions else ["No action"]
'''
# Drilldown DataFrame
drilldown_data = []
for _, row in results_df.iterrows():
churn_pred = row["Churn Prediction"]
prob= row["Probability"]
clusters = row["Clusters"]
if isinstance(clusters, dict):
for cluster_name, rec_list in clusters.items():
for rec in rec_list:
drilldown_data.append({
"Churn Prediction": churn_pred,
"Cluster": cluster_name,
"Recommendation": rec,
"Probability": prob
})
drilldown_df = pd.DataFrame(drilldown_data)
# Chart defaults
px.defaults.template = "plotly_white"
px.defaults.color_discrete_sequence = px.colors.qualitative.Set2
# Helper for numeric KPI cards
def make_numeric_card(title, value, suffix=""):
fig = go.Figure(go.Indicator(
mode="number",
value=value,
number={'font': {'size': 36}, 'suffix': suffix},
title={'text': title, 'font': {'size': 16}}
))
fig.update_layout(
height=140, width=220,
margin=dict(l=10, r=10, t=25, b=10),
paper_bgcolor="white"
)
return fig
# Build Analytics Figures
def generate_analytics_figs(df):
figs = {}
figs["avg_tenure"] = make_numeric_card("Avg Tenure (Months)", df["Tenure in Months"].mean())
figs["avg_charge"] = make_numeric_card("Avg Monthly Charge ($)", df["Monthly Charge"].mean(), suffix="$")
figs["avg_data"] = make_numeric_card("Avg Monthly GB Download", df["Avg Monthly GB Download"].mean())
figs["tenure_hist"] = px.histogram(df, x="Tenure in Months", nbins=20, title="Tenure Distribution")
figs["age_hist"] = px.histogram(df, x="Age", nbins=20, title="Age Distribution")
figs["contract_pie"] = px.pie(df, names="Contract", hole=0.4, title="Contract Types")
figs["dependents_donut"] = px.pie(df, names="Number of Dependents", hole=0.4, title="Dependents Distribution")
figs["senior_pie"] = px.pie(df, names="Senior Citizen", hole=0.4, title="Senior Citizen Ratio")
figs["streaming_pie"] = px.pie(df, names="Streaming Movies", hole=0.4, title="Streaming Movies Adoption")
figs["security_pie"] = px.pie(df, names="Online Security", hole=0.4, title="Online Security Subscription")
figs["unlimited_pie"] = px.pie(df, names="Unlimited Data", hole=0.4, title="Unlimited Data Adoption")
referrals_df = df["Number of Referrals"].value_counts().reset_index()
referrals_df.columns = ["Number of Referrals", "count"]
figs["referrals_bar"] = px.bar(
referrals_df,
x="Number of Referrals",
y="count",
title="Referrals Count Distribution",
labels={"Number of Referrals": "Number of Referrals", "count": "Count"}
)
for f in figs.values():
f.update_layout(margin=dict(l=20, r=20, t=40, b=20))
return figs
analytics_figs = generate_analytics_figs(results_df)
# Drilldown Chart
def generate_drilldown_chart():
if drilldown_df.empty:
fig = px.treemap(names=["No data"], parents=[""], values=[1], title="No recommendations yet")
return fig
fig = px.treemap(
drilldown_df,
path=['Churn Prediction', 'Cluster', 'Recommendation'],
values=None,
title="Recommendations by Churn Prediction & Cluster",
)
fig.update_traces(
textinfo='label+percent parent',
hovertemplate="%{label}
Customers: %{value}
Percentage of parent: %{percentParent:.1%}
• ".join(rec_lines) if rec_lines else "No recommendations"
else:
recommendations_html = "No recommendations"
selected_features = [
'Contract', 'Tenure in Months', 'Age', 'Number of Referrals',
'Monthly Charge', 'Number of Dependents', 'Senior Citizen',
'Streaming Movies', 'Online Security', 'Avg Monthly GB Download',
'Unlimited Data'
]
# Table with customer features
table_html = "
| {col} | " table_html += f"{row[col]} |