logeswari commited on
Commit
1de30c8
Β·
1 Parent(s): e478a9b
Files changed (2) hide show
  1. app.py +185 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import pandas as pd
4
+ import plotly.express as px
5
+
6
+ # Backend API URL
7
+ API_BASE_URL = "https://logeswari-cap-backend.hf.space"
8
+
9
+ def fetch_data(endpoint, params=None):
10
+ """Fetch data from the FastAPI backend with optional query parameters."""
11
+ url = f"{API_BASE_URL}{endpoint}"
12
+
13
+ try:
14
+ response = requests.get(url, params=params)
15
+ response.raise_for_status()
16
+ data = response.json()
17
+
18
+ # Debugging: Print API response
19
+ print(f"Fetched from {url} | Params: {params} | Response: {data}")
20
+
21
+ # Handle single scalar values (like correlation coefficients)
22
+ if isinstance(data, dict) and len(data) == 1 and isinstance(list(data.values())[0], (int, float)):
23
+ return pd.DataFrame([data]) # Convert dictionary to DataFrame
24
+
25
+ return pd.DataFrame(data) # Convert list to DataFrame
26
+ except requests.exceptions.RequestException as e:
27
+ st.error(f"Error fetching data: {e}")
28
+ return None
29
+
30
+
31
+ # βœ… Set up Streamlit app layout
32
+ st.set_page_config(page_title="HR Analytics Dashboard", layout="wide")
33
+
34
+ # βœ… Sidebar Navigation: Two main pages
35
+ st.sidebar.title("πŸ“Š HR Analytics Dashboard")
36
+ page = st.sidebar.selectbox("Select Page", ["🏠 Home", "πŸ“ˆ Analytics Dashboard"])
37
+
38
+ # βœ… HOME PAGE
39
+ if page == "🏠 Home":
40
+ st.title("🏠 Welcome to the HR Analytics Dashboard")
41
+ st.write("Explore different HR metrics and insights.")
42
+
43
+ # βœ… ANALYTICS DASHBOARD (with 9 endpoints)
44
+ elif page == "πŸ“ˆ Analytics Dashboard":
45
+
46
+ # Select an analysis option inside the Analytics page
47
+ analysis_option = st.sidebar.selectbox("Choose an analysis:", [
48
+ "Employee Satisfaction", "Department Performance", "Training Analytics",
49
+ "Engagement-Performance Correlation", "Cost-Benefit Analysis",
50
+ "Training Effectiveness", "Diversity & Inclusion", "Work-Life Balance Impact",
51
+ "Career Development Tracking"
52
+ ])
53
+
54
+ def display_chart(data, chart_type, x_col, y_col, title):
55
+ """Display different types of charts based on user selection."""
56
+ if data is None or data.empty:
57
+ st.warning(f"No data available for {title}")
58
+ return
59
+
60
+ st.title(title)
61
+ st.dataframe(data) # Show table before visualization
62
+
63
+ if chart_type == "bar":
64
+ fig = px.bar(data, x=x_col, y=y_col, color=y_col, title=title, height=500)
65
+ elif chart_type == "scatter":
66
+ fig = px.scatter(data, x=x_col, y=y_col, color=y_col, title=title, size=y_col, height=500)
67
+ elif chart_type == "line":
68
+ fig = px.line(data, x=x_col, y=y_col, markers=True, title=title, height=500)
69
+ elif chart_type == "pie":
70
+ fig = px.pie(data, names=x_col, values=y_col, title=title)
71
+ elif chart_type == "histogram":
72
+ fig = px.histogram(data, x=x_col, title=title, nbins=10, height=500)
73
+ else:
74
+ st.warning("Invalid chart type")
75
+ return
76
+
77
+ st.plotly_chart(fig, use_container_width=True)
78
+
79
+
80
+ # βœ… Employee Satisfaction Analysis
81
+ if analysis_option == "Employee Satisfaction":
82
+ department = st.sidebar.text_input("Enter Department (optional):")
83
+ params = {"department": department} if department else None
84
+ data = fetch_data("/satisfaction-analysis", params)
85
+
86
+ if data is not None and not data.empty:
87
+ st.subheader(f"πŸ“Š Employee Satisfaction in {department if department else 'All Departments'}")
88
+ display_chart(data, "bar", "DepartmentType", "Satisfaction Score", "πŸ“Š Employee Satisfaction by Department")
89
+ else:
90
+ st.warning(f"No data available for the selected department: {department}")
91
+
92
+
93
+ # βœ… Department Performance Metrics
94
+ if analysis_option == "Department Performance":
95
+ data = fetch_data("/department-performance")
96
+ display_chart(data, "scatter", "DepartmentType", "Performance Score", "πŸ“Š Departmental Performance Metrics")
97
+
98
+
99
+ # βœ… Training Program Analytics
100
+ if analysis_option == "Training Analytics":
101
+ program_name = st.sidebar.text_input("Enter Training Program Name (optional):")
102
+ params = {"program_name": program_name} if program_name else None
103
+ data = fetch_data("/training-analytics", params)
104
+
105
+ if data is not None and not data.empty:
106
+ st.title("πŸ“Š Training Program Analytics")
107
+ st.dataframe(data)
108
+
109
+ if "Training Program Name" in data.columns:
110
+ melted_data = data.melt(id_vars="Training Program Name", var_name="Outcome", value_name="Percentage")
111
+ display_chart(melted_data, "bar", "Training Program Name", "Percentage", "πŸ“Š Training Outcomes by Program")
112
+ else:
113
+ st.warning("Training Program Name column missing in data.")
114
+
115
+
116
+ # βœ… Engagement vs. Performance Correlation
117
+ if analysis_option == "Engagement-Performance Correlation":
118
+ data = fetch_data("/engagement-performance")
119
+
120
+ if data is not None and not data.empty:
121
+ st.title("πŸ“Š Engagement vs. Performance Correlation")
122
+ correlation_value = data.iloc[0, 0] # Extract correlation coefficient
123
+ st.metric(label="Correlation Coefficient", value=f"{correlation_value:.2f}") # Display metric
124
+
125
+
126
+ # βœ… Cost-Benefit Analysis
127
+ if analysis_option == "Cost-Benefit Analysis":
128
+ data = fetch_data("/cost-benefit-analysis")
129
+ display_chart(data, "pie", "DepartmentType", "ROI", "πŸ“Š Cost-Benefit Analysis")
130
+
131
+
132
+ # βœ… Training Effectiveness Metrics
133
+ if analysis_option == "Training Effectiveness":
134
+ data = fetch_data("/training-effectiveness")
135
+ display_chart(data, "line", "Training Program Name", "Performance Score", "πŸ“Š Training Effectiveness")
136
+
137
+
138
+ # βœ… Diversity and Inclusion
139
+ if analysis_option == "Diversity & Inclusion":
140
+ data = fetch_data("/diversity-inclusion")
141
+
142
+ if data is not None and not data.empty:
143
+ st.title("πŸ“Š Diversity and Inclusion Dashboard")
144
+ st.dataframe(data)
145
+
146
+ # Convert to long format for plotting
147
+ melted_data = data.melt(id_vars="DepartmentType", var_name="Gender", value_name="Percentage")
148
+
149
+ # Bar chart for gender distribution by department
150
+ fig = px.bar(melted_data, x="DepartmentType", y="Percentage", color="Gender", barmode="group",
151
+ title="πŸ“Š Diversity Breakdown by Department", height=500)
152
+ st.plotly_chart(fig, use_container_width=True)
153
+
154
+
155
+ # βœ… Work-Life Balance Impact
156
+ if analysis_option == "Work-Life Balance Impact":
157
+ data = fetch_data("/work-life-balance")
158
+
159
+ if data is not None and not data.empty:
160
+ st.title("πŸ“Š Work-Life Balance vs. Performance Correlation")
161
+ correlation_value = data.iloc[0, 0] if isinstance(data, pd.DataFrame) else data["correlation_coefficient"]
162
+ st.metric(label="Correlation Coefficient", value=f"{correlation_value:.2f}")
163
+
164
+
165
+ # βœ… Career Development Tracking
166
+ if analysis_option == "Career Development Tracking":
167
+ st.title("πŸ“Š Career Development Tracking")
168
+
169
+ all_employees = fetch_data("/career-development")
170
+
171
+ if all_employees is not None and not all_employees.empty:
172
+ employee_ids = all_employees["Employee ID"].unique().tolist()
173
+ selected_employee = st.sidebar.selectbox("Select Employee ID:", [""] + employee_ids)
174
+ else:
175
+ selected_employee = None
176
+
177
+ params = {"employee_id": selected_employee} if selected_employee else None
178
+ data = fetch_data("/career-development", params)
179
+
180
+ if data is not None and not data.empty:
181
+ st.dataframe(data)
182
+ fig = px.histogram(data, x="Career Movements", title="πŸ“Š Career Development Histogram", nbins=10, height=500)
183
+ st.plotly_chart(fig, use_container_width=True)
184
+ else:
185
+ st.warning("No data available for the selected Employee ID.")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ requests
3
+ pandas
4
+ plotly