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()