nikwagh commited on
Commit
1d0bfde
·
verified ·
1 Parent(s): 52999e8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Customer Churn Explorer - Gradio App
3
+ Loads churn.csv from GitHub and provides interactive visualizations.
4
+ """
5
+
6
+ import pandas as pd
7
+ import seaborn as sns
8
+ import matplotlib.pyplot as plt
9
+ import gradio as gr
10
+ import io
11
+ import requests
12
+
13
+ # --- GitHub Raw CSV URL ---
14
+ CSV_URL = "https://raw.githubusercontent.com/albayraktaroglu/Datasets/master/churn.csv"
15
+
16
+ # --- Load CSV ---
17
+ def load_csv():
18
+ """Load churn.csv from GitHub."""
19
+ try:
20
+ response = requests.get(CSV_URL)
21
+ response.raise_for_status()
22
+ df = pd.read_csv(io.StringIO(response.text))
23
+ return df
24
+ except Exception as e:
25
+ raise ValueError(f"Error loading CSV: {e}")
26
+
27
+ # --- Visualization helpers ---
28
+ def plot_churn_distribution(df):
29
+ """Plot churn vs non-churn counts."""
30
+ fig, ax = plt.subplots(figsize=(4, 3))
31
+ if "Churn" not in df.columns:
32
+ ax.text(0.5, 0.5, "No 'Churn' column found", ha="center")
33
+ return fig
34
+ df["Churn"].value_counts().plot(kind="bar", ax=ax, color=["skyblue", "salmon"])
35
+ ax.set_title("Churn vs Non-Churn Count")
36
+ ax.set_ylabel("Count")
37
+ plt.tight_layout()
38
+ return fig
39
+
40
+ def plot_corr(df):
41
+ """Plot numeric correlation heatmap."""
42
+ fig, ax = plt.subplots(figsize=(5, 4))
43
+ num = df.select_dtypes("number")
44
+ if num.empty:
45
+ ax.text(0.5, 0.5, "No numeric columns", ha="center")
46
+ return fig
47
+ sns.heatmap(num.corr(), annot=True, fmt=".2f", cmap="coolwarm", ax=ax)
48
+ ax.set_title("Numeric Correlation Heatmap")
49
+ plt.tight_layout()
50
+ return fig
51
+
52
+ def plot_categorical(df):
53
+ """Plot first categorical column distribution."""
54
+ cat_cols = df.select_dtypes("object").columns
55
+ fig, ax = plt.subplots(figsize=(5, 3))
56
+ if len(cat_cols) == 0:
57
+ ax.text(0.5, 0.5, "No categorical columns", ha="center")
58
+ return fig
59
+ col = cat_cols[0]
60
+ df[col].value_counts().head(10).plot(kind="bar", ax=ax)
61
+ ax.set_title(f"Top 10 '{col}' Values")
62
+ plt.tight_layout()
63
+ return fig
64
+
65
+
66
+ # --- Build Gradio App ---
67
+ def build_app():
68
+ df_default = load_csv()
69
+
70
+ with gr.Blocks(title="Customer Churn Explorer") as demo:
71
+ gr.Markdown(
72
+ """
73
+ # 📊 Customer Churn Explorer
74
+ Explore customer churn data interactively.
75
+ Loaded automatically from GitHub.
76
+
77
+ **Visuals included:**
78
+ - Churn vs Non-Churn count
79
+ - Numeric correlation heatmap
80
+ - Categorical value distribution
81
+ - Summary statistics
82
+ """
83
+ )
84
+
85
+ with gr.Tab("🧾 Data & Summary"):
86
+ table = gr.Dataframe(df_default.head(20), label="Data Preview")
87
+ summary = gr.Dataframe(df_default.describe(include="all").reset_index(), label="Summary Statistics")
88
+
89
+ with gr.Tab("📈 Visualizations"):
90
+ fig1 = gr.Plot(label="Churn Distribution")
91
+ fig2 = gr.Plot(label="Correlation Heatmap")
92
+ fig3 = gr.Plot(label="Categorical Distribution")
93
+
94
+ def update():
95
+ df = load_csv()
96
+ return (
97
+ df.head(50),
98
+ df.describe(include="all").reset_index(),
99
+ plot_churn_distribution(df),
100
+ plot_corr(df),
101
+ plot_categorical(df),
102
+ )
103
+
104
+ refresh_btn = gr.Button(_