Spaces:
Runtime error
Runtime error
Download app.py from Suhani0409/Sample12: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/spaces/Suhani0409/Sample12/resolve/main/app.py
- Command line
-
hf download hf://spaces/Suhani0409/Sample12/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Suhani0409/Sample12/resolve/main/app.py
2.64 kB
| import gradio as gr | |
| import pandas as pd | |
| import seaborn as sns | |
| import matplotlib.pyplot as plt | |
| # Load dataset | |
| URL = "https://raw.githubusercontent.com/laxmimerit/All-CSV-ML-Data-Files-Download/master/employees.csv" | |
| df = pd.read_csv(URL) | |
| # Pre-processing / cleaning | |
| df = df.copy() | |
| # convert HIRE_DATE to datetime | |
| df["HIRE_DATE"] = pd.to_datetime(df["HIRE_DATE"], errors="coerce", dayfirst=True) | |
| # convert SALARY to numeric (if needed) | |
| df["SALARY"] = pd.to_numeric(df["SALARY"], errors="coerce") | |
| # drop rows with missing crucial fields | |
| df = df.dropna(subset=["EMPLOYEE_ID","FIRST_NAME","LAST_NAME","HIRE_DATE","SALARY"]) | |
| # Visualization functions | |
| def preview_data(n_rows: int): | |
| return df.head(n_rows) | |
| def plot_vis(choice: str): | |
| plt.figure(figsize=(8,5)) | |
| if choice == "Salary Distribution": | |
| sns.histplot(df["SALARY"], bins=30, kde=True) | |
| plt.title("Distribution of Salaries") | |
| elif choice == "Hires Over Time": | |
| hires = df.set_index("HIRE_DATE").resample("Y").size() | |
| sns.lineplot(x=hires.index.year, y=hires.values) | |
| plt.title("Number of Hires Over Years") | |
| plt.xlabel("Year") | |
| plt.ylabel("Count") | |
| elif choice == "Top Job IDs by Count": | |
| top_jobs = df["JOB_ID"].value_counts().head(10) | |
| sns.barplot(x=top_jobs.values, y=top_jobs.index) | |
| plt.title("Top 10 JOB_IDs by Employee Count") | |
| plt.xlabel("Count") | |
| plt.ylabel("JOB_ID") | |
| elif choice == "Salary vs Department": | |
| dept_salary = df.groupby("DEPARTMENT_ID")["SALARY"].mean().sort_values(ascending=False).head(10) | |
| sns.barplot(x=dept_salary.values, y=dept_salary.index) | |
| plt.title("Average Salary by Department (Top10)") | |
| plt.xlabel("Avg Salary") | |
| plt.ylabel("Department ID") | |
| plt.tight_layout() | |
| return plt.gcf() | |
| # Build Gradio interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# π₯ Employee Data Explorer") | |
| gr.Markdown("Visualize and explore the employees dataset.") | |
| with gr.Tab("π Data Preview"): | |
| n = gr.Slider(5, 100, value=10, step=5, label="Number of rows to show") | |
| table = gr.Dataframe(label="Dataset Preview") | |
| n.change(preview_data, inputs=n, outputs=table) | |
| with gr.Tab("π Visualizations"): | |
| vis_choice = gr.Radio( | |
| ["Salary Distribution", "Hires Over Time", "Top Job IDs by Count", "Salary vs Department"], | |
| label="Select a visualization" | |
| ) | |
| plot = gr.Plot(label="Visualization") | |
| vis_choice.change(plot_vis, inputs=vis_choice, outputs=plot) | |
| gr.Markdown("Built with Gradio + seaborn + matplotlib") | |
| demo.launch() | |