2seriescs commited on
Commit
aa7d0bc
1 Parent(s): 6207519

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +46 -0
main.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+
6
+ from transformers import pipeline
7
+
8
+ analyis = pipeline(
9
+ "text-classification",
10
+ model="distilbert/distilbert-base-uncased-finetuned-sst-2-english"
11
+ )
12
+
13
+ def sentiment_analysis(review):
14
+ sentiment = analyis(review)
15
+ return sentiment[0]['label']
16
+
17
+ def sentiment_bar_chart(df):
18
+ sentiment_counts = df['Sentiment'].value_counts()
19
+
20
+ fig, ax = plt.subplots()
21
+ sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', color=['green', 'red'])
22
+ ax.set_title('Review Sentiment Counts')
23
+ ax.set_xlabel('Sentiment')
24
+ ax.set_ylabel('Count')
25
+
26
+ return fig
27
+
28
+ def read_reviews_and_analyze_sentiment(file_object):
29
+ df = pd.read_excel(file_object)
30
+
31
+ if 'Reviews' not in df.columns:
32
+ raise ValueError("Excel file must contain a 'Review' column")
33
+
34
+ df['Sentiment'] = df['Reviews'].apply(sentiment_analysis)
35
+ chart_object = sentiment_bar_chart(df)
36
+ return df, chart_object
37
+
38
+ demo = gr.Interface(
39
+ fn=sentiment_analysis,
40
+ inputs=[gr.File(file_types=["xlsx"], label="Input users reviews")],
41
+ outputs=[gr.DataFrame(label="User sentiment"), gr.Plot(label="Users' Sentiments")],
42
+ title="@caesar-2series: Users' Sentiment Analysis",
43
+ description="User Sentiment Review Analysis Based on File Uploaded"
44
+ )
45
+
46
+ demo.launch()