Spaces:
No application file
No application file
Download suhani.py from Suhani0409/apps: direct link, hf CLI and curl.
- Browser
- Download file 1.87 kB
-
https://huggingface.co/spaces/Suhani0409/apps/resolve/main/suhani.py
- Command line
-
hf download hf://spaces/Suhani0409/apps/suhani.py
-
curl -L -o suhani.py https://huggingface.co/spaces/Suhani0409/apps/resolve/main/suhani.py
1.87 kB
| 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() | |