import torch import gradio as gr import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # Use a pipeline as a high-level helper from transformers import pipeline # model_path = ("../Models/models--distilbert--distilbert-base-uncased-finetuned-sst-2-english/snapshots/714eb0fa89d2f80546fda750413ed43d93601a13") # analyzer = pipeline("text-classification", model=model_path) analyzer = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english") # print(analyzer(["This is good", "This product is quite expensive"])) def sentiment_analyzer(review): sentiment = analyzer(review) return sentiment[0]['label'], sentiment[0]['score'] # Return sentiment label and confidence score def create_sentiment_pie_chart(df): sentiment_counts = df['Sentiment'].value_counts() # Style the pie chart colors = ['#e67e22', '#3498db'] # Green for Positive, Red for Negative explode = [0.05, 0] # Slightly separate the positive slice for emphasis fig, ax = plt.subplots(figsize=(6, 6)) sentiment_counts.plot( kind='pie', colors=colors, autopct='%1.1f%%', startangle=140, explode=explode, ax=ax, textprops={'fontsize': 10, 'color': 'black'}, radius=0.7 # Decrease radius to make the pie smaller ) ax.set_title("Sentiment Distribution (Pie Chart)", fontsize=10, fontweight='bold', color='#34495e') ax.set_ylabel("") # Remove default 'Count' label plt.tight_layout() return fig # Create a styled bar chart def create_sentiment_bar_chart(df): sentiment_counts = df['Sentiment'].value_counts() # Use Seaborn for stylish bar plots fig, ax = plt.subplots(figsize=(8, 5)) # Use a categorical variable for hue to avoid the warning sns.barplot( x=sentiment_counts.index, y=sentiment_counts.values, # for hue warning which doesn't effect code hue=sentiment_counts.index, # Use 'Sentiment' as the hue variable palette=['#e67e22', '#3498db'], # Green for Positive, Red for Negative ax=ax, legend=False, # We don't need a legend for this plot width = 0.5 ) # Add value annotations on top of each bar for idx, value in enumerate(sentiment_counts.values): ax.text(idx, value + 0.5, str(value), ha='center', fontsize=10, color='black', fontweight='bold') ax.set_title("Sentiment Distribution (Bar Chart)", fontsize=12, fontweight='bold', color='#34495e') ax.set_xlabel("Sentiment", fontsize=10, fontweight='bold', color='#34495e') ax.set_ylabel("Count", fontsize=10, fontweight='bold', color='#34495e') ax.spines['top'].set_visible(False) # Clean up borders ax.spines['right'].set_visible(False) plt.xticks(fontsize=10, fontweight='bold', color='#34495e') plt.yticks(fontsize=10, color='#34495e') plt.tight_layout() return fig # Define the main function to process the Excel file def read_reviews_and_analyze_sentiment(file_object): # Read the Excel file into a DataFrame df = pd.read_excel(file_object) # Ensure the column containing reviews exists if 'Reviews' not in df.columns: raise ValueError("The Excel file must contain a column named 'Reviews'.") # Apply the sentiment analyzer and split into two columns df[['Sentiment', 'Confidence']] = df['Reviews'].apply( lambda x: pd.Series(sentiment_analyzer(x)) ) # Generate the pie and bar charts pie_chart = create_sentiment_pie_chart(df) bar_chart = create_sentiment_bar_chart(df) return df, pie_chart, bar_chart # Example usage: # df = process_reviews("reviews.xlsx") # print(df) # result = read_reviews_and_analyze_sentiment("../Files/Updated_Reviews_Sample.xlsx") # print(result) import gradio as gr # Custom CSS for styling the title custom_css = """ .title { font-size: 24px; font-weight: bold; color: #34495e; } .gradio-label, .gradio-description { font-size: 18px; /* Increase font size */ font-weight: bold; /* Make text bold */ } """ # Interface interface = gr.Interface( fn=read_reviews_and_analyze_sentiment, inputs=gr.File(file_types=[".xlsx"], label="Upload Excel File"), # Accept only Excel files outputs=[ gr.Dataframe( headers=["Reviews", "Sentiment", "Confidence"], # Custom headers for output label="Sentiment Analysis Results" ), gr.Plot(label="Sentiment Distribution (Pie Chart)"), gr.Plot(label="Sentiment Distribution (Bar Chart)") ], title="Sentiment Analyzer with Confidence Scores", description=( "Upload an Excel file with a 'Reviews' column to analyze sentiments and " "visualize the distribution in both pie and bar charts." ), theme="default", # Align components vertically css=custom_css # Apply the custom CSS ) interface.launch()