Sahibhim commited on
Commit
c63966f
·
verified ·
1 Parent(s): a100693

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This app analyzes product review sentiments using a DistilBERT model via Hugging Face + Gradio.
2
+ # Input must be a .csv or .xlsx file with a 'Reviews' column; other columns are optional.
3
+ # Only 'Reviews' are processed for classification as Positive or Negative.
4
+
5
+ import torch
6
+ import gradio as gr
7
+ import pandas as pd
8
+ import matplotlib.pyplot as plt
9
+
10
+ # Use a pipeline as a high-level helper
11
+ from transformers import pipeline
12
+ # model_path= ("../Models/models--distilbert--distilbert-base-uncased-finetuned-sst-2-english"
13
+ # "/snapshots/714eb0fa89d2f80546fda750413ed43d93601a13")
14
+
15
+
16
+ analyzer = pipeline("text-classification",
17
+ model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
18
+
19
+ # analyzer = pipeline("text-classification",
20
+ # model=model_path)
21
+
22
+
23
+ #print(analyzer(["Sahithi is good person and will always trust all people"]))
24
+
25
+ def sentiment_analyzer(review):
26
+ sentiment = analyzer(review)
27
+ return sentiment[0]['label']
28
+
29
+ def sentiment_bar_chart(df):
30
+ sentiment_counts = df['Sentiment'].value_counts()
31
+
32
+ # Create a bar chart
33
+ fig, ax = plt.subplots()
34
+ sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', color=['green', 'red'])
35
+ ax.set_title('Review Sentiment Counts')
36
+ ax.set_xlabel('Sentiment')
37
+ ax.set_ylabel('Count')
38
+ # ax.set_xticklabels(['Positive', 'Negative'], rotation=0)
39
+
40
+ # Return the figure object
41
+ return fig
42
+
43
+ def read_reviews_and_analyze_sentiment(file_object):
44
+ # Load the Excel file into a DataFrame
45
+ df = pd.read_excel(file_object)
46
+
47
+ # Check if 'Review' column is in the DataFrame
48
+ if 'Reviews' not in df.columns:
49
+ raise ValueError("Excel file must contain a 'Review' column.")
50
+
51
+ # Apply the get_sentiment function to each review in the DataFrame
52
+ df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
53
+ chart_object = sentiment_bar_chart(df)
54
+ return df, chart_object
55
+
56
+ # result = read_reviews_and_analyze_sentiment("../Files/Prod-review.xlsx")
57
+ # print(result)
58
+ # Example usage:
59
+ # df = read_reviews_and_analyze_sentiment('path_to_your_excel_file.xlsx')
60
+ # print(df)
61
+
62
+
63
+
64
+ demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
65
+ inputs=[gr.File(label="Upload your review comment file")],
66
+ outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Analysis")],
67
+ title="@Sahibhim GenAI Project 3: Sentiment Analyzer",
68
+ description="THIS APPLICATION WILL BE USED TO ANALYZE THE SENTIMENT BASED ON FILE UPLAODED.")
69
+ demo.launch()