File size: 4,912 Bytes
0d9356b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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()