File size: 1,953 Bytes
6f352f5
 
 
 
 
626c8a8
6f352f5
 
b8973a1
6f352f5
b8973a1
6f352f5
 
 
 
 
f8a7c29
 
 
 
 
 
 
 
 
 
6f352f5
 
 
 
 
f8a7c29
 
 
6f352f5
b8973a1
6f352f5
 
 
 
 
 
 
f8a7c29
 
6f352f5
 
 
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
# Use a pipeline as a high-level helper
from transformers import pipeline
import torch
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt

#model_path = ("C:/Users/ankitdwivedi/OneDrive - Adobe/Desktop/NLP Projects/Video to Text Summarization/Model/models--distilbert--distilbert-base-uncased-finetuned-sst-2-english/snapshots/714eb0fa89d2f80546fda750413ed43d93601a13")
#analyzer = pipeline("text-classification", model=model_path)
# print(analyzer(["This product is good", "This product is expensive"]))
analyzer = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")

def sentiment_analyzer(review):
    sentiment = analyzer(review)
    return sentiment[0]['label']

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

def Read_Analyze(file_object):
    df = pd.read_csv(file_object, encoding='latin1')

    if 'Review' not in df.columns:
        raise ValueError("Review column not found")
    df['Sentiment'] = df['Review'].apply(sentiment_analyzer)
    chart_object = sentiment_bar_chart(df)
    return df, chart_object


# result = sentiment_analyzer("C:/Users/ankitdwivedi/OneDrive - Adobe/Desktop/NLP Projects/Video to Text Summarization/Files/all-data.csv")
# print (result)

gr.close_all()

demo = gr.Interface(fn=Read_Analyze, 
                    inputs=[gr.File(file_types = ["csv"], 
                    label="Upload your review file")],
                    outputs=[gr.Dataframe(label="Reviews"), gr.Plot(label="Sentiment Analysis")],
                    title="Project 3: Sentiment Analyzer",
                    description="""This is a Sentiment Analysis Model.""")
demo.launch()