# 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 = """
Department | Total Employees | Positive | Neutral | Negative |
---|---|---|---|---|
{row["Department"]} | {row["Total Employees"]} | {row["positive"]} | {row["neutral"]} | {row["negative"]} |