File size: 2,639 Bytes
78c5ddd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
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()