File size: 2,289 Bytes
e1363df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8dd216d
e1363df
 
 
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
# Use a pipeline as a high-level helper
from transformers import pipeline
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt

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

# model_path = ("./Models/models--distilbert--distilbert-base-uncased-finetuned-sst-2-english/snapshots/714eb0fa89d2f80546fda750413ed43d93601a13")

# analyser = pipeline("text-classification", model=model_path)

# print(analyser(["This product is good!", "This product is expensive!"]))


def sentiment_analysis(text_to_review):
    sentiment = analyser(text_to_review)
    return sentiment[0]['label']

# print(sentiment_analysis(["This product is good!", "This product is expensive!"]))

def plot_sentiment_pie(df):
    # Count the number of positive and negative reviews
    sentiment_counts = df['Sentiment'].value_counts()
    
    # Create the pie chart
    fig, ax = plt.subplots(figsize=(6, 6))
    ax.pie(sentiment_counts.values, labels=sentiment_counts.index, autopct='%1.1f%%')
    ax.set_title('Sentiment Distribution')
    
    # Convert the Matplotlib figure to a Gradio Plots component
    return fig

def read_excel_and_get_sentiment(file):
    try:
        df = pd.read_excel(file)
        if 'Review' not in df.columns:
            raise KeyError("'Review' column not found in the Excel file.")
        df['Sentiment'] = df['Review'].apply(sentiment_analysis)

        chart_object = plot_sentiment_pie(df)

        return df, chart_object
    except FileNotFoundError:
        print(f"Error: {file} not found.")
        raise
    except Exception as e:
        print(f"Error: {e}")
        raise

gr.close_all()

demo = gr.Interface(fn=read_excel_and_get_sentiment,
                    inputs=[gr.File(file_types= ['xlsx'],label="upload your review comment excel file.")],
                    outputs=[gr.DataFrame(label="Reviewed text"), gr.Plot(label="Sentiment Analysis")],
                    title="@IT AI Enthusiast (https://www.youtube.com/@itaienthusiast/) - Sentiment Analysis",
                    description="THIS APPLICATION WILL BE USED TO ANALYZER THE SENTIMENT BASED ON THE COMMENT PROVIDER.",
                    theme=gr.themes.Soft(),
                    concurrency_limit=16)
demo.launch()