Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import seaborn as sns
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
# Load dataset
|
| 7 |
+
URL = "https://raw.githubusercontent.com/laxmimerit/All-CSV-ML-Data-Files-Download/master/employees.csv"
|
| 8 |
+
df = pd.read_csv(URL)
|
| 9 |
+
|
| 10 |
+
# Pre-processing / cleaning
|
| 11 |
+
df = df.copy()
|
| 12 |
+
# convert HIRE_DATE to datetime
|
| 13 |
+
df["HIRE_DATE"] = pd.to_datetime(df["HIRE_DATE"], errors="coerce", dayfirst=True)
|
| 14 |
+
# convert SALARY to numeric (if needed)
|
| 15 |
+
df["SALARY"] = pd.to_numeric(df["SALARY"], errors="coerce")
|
| 16 |
+
# drop rows with missing crucial fields
|
| 17 |
+
df = df.dropna(subset=["EMPLOYEE_ID","FIRST_NAME","LAST_NAME","HIRE_DATE","SALARY"])
|
| 18 |
+
|
| 19 |
+
# Visualization functions
|
| 20 |
+
def preview_data(n_rows: int):
|
| 21 |
+
return df.head(n_rows)
|
| 22 |
+
|
| 23 |
+
def plot_vis(choice: str):
|
| 24 |
+
plt.figure(figsize=(8,5))
|
| 25 |
+
if choice == "Salary Distribution":
|
| 26 |
+
sns.histplot(df["SALARY"], bins=30, kde=True)
|
| 27 |
+
plt.title("Distribution of Salaries")
|
| 28 |
+
elif choice == "Hires Over Time":
|
| 29 |
+
hires = df.set_index("HIRE_DATE").resample("Y").size()
|
| 30 |
+
sns.lineplot(x=hires.index.year, y=hires.values)
|
| 31 |
+
plt.title("Number of Hires Over Years")
|
| 32 |
+
plt.xlabel("Year")
|
| 33 |
+
plt.ylabel("Count")
|
| 34 |
+
elif choice == "Top Job IDs by Count":
|
| 35 |
+
top_jobs = df["JOB_ID"].value_counts().head(10)
|
| 36 |
+
sns.barplot(x=top_jobs.values, y=top_jobs.index)
|
| 37 |
+
plt.title("Top 10 JOB_IDs by Employee Count")
|
| 38 |
+
plt.xlabel("Count")
|
| 39 |
+
plt.ylabel("JOB_ID")
|
| 40 |
+
elif choice == "Salary vs Department":
|
| 41 |
+
dept_salary = df.groupby("DEPARTMENT_ID")["SALARY"].mean().sort_values(ascending=False).head(10)
|
| 42 |
+
sns.barplot(x=dept_salary.values, y=dept_salary.index)
|
| 43 |
+
plt.title("Average Salary by Department (Top10)")
|
| 44 |
+
plt.xlabel("Avg Salary")
|
| 45 |
+
plt.ylabel("Department ID")
|
| 46 |
+
plt.tight_layout()
|
| 47 |
+
return plt.gcf()
|
| 48 |
+
|
| 49 |
+
# Build Gradio interface
|
| 50 |
+
with gr.Blocks() as demo:
|
| 51 |
+
gr.Markdown("# 👥 Employee Data Explorer")
|
| 52 |
+
gr.Markdown("Visualize and explore the employees dataset.")
|
| 53 |
+
|
| 54 |
+
with gr.Tab("📋 Data Preview"):
|
| 55 |
+
n = gr.Slider(5, 100, value=10, step=5, label="Number of rows to show")
|
| 56 |
+
table = gr.Dataframe(label="Dataset Preview")
|
| 57 |
+
n.change(preview_data, inputs=n, outputs=table)
|
| 58 |
+
|
| 59 |
+
with gr.Tab("📊 Visualizations"):
|
| 60 |
+
vis_choice = gr.Radio(
|
| 61 |
+
["Salary Distribution", "Hires Over Time", "Top Job IDs by Count", "Salary vs Department"],
|
| 62 |
+
label="Select a visualization"
|
| 63 |
+
)
|
| 64 |
+
plot = gr.Plot(label="Visualization")
|
| 65 |
+
vis_choice.change(plot_vis, inputs=vis_choice, outputs=plot)
|
| 66 |
+
|
| 67 |
+
gr.Markdown("Built with Gradio + seaborn + matplotlib")
|
| 68 |
+
|
| 69 |
+
demo.launch()
|