enasyazzs commited on
Commit
5ac2959
·
verified ·
1 Parent(s): 583bbed

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+
6
+ # Use a pipeline as a high-level helper
7
+ from transformers import pipeline
8
+ # model_path = ("../.venv/Modal/models--distilbert--distilbert-base-uncased-finetuned-sst-2-english"
9
+ # "/snapshots/714eb0fa89d2f80546fda750413ed43d93601a13")
10
+
11
+ analyzer = pipeline("text-classification",
12
+ model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
13
+
14
+
15
+ # analyzer = pipeline("text-classification",
16
+ # model=model_path)
17
+
18
+ # print(analyzer(["This product is good", "This product was quite expensive"]))
19
+
20
+ def sentiment_analyzer(review):
21
+ sentiment = analyzer(review)
22
+ return sentiment[0]['label']
23
+
24
+ def sentiment_bar_chart(df):
25
+ sentiment_counts = df['Sentiment'].value_counts()
26
+
27
+ # Create a bar chart
28
+ fig, ax = plt.subplots()
29
+ sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', color=['green', 'red'])
30
+ ax.set_title('Review Sentiment Counts')
31
+ ax.set_xlabel('Sentiment')
32
+ ax.set_ylabel('Count')
33
+ # ax.set_xticklabels(['Positive', 'Negative'], rotation=0)
34
+
35
+ # Return the figure object
36
+ return fig
37
+
38
+ def read_reviews_and_analyze_sentiment(file_object):
39
+ df = pd.read_excel(file_object)
40
+
41
+ if 'Reviews' not in df.columns:
42
+ raise ValueError("Error file must contains a 'Reviews' column.")
43
+
44
+ df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
45
+ chart_object = sentiment_bar_chart(df)
46
+ return df, chart_object
47
+
48
+ # result = read_reviews_and_analyze_sentiment("../.venv/Files/reviews-sentiment.xlsx")
49
+ # print(result)
50
+
51
+ demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
52
+ inputs=[gr.File(file_types=[".xlsx"], label="Input your review comment file")],
53
+ outputs=[gr.Dataframe(label="Sentiment"), gr.Plot(label="Sentiment Analysis")],
54
+ title="Project 3: Sentiment Analyzer",
55
+ description="THIS APPLICATIONS WILL BE USED TO ANALYZER THE SENTIMENT BASED ON FILE UPLOADED.")
56
+
57
+ demo.launch()