File size: 1,796 Bytes
370ca4d
 
 
 
 
ad1f13f
5cf0139
5b11708
370ca4d
 
5b11708
 
 
 
ad1f13f
5b11708
370ca4d
 
 
 
0e72e73
370ca4d
 
 
 
 
 
5b11708
 
 
 
 
 
 
 
ad1f13f
5b11708
370ca4d
0e72e73
370ca4d
 
 
 
 
0e72e73
 
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
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
from transformers import pipeline

# Use a pipeline with a suitable model for sentiment analysis
model_name = "distilbert/distilbert-base-uncased-finetuned-sst-2-english"
analyzer = pipeline("text-classification", model=model_name)

def sentiment_analyzer(review):
    try:
        sentiment = analyzer(review)
        return sentiment[0]['label']
    except Exception as e:
        print(f"Error in sentiment_analyzer: {e}")
        return f"Error: {e}"

def sentiment_bar_chart(df):
    sentiment_counts = df['Sentiment'].value_counts()
    fig, ax = plt.subplots()
    sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', colors=['green', 'red'])
    ax.set_title('Review Sentiment Counts')
    ax.set_xlabel('Sentiment')
    ax.set_ylabel('Count')
    return fig

def read_reviews_and_analyze_sentiment(file_object):
    try:
        df = pd.read_excel(file_object)
        if 'Reviews' not in df.columns:
            raise ValueError("Excel file must contain a 'Reviews' column.")
        df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
        chart_object = sentiment_bar_chart(df)
        return df, chart_object
    except Exception as e:
        print(f"Error in read_reviews_and_analyze_sentiment: {e}")
        return f"Error: {e}", None

gr.close_all()

demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
                    inputs=[gr.File(file_types=["xlsx"], label="Upload your review comment file")],
                    outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Analysis")],
                    title="Sentiment Analyzer",
                    description="This application will be used to analyze the sentiment based on the uploaded file.")
demo.launch()