# exclude Executive import gradio as gr import pandas as pd import matplotlib.pyplot as plt from transformers import pipeline # Load the sentiment analysis model (supports neutral feedback) pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest") # Function to process CSV and generate dropdown options # Function to process CSV and generate dropdown options def process_csv(file): df = pd.read_csv(file.name) # Exclude the 'Executive' department from the department list departments = ['All'] + [dept for dept in df['Department'].unique().tolist() if dept != 'Executive'] # Include all questions questions = ['All'] + df['Question'].unique().tolist() return gr.update(choices=departments), gr.update(choices=questions) # Function to analyze sentiments and generate a styled HTML table # Function to analyze sentiments and generate a styled HTML table def get_analysis(department, question, file): df = pd.read_csv(file.name) # Exclude the Executive department df = df[df['Department'] != 'Executive'] # Filter by department and question if department != 'All': df = df[df['Department'] == department] if question != 'All': df = df[df['Question'] == question] # Perform sentiment analysis df['Sentiment'] = df['Feedback'].apply(lambda x: pipe(x)[0]['label']) # Count employees per department employee_counts = df.groupby('Department')['Employee'].nunique().reset_index() employee_counts.columns = ['Department', 'Total Employees'] # Count positive, neutral, and negative feedback per department sentiment_counts = df.groupby(['Department', 'Sentiment']).size().unstack(fill_value=0) sentiment_counts = sentiment_counts.reindex(columns=['positive', 'neutral', 'negative'], fill_value=0) # Merge employee counts with sentiment counts summary_df = employee_counts.merge(sentiment_counts, on="Department", how="left") # Add a row for total counts total_row = pd.DataFrame({ 'Department': ['Total'], 'Total Employees': [df['Employee'].nunique()], 'positive': [summary_df['positive'].sum()], 'neutral': [summary_df['neutral'].sum()], 'negative': [summary_df['negative'].sum()] }) summary_df = pd.concat([summary_df, total_row], ignore_index=True) # **Generate HTML Table with Colors** def generate_html_table(df): html = """ """ for _, row in df.iterrows(): row_class = "total" if row["Department"] == "Total" else "" html += f""" """ html += "
DepartmentTotal Employees PositiveNeutralNegative
{row["Department"]} {row["Total Employees"]} {row["positive"]} {row["neutral"]} {row["negative"]}
" return html table_html = generate_html_table(summary_df) # **Plot department-wise sentiment distribution** plt.figure(figsize=(10, 6)) sentiment_counts.plot(kind='bar', stacked=False, color=['green', 'gray', 'red'], width=0.6) plt.xlabel("Department") plt.ylabel("Count") plt.title("Department-wise Sentiment Analysis") plt.xticks(rotation=45) plt.legend(title="Sentiment", loc="upper right") plt.grid(axis='y', linestyle='--', alpha=0.7) plt.tight_layout() return table_html, plt.gcf() # **Gradio UI** with gr.Blocks() as demo: gr.Markdown("### Employee Sentiment Analysis") file_input = gr.File(label="Upload CSV", file_types=[".csv"]) process_button = gr.Button("Process CSV") dept_dropdown = gr.Dropdown(label="Department", choices=["All"], interactive=True) question_dropdown = gr.Dropdown(label="Question", choices=["All"], interactive=True) analyze_button = gr.Button("Get Analysis") output_table = gr.HTML(label="Department-wise Sentiment Summary") output_plot = gr.Plot(label="Department-wise Sentiment Distribution") process_button.click(process_csv, inputs=file_input, outputs=[dept_dropdown, question_dropdown]) analyze_button.click(get_analysis, inputs=[dept_dropdown, question_dropdown, file_input], outputs=[output_table, output_plot]) demo.launch(share=True)