Spaces:
Sleeping
Sleeping
| # 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() | |