pratikshahp commited on
Commit
0a6976d
·
verified ·
1 Parent(s): 766ebdb

Create exclude-executives-app.py

Browse files
Files changed (1) hide show
  1. exclude-executives-app.py +126 -0
exclude-executives-app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # exclude Executive
2
+ import gradio as gr
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+ from transformers import pipeline
6
+
7
+ # Load the sentiment analysis model (supports neutral feedback)
8
+ pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
9
+
10
+ # Function to process CSV and generate dropdown options
11
+ # Function to process CSV and generate dropdown options
12
+ def process_csv(file):
13
+ df = pd.read_csv(file.name)
14
+
15
+ # Exclude the 'Executive' department from the department list
16
+ departments = ['All'] + [dept for dept in df['Department'].unique().tolist() if dept != 'Executive']
17
+
18
+ # Include all questions
19
+ questions = ['All'] + df['Question'].unique().tolist()
20
+
21
+ return gr.update(choices=departments), gr.update(choices=questions)
22
+
23
+
24
+ # Function to analyze sentiments and generate a styled HTML table
25
+ # Function to analyze sentiments and generate a styled HTML table
26
+ def get_analysis(department, question, file):
27
+ df = pd.read_csv(file.name)
28
+
29
+ # Exclude the Executive department
30
+ df = df[df['Department'] != 'Executive']
31
+
32
+ # Filter by department and question
33
+ if department != 'All':
34
+ df = df[df['Department'] == department]
35
+ if question != 'All':
36
+ df = df[df['Question'] == question]
37
+
38
+ # Perform sentiment analysis
39
+ df['Sentiment'] = df['Feedback'].apply(lambda x: pipe(x)[0]['label'])
40
+
41
+ # Count employees per department
42
+ employee_counts = df.groupby('Department')['Employee'].nunique().reset_index()
43
+ employee_counts.columns = ['Department', 'Total Employees']
44
+
45
+ # Count positive, neutral, and negative feedback per department
46
+ sentiment_counts = df.groupby(['Department', 'Sentiment']).size().unstack(fill_value=0)
47
+ sentiment_counts = sentiment_counts.reindex(columns=['positive', 'neutral', 'negative'], fill_value=0)
48
+
49
+ # Merge employee counts with sentiment counts
50
+ summary_df = employee_counts.merge(sentiment_counts, on="Department", how="left")
51
+
52
+ # Add a row for total counts
53
+ total_row = pd.DataFrame({
54
+ 'Department': ['Total'],
55
+ 'Total Employees': [df['Employee'].nunique()],
56
+ 'positive': [summary_df['positive'].sum()],
57
+ 'neutral': [summary_df['neutral'].sum()],
58
+ 'negative': [summary_df['negative'].sum()]
59
+ })
60
+
61
+ summary_df = pd.concat([summary_df, total_row], ignore_index=True)
62
+
63
+ # **Generate HTML Table with Colors**
64
+ def generate_html_table(df):
65
+ html = """<style>
66
+ table {width: 100%; border-collapse: collapse; font-family: Arial, sans-serif; text-align: center;}
67
+ th {background-color: gray; padding: 10px;}
68
+ td, th {border: 1px solid #ddd; padding: 8px;}
69
+ tr:nth-child(even) {background-color: #f2f2f2;}
70
+ .total {background-color: lightgray; font-weight: bold;}
71
+ .positive {color: green; font-weight: bold;}
72
+ .neutral {color: gray; font-weight: bold;}
73
+ .negative {color: red; font-weight: bold;}
74
+ </style>
75
+ <table>
76
+ <tr>
77
+ <th>Department</th><th>Total Employees</th>
78
+ <th>Positive</th><th>Neutral</th><th>Negative</th>
79
+ </tr>"""
80
+ for _, row in df.iterrows():
81
+ row_class = "total" if row["Department"] == "Total" else ""
82
+ html += f"""
83
+ <tr class="{row_class}">
84
+ <td>{row["Department"]}</td>
85
+ <td>{row["Total Employees"]}</td>
86
+ <td class="positive">{row["positive"]}</td>
87
+ <td class="neutral">{row["neutral"]}</td>
88
+ <td class="negative">{row["negative"]}</td>
89
+ </tr>"""
90
+ html += "</table>"
91
+ return html
92
+
93
+ table_html = generate_html_table(summary_df)
94
+
95
+ # **Plot department-wise sentiment distribution**
96
+ plt.figure(figsize=(10, 6))
97
+ sentiment_counts.plot(kind='bar', stacked=False, color=['green', 'gray', 'red'], width=0.6)
98
+ plt.xlabel("Department")
99
+ plt.ylabel("Count")
100
+ plt.title("Department-wise Sentiment Analysis")
101
+ plt.xticks(rotation=45)
102
+ plt.legend(title="Sentiment", loc="upper right")
103
+ plt.grid(axis='y', linestyle='--', alpha=0.7)
104
+ plt.tight_layout()
105
+
106
+ return table_html, plt.gcf()
107
+
108
+ # **Gradio UI**
109
+ with gr.Blocks() as demo:
110
+ gr.Markdown("### Employee Sentiment Analysis")
111
+
112
+ file_input = gr.File(label="Upload CSV", file_types=[".csv"])
113
+ process_button = gr.Button("Process CSV")
114
+
115
+ dept_dropdown = gr.Dropdown(label="Department", choices=["All"], interactive=True)
116
+ question_dropdown = gr.Dropdown(label="Question", choices=["All"], interactive=True)
117
+
118
+ analyze_button = gr.Button("Get Analysis")
119
+
120
+ output_table = gr.HTML(label="Department-wise Sentiment Summary")
121
+ output_plot = gr.Plot(label="Department-wise Sentiment Distribution")
122
+
123
+ process_button.click(process_csv, inputs=file_input, outputs=[dept_dropdown, question_dropdown])
124
+ analyze_button.click(get_analysis, inputs=[dept_dropdown, question_dropdown, file_input], outputs=[output_table, output_plot])
125
+
126
+ demo.launch(share=True)