Spaces:
No application file
No application file
Create suhani.py
Browse files
suhani.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import seaborn as sns
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
|
| 5 |
+
# Load Titanic dataset
|
| 6 |
+
df = sns.load_dataset("titanic")
|
| 7 |
+
|
| 8 |
+
# --- Visualization functions ---
|
| 9 |
+
|
| 10 |
+
def show_data(rows: int = 5):
|
| 11 |
+
"""Show top N rows of dataset."""
|
| 12 |
+
return df.head(rows)
|
| 13 |
+
|
| 14 |
+
def plot_survival_by_class():
|
| 15 |
+
"""Bar plot: survival count by passenger class."""
|
| 16 |
+
fig, ax = plt.subplots()
|
| 17 |
+
sns.countplot(data=df, x="pclass", hue="survived", ax=ax)
|
| 18 |
+
ax.set_title("Survival Count by Passenger Class")
|
| 19 |
+
ax.set_xlabel("Passenger Class")
|
| 20 |
+
ax.set_ylabel("Count")
|
| 21 |
+
return fig
|
| 22 |
+
|
| 23 |
+
def plot_gender_distribution():
|
| 24 |
+
"""Pie chart: gender distribution."""
|
| 25 |
+
fig, ax = plt.subplots()
|
| 26 |
+
df["sex"].value_counts().plot.pie(
|
| 27 |
+
autopct="%1.1f%%", startangle=90, colors=["#66b3ff", "#ff9999"], ax=ax
|
| 28 |
+
)
|
| 29 |
+
ax.set_ylabel("")
|
| 30 |
+
ax.set_title("Gender Distribution")
|
| 31 |
+
return fig
|
| 32 |
+
|
| 33 |
+
def plot_age_distribution():
|
| 34 |
+
"""Histogram: age distribution."""
|
| 35 |
+
fig, ax = plt.subplots()
|
| 36 |
+
df["age"].dropna().plot.hist(bins=20, edgecolor="black", ax=ax)
|
| 37 |
+
ax.set_title("Age Distribution")
|
| 38 |
+
ax.set_xlabel("Age")
|
| 39 |
+
ax.set_ylabel("Frequency")
|
| 40 |
+
return fig
|
| 41 |
+
|
| 42 |
+
# --- Gradio Interface ---
|
| 43 |
+
|
| 44 |
+
with gr.Blocks(title="Titanic Data Dashboard") as demo:
|
| 45 |
+
gr.Markdown("# π’ Titanic Data Dashboard")
|
| 46 |
+
gr.Markdown("Explore the Titanic dataset β view data and visualize key statistics.")
|
| 47 |
+
|
| 48 |
+
with gr.Tab("π Dataset"):
|
| 49 |
+
gr.Interface(
|
| 50 |
+
fn=show_data,
|
| 51 |
+
inputs=gr.Slider(1, 50, value=5, step=1, label="Number of rows"),
|
| 52 |
+
outputs=gr.Dataframe(label="Titanic Data"),
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
with gr.Tab("π Survival by Class"):
|
| 56 |
+
gr.Plot(plot_survival_by_class)
|
| 57 |
+
|
| 58 |
+
with gr.Tab("π« Gender Distribution"):
|
| 59 |
+
gr.Plot(plot_gender_distribution)
|
| 60 |
+
|
| 61 |
+
with gr.Tab("π Age Distribution"):
|
| 62 |
+
gr.Plot(plot_age_distribution)
|
| 63 |
+
|
| 64 |
+
demo.launch()
|