Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,26 +4,32 @@ import numpy as np
|
|
4 |
from sklearn.model_selection import train_test_split
|
5 |
from sklearn.ensemble import RandomForestRegressor
|
6 |
from sklearn.preprocessing import StandardScaler
|
7 |
-
import
|
|
|
8 |
|
9 |
# Load and preprocess data
|
10 |
-
@st.cache_data
|
11 |
def load_data():
|
12 |
-
#
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
return data
|
15 |
|
16 |
# Train model
|
17 |
-
@st.cache_resource
|
18 |
def train_model(data):
|
19 |
-
X = data.drop('performance_score', axis=1)
|
20 |
y = data['performance_score']
|
21 |
|
22 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
23 |
|
24 |
scaler = StandardScaler()
|
25 |
X_train_scaled = scaler.fit_transform(X_train)
|
26 |
-
X_test_scaled = scaler.transform(X_test)
|
27 |
|
28 |
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
29 |
model.fit(X_train_scaled, y_train)
|
@@ -32,18 +38,32 @@ def train_model(data):
|
|
32 |
|
33 |
# Streamlit app
|
34 |
def main():
|
35 |
-
st.title('Employee Performance
|
36 |
|
37 |
data = load_data()
|
38 |
model, scaler = train_model(data)
|
39 |
|
40 |
-
st.sidebar.header('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
47 |
|
48 |
# Create a dataframe with user input
|
49 |
user_input = pd.DataFrame({
|
@@ -59,18 +79,48 @@ def main():
|
|
59 |
# Make prediction
|
60 |
prediction = model.predict(user_input_scaled)
|
61 |
|
62 |
-
|
63 |
-
st.
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
st.subheader('Performance Interpretation')
|
66 |
if prediction[0] >= 90:
|
67 |
-
st.write("Excellent performance!")
|
68 |
elif prediction[0] >= 75:
|
69 |
-
st.write("Good performance")
|
70 |
elif prediction[0] >= 60:
|
71 |
-
st.write("Average performance")
|
72 |
else:
|
73 |
-
st.write("
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
if __name__ == '__main__':
|
76 |
main()
|
|
|
4 |
from sklearn.model_selection import train_test_split
|
5 |
from sklearn.ensemble import RandomForestRegressor
|
6 |
from sklearn.preprocessing import StandardScaler
|
7 |
+
import plotly.express as px
|
8 |
+
import plotly.graph_objects as go
|
9 |
|
10 |
# Load and preprocess data
|
|
|
11 |
def load_data():
|
12 |
+
# For this example, we'll generate synthetic data
|
13 |
+
np.random.seed(42)
|
14 |
+
data = pd.DataFrame({
|
15 |
+
'employee_id': range(1000),
|
16 |
+
'age': np.random.randint(22, 65, 1000),
|
17 |
+
'experience': np.random.randint(0, 40, 1000),
|
18 |
+
'training_score': np.random.uniform(50, 100, 1000),
|
19 |
+
'project_completion': np.random.uniform(60, 100, 1000),
|
20 |
+
'performance_score': np.random.uniform(50, 100, 1000)
|
21 |
+
})
|
22 |
return data
|
23 |
|
24 |
# Train model
|
|
|
25 |
def train_model(data):
|
26 |
+
X = data.drop(['employee_id', 'performance_score'], axis=1)
|
27 |
y = data['performance_score']
|
28 |
|
29 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
30 |
|
31 |
scaler = StandardScaler()
|
32 |
X_train_scaled = scaler.fit_transform(X_train)
|
|
|
33 |
|
34 |
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
35 |
model.fit(X_train_scaled, y_train)
|
|
|
38 |
|
39 |
# Streamlit app
|
40 |
def main():
|
41 |
+
st.title('Employee Performance Analysis Dashboard')
|
42 |
|
43 |
data = load_data()
|
44 |
model, scaler = train_model(data)
|
45 |
|
46 |
+
st.sidebar.header('Employee Details')
|
47 |
+
|
48 |
+
# Input for employee ID
|
49 |
+
employee_id = st.sidebar.number_input('Employee ID', min_value=1, max_value=1000, value=1)
|
50 |
+
|
51 |
+
# Get employee data
|
52 |
+
employee = data[data['employee_id'] == employee_id].iloc[0]
|
53 |
+
|
54 |
+
# Display current employee details
|
55 |
+
st.sidebar.subheader('Current Details')
|
56 |
+
st.sidebar.write(f"Age: {employee['age']}")
|
57 |
+
st.sidebar.write(f"Experience: {employee['experience']} years")
|
58 |
+
st.sidebar.write(f"Training Score: {employee['training_score']:.2f}")
|
59 |
+
st.sidebar.write(f"Project Completion: {employee['project_completion']:.2f}%")
|
60 |
|
61 |
+
# Input fields for updating employee details
|
62 |
+
st.sidebar.subheader('Update Details')
|
63 |
+
age = st.sidebar.number_input('Age', min_value=22, max_value=65, value=int(employee['age']))
|
64 |
+
experience = st.sidebar.number_input('Years of Experience', min_value=0, max_value=40, value=int(employee['experience']))
|
65 |
+
training_score = st.sidebar.slider('Training Score', 50.0, 100.0, float(employee['training_score']))
|
66 |
+
project_completion = st.sidebar.slider('Project Completion Rate', 60.0, 100.0, float(employee['project_completion']))
|
67 |
|
68 |
# Create a dataframe with user input
|
69 |
user_input = pd.DataFrame({
|
|
|
79 |
# Make prediction
|
80 |
prediction = model.predict(user_input_scaled)
|
81 |
|
82 |
+
# Display results
|
83 |
+
col1, col2 = st.columns(2)
|
84 |
|
85 |
+
with col1:
|
86 |
+
st.subheader('Predicted Performance Score')
|
87 |
+
fig = go.Figure(go.Indicator(
|
88 |
+
mode = "gauge+number",
|
89 |
+
value = prediction[0],
|
90 |
+
domain = {'x': [0, 1], 'y': [0, 1]},
|
91 |
+
title = {'text': "Performance"},
|
92 |
+
gauge = {'axis': {'range': [None, 100]},
|
93 |
+
'steps' : [
|
94 |
+
{'range': [0, 60], 'color': "lightgray"},
|
95 |
+
{'range': [60, 80], 'color': "gray"},
|
96 |
+
{'range': [80, 100], 'color': "darkgray"}],
|
97 |
+
'threshold': {'line': {'color': "red", 'width': 4}, 'thickness': 0.75, 'value': 90}}))
|
98 |
+
st.plotly_chart(fig)
|
99 |
+
|
100 |
+
with col2:
|
101 |
+
st.subheader('Feature Importance')
|
102 |
+
importances = model.feature_importances_
|
103 |
+
feature_importance = pd.DataFrame({'feature': user_input.columns, 'importance': importances})
|
104 |
+
feature_importance = feature_importance.sort_values('importance', ascending=False)
|
105 |
+
fig = px.bar(feature_importance, x='importance', y='feature', orientation='h')
|
106 |
+
st.plotly_chart(fig)
|
107 |
+
|
108 |
+
# Performance Interpretation
|
109 |
st.subheader('Performance Interpretation')
|
110 |
if prediction[0] >= 90:
|
111 |
+
st.write("Excellent performance! This employee is exceeding expectations.")
|
112 |
elif prediction[0] >= 75:
|
113 |
+
st.write("Good performance. The employee is meeting expectations.")
|
114 |
elif prediction[0] >= 60:
|
115 |
+
st.write("Average performance. There's room for improvement.")
|
116 |
else:
|
117 |
+
st.write("Performance needs improvement. Consider additional training or support.")
|
118 |
+
|
119 |
+
# Historical Performance
|
120 |
+
st.subheader('Historical Performance')
|
121 |
+
historical_data = data[data['employee_id'] == employee_id]
|
122 |
+
fig = px.line(historical_data, x='employee_id', y='performance_score', title='Performance Over Time')
|
123 |
+
st.plotly_chart(fig)
|
124 |
|
125 |
if __name__ == '__main__':
|
126 |
main()
|