File size: 1,344 Bytes
aa7d0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt

from transformers import pipeline

analyis = pipeline(
    "text-classification", 
    model="distilbert/distilbert-base-uncased-finetuned-sst-2-english"
)

def sentiment_analysis(review):
    sentiment = analyis(review)
    return sentiment[0]['label']

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%%', color=['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):
    df = pd.read_excel(file_object)

    if 'Reviews' not in df.columns:
        raise ValueError("Excel file must contain a 'Review' column")

    df['Sentiment'] = df['Reviews'].apply(sentiment_analysis)
    chart_object = sentiment_bar_chart(df)
    return df, chart_object

demo = gr.Interface(
    fn=sentiment_analysis,
    inputs=[gr.File(file_types=["xlsx"], label="Input users reviews")],
    outputs=[gr.DataFrame(label="User sentiment"), gr.Plot(label="Users' Sentiments")],
    title="@caesar-2series: Users' Sentiment Analysis",
    description="User Sentiment Review Analysis Based on File Uploaded"
)

demo.launch()