File size: 1,869 Bytes
a4b813b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
import seaborn as sns
import matplotlib.pyplot as plt

# Load Titanic dataset
df = sns.load_dataset("titanic")

# --- Visualization functions ---

def show_data(rows: int = 5):
    """Show top N rows of dataset."""
    return df.head(rows)

def plot_survival_by_class():
    """Bar plot: survival count by passenger class."""
    fig, ax = plt.subplots()
    sns.countplot(data=df, x="pclass", hue="survived", ax=ax)
    ax.set_title("Survival Count by Passenger Class")
    ax.set_xlabel("Passenger Class")
    ax.set_ylabel("Count")
    return fig

def plot_gender_distribution():
    """Pie chart: gender distribution."""
    fig, ax = plt.subplots()
    df["sex"].value_counts().plot.pie(
        autopct="%1.1f%%", startangle=90, colors=["#66b3ff", "#ff9999"], ax=ax
    )
    ax.set_ylabel("")
    ax.set_title("Gender Distribution")
    return fig

def plot_age_distribution():
    """Histogram: age distribution."""
    fig, ax = plt.subplots()
    df["age"].dropna().plot.hist(bins=20, edgecolor="black", ax=ax)
    ax.set_title("Age Distribution")
    ax.set_xlabel("Age")
    ax.set_ylabel("Frequency")
    return fig

# --- Gradio Interface ---

with gr.Blocks(title="Titanic Data Dashboard") as demo:
    gr.Markdown("# 🚒 Titanic Data Dashboard")
    gr.Markdown("Explore the Titanic dataset β€” view data and visualize key statistics.")

    with gr.Tab("πŸ“‹ Dataset"):
        gr.Interface(
            fn=show_data,
            inputs=gr.Slider(1, 50, value=5, step=1, label="Number of rows"),
            outputs=gr.Dataframe(label="Titanic Data"),
        )

    with gr.Tab("πŸ“Š Survival by Class"):
        gr.Plot(plot_survival_by_class)

    with gr.Tab("πŸ‘« Gender Distribution"):
        gr.Plot(plot_gender_distribution)

    with gr.Tab("πŸŽ‚ Age Distribution"):
        gr.Plot(plot_age_distribution)

demo.launch()