Spaces:
Sleeping
Sleeping
File size: 4,704 Bytes
b6e85e1 97f014a b6e85e1 97f014a b6e85e1 97f014a b6e85e1 22cbe8d b6e85e1 22cbe8d b6e85e1 22cbe8d b6e85e1 97f014a b6e85e1 22cbe8d b6e85e1 22cbe8d b6e85e1 |
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# app.py
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
def load_csv(file):
global df
try:
df = pd.read_csv(file.name)
required_cols = {"Name", "Age", "Salary", "Performance Score"}
if not required_cols.issubset(df.columns):
return None, f"β CSV must contain {required_cols}"
# Add Age Group
df["Age Group"] = pd.cut(
df["Age"],
bins=[20, 25, 30, 35, 40],
labels=["21-25", "26-30", "31-35", "36-40"]
)
return df, "β
File loaded successfully!"
except Exception as e:
return None, f"β Error: {e}"
def show_analysis():
if df.empty:
return "β οΈ Please load a CSV first!"
avg_salary = df["Salary"].mean()
top_salary = df.loc[df["Salary"].idxmax(), "Name"]
top_perf = df.loc[df["Performance Score"].idxmax(), "Name"]
return f"""
π **Average Salary:** {avg_salary:.2f}
π° **Highest Salary Holder:** {top_salary}
π **Top Performer:** {top_perf}
"""
def salary_chart():
if df.empty:
return None
plt.figure(figsize=(8, 5))
plt.bar(df["Name"], df["Salary"], color="skyblue", edgecolor="black")
plt.title("Employee Salaries", fontsize=14)
plt.ylabel("Salary")
plt.xticks(rotation=45)
plt.grid(axis="y", linestyle="--", alpha=0.7)
plt.tight_layout()
filepath = "salary_chart.png"
plt.savefig(filepath)
plt.close()
return filepath
def performance_chart():
if df.empty:
return None
plt.figure(figsize=(8, 5))
plt.plot(df["Name"], df["Performance Score"], marker="o",
color="green", linewidth=2, markersize=8)
plt.title("Employee Performance Scores", fontsize=14)
plt.ylabel("Performance Score")
plt.xticks(rotation=45)
plt.grid(True, linestyle="--", alpha=0.6)
plt.tight_layout()
filepath = "performance_chart.png"
plt.savefig(filepath)
plt.close()
return filepath
def search_employee(query):
if df.empty:
return None
if not query.strip():
return df
return df[df["Name"].str.lower().str.contains(query.lower())]
def show_missing():
if df.empty:
return "β οΈ Please load a CSV first!"
missing = df.isnull().sum()
percent = (df.isnull().mean() * 100).round(2)
report = "### π Missing Values Report\n\n"
for col in df.columns:
report += f"- **{col}** β {missing[col]} missing ({percent[col]}%)\n"
return report
def fill_missing():
global df
if df.empty:
return None, "β οΈ Please load a CSV first!"
# Fill numeric cols with mean, categorical with mode
for col in df.columns:
if df[col].dtype in ["float64", "int64"]:
df[col] = df[col].fillna(df[col].mean())
else:
df[col] = df[col].fillna(df[col].mode()[0] if not df[col].mode().empty else "Unknown")
return df, "β
Missing values filled (mean for numeric, mode for categorical)"
# Build Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## β¨ Employee Data Analysis Dashboard β¨")
with gr.Row():
file_input = gr.File(label="π Upload CSV", file_types=[".csv"])
status = gr.Textbox(label="Status", interactive=False)
with gr.Row():
search_box = gr.Textbox(label="π Search Employee")
analysis_btn = gr.Button("π Show Analysis")
data_table = gr.Dataframe(
headers=["Name", "Age", "Salary", "Performance Score", "Age Group"],
label="Employee Data"
)
analysis_output = gr.Markdown()
with gr.Row():
salary_btn = gr.Button("π° Salary Chart")
performance_btn = gr.Button("π Performance Chart")
salary_plot = gr.Image(type="filepath", label="Salary Chart")
performance_plot = gr.Image(type="filepath", label="Performance Chart")
# π Missing value section
with gr.Row():
missing_btn = gr.Button("π Show Missing Values")
fill_btn = gr.Button("π Fill Missing Values")
missing_output = gr.Markdown()
# Events
file_input.change(load_csv, inputs=file_input, outputs=[data_table, status])
search_box.submit(search_employee, inputs=search_box, outputs=data_table)
analysis_btn.click(show_analysis, inputs=None, outputs=analysis_output)
salary_btn.click(salary_chart, inputs=None, outputs=salary_plot)
performance_btn.click(performance_chart, inputs=None, outputs=performance_plot)
missing_btn.click(show_missing, inputs=None, outputs=missing_output)
fill_btn.click(fill_missing, inputs=None, outputs=[data_table, status])
if __name__ == "__main__":
demo.launch()
|