Spaces:
Sleeping
Sleeping
Commit Β·
b6e85e1
1
Parent(s): 2b5c277
updaed
Browse files- app.py +112 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
df = pd.DataFrame()
|
| 8 |
+
|
| 9 |
+
def load_csv(file):
|
| 10 |
+
global df
|
| 11 |
+
try:
|
| 12 |
+
df = pd.read_csv(file.name)
|
| 13 |
+
|
| 14 |
+
required_cols = {"Name", "Age", "Salary", "Performance Score"}
|
| 15 |
+
if not required_cols.issubset(df.columns):
|
| 16 |
+
return None, f"β CSV must contain {required_cols}"
|
| 17 |
+
|
| 18 |
+
# Add Age Group
|
| 19 |
+
df["Age Group"] = pd.cut(df["Age"],
|
| 20 |
+
bins=[20, 25, 30, 35, 40],
|
| 21 |
+
labels=["21-25", "26-30", "31-35", "36-40"])
|
| 22 |
+
return df, "β
File loaded successfully!"
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return None, f"β Error: {e}"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def show_analysis():
|
| 28 |
+
if df.empty:
|
| 29 |
+
return "β οΈ Please load a CSV first!"
|
| 30 |
+
avg_salary = df["Salary"].mean()
|
| 31 |
+
top_salary = df.loc[df["Salary"].idxmax(), "Name"]
|
| 32 |
+
top_perf = df.loc[df["Performance Score"].idxmax(), "Name"]
|
| 33 |
+
|
| 34 |
+
return f"""
|
| 35 |
+
π **Average Salary:** {avg_salary:.2f}
|
| 36 |
+
π° **Highest Salary Holder:** {top_salary}
|
| 37 |
+
π **Top Performer:** {top_perf}
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def salary_chart():
|
| 42 |
+
if df.empty:
|
| 43 |
+
return None
|
| 44 |
+
plt.figure(figsize=(8, 5))
|
| 45 |
+
plt.bar(df["Name"], df["Salary"], color="skyblue", edgecolor="black")
|
| 46 |
+
plt.title("Employee Salaries", fontsize=14)
|
| 47 |
+
plt.ylabel("Salary")
|
| 48 |
+
plt.xticks(rotation=45)
|
| 49 |
+
plt.grid(axis="y", linestyle="--", alpha=0.7)
|
| 50 |
+
plt.tight_layout()
|
| 51 |
+
buf = io.BytesIO()
|
| 52 |
+
plt.savefig(buf, format="png")
|
| 53 |
+
buf.seek(0)
|
| 54 |
+
return buf
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def performance_chart():
|
| 58 |
+
if df.empty:
|
| 59 |
+
return None
|
| 60 |
+
plt.figure(figsize=(8, 5))
|
| 61 |
+
plt.plot(df["Name"], df["Performance Score"], marker="o",
|
| 62 |
+
color="green", linewidth=2, markersize=8)
|
| 63 |
+
plt.title("Employee Performance Scores", fontsize=14)
|
| 64 |
+
plt.ylabel("Performance Score")
|
| 65 |
+
plt.xticks(rotation=45)
|
| 66 |
+
plt.grid(True, linestyle="--", alpha=0.6)
|
| 67 |
+
plt.tight_layout()
|
| 68 |
+
buf = io.BytesIO()
|
| 69 |
+
plt.savefig(buf, format="png")
|
| 70 |
+
buf.seek(0)
|
| 71 |
+
return buf
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def search_employee(query):
|
| 75 |
+
if df.empty:
|
| 76 |
+
return None
|
| 77 |
+
if not query.strip():
|
| 78 |
+
return df
|
| 79 |
+
return df[df["Name"].str.lower().str.contains(query.lower())]
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# Build Gradio UI
|
| 83 |
+
with gr.Blocks() as demo:
|
| 84 |
+
gr.Markdown("## β¨ Employee Data Analysis Dashboard β¨")
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
file_input = gr.File(label="π Upload CSV", file_types=[".csv"])
|
| 88 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
search_box = gr.Textbox(label="π Search Employee")
|
| 92 |
+
analysis_btn = gr.Button("π Show Analysis")
|
| 93 |
+
|
| 94 |
+
data_table = gr.Dataframe(headers=["Name", "Age", "Salary", "Performance Score", "Age Group"], label="Employee Data")
|
| 95 |
+
analysis_output = gr.Markdown()
|
| 96 |
+
|
| 97 |
+
with gr.Row():
|
| 98 |
+
salary_btn = gr.Button("π° Salary Chart")
|
| 99 |
+
performance_btn = gr.Button("π Performance Chart")
|
| 100 |
+
|
| 101 |
+
salary_plot = gr.Image(type="auto", label="Salary Chart")
|
| 102 |
+
performance_plot = gr.Image(type="auto", label="Performance Chart")
|
| 103 |
+
|
| 104 |
+
# Events
|
| 105 |
+
file_input.change(load_csv, inputs=file_input, outputs=[data_table, status])
|
| 106 |
+
search_box.submit(search_employee, inputs=search_box, outputs=data_table)
|
| 107 |
+
analysis_btn.click(show_analysis, inputs=None, outputs=analysis_output)
|
| 108 |
+
salary_btn.click(salary_chart, inputs=None, outputs=salary_plot)
|
| 109 |
+
performance_btn.click(performance_chart, inputs=None, outputs=performance_plot)
|
| 110 |
+
|
| 111 |
+
if __name__ == "__main__":
|
| 112 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
matplotlib
|