shil51 commited on
Commit
bffd1c8
·
verified ·
1 Parent(s): 8598a14

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+ # Use a pipeline as a high-level helper
6
+ from transformers import pipeline
7
+ # model_path = "../models/models--distilbert--distilbert-base-uncased-finetuned-sst-2-english/snapshots/714eb0fa89d2f80546fda750413ed43d93601a13"
8
+
9
+ # analyzer = pipeline("text-classification", model=model_path)
10
+ analyzer = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
11
+ # print(analyzer(["This product is good", "This product was quite expensive"]))
12
+
13
+
14
+ def sentiment_analyzer(review):
15
+ sentiment = analyzer(review)
16
+ return sentiment[0]['label']
17
+
18
+ def generate_sentiment_bar_chart(df):
19
+ # Validate DataFrame
20
+ if not {'Review', 'Sentiment'}.issubset(df.columns):
21
+ raise ValueError("DataFrame must contain 'Review' and 'Sentiment' columns.")
22
+
23
+ # Count occurrences of each sentiment
24
+ sentiment_counts = df['Sentiment'].value_counts()
25
+
26
+ # Create bar chart
27
+ fig, ax = plt.subplots(figsize=(8, 5))
28
+ sentiment_counts.plot(kind='bar', color=['green', 'red'], edgecolor='black', ax=ax)
29
+
30
+ # Customize plot
31
+ ax.set_title("Sentiment Distribution", fontsize=14)
32
+ ax.set_xlabel("Sentiment", fontsize=12)
33
+ ax.set_ylabel("Count", fontsize=12)
34
+ ax.grid(axis='y', linestyle='--', alpha=0.7)
35
+ plt.xticks(rotation=45)
36
+
37
+ # Adjust layout
38
+ plt.tight_layout()
39
+
40
+ return fig
41
+
42
+
43
+ def read_review_and_analyze_sentiment(file_object):
44
+ df = pd.read_excel(file_object)
45
+ if 'Review' not in df.columns:
46
+ raise ValueError("Excel file must contain a 'Review' colum.")
47
+ df['Sentiment'] = df['Review'].apply(sentiment_analyzer)
48
+ chat_object = generate_sentiment_bar_chart(df)
49
+ return df, chat_object
50
+ # file = '../files/product_review.xlsx'
51
+ # result = read_review_and_analyze_sentiment(file)
52
+ # print(result)
53
+
54
+ gr.close_all()
55
+
56
+ # demo = gr.Interface(fn=summary, inputs="text", outputs="text")
57
+ demo = gr.Interface(fn=read_review_and_analyze_sentiment,
58
+ inputs=[gr.File(file_types=[".xlsx"],label="Input your review comment")],
59
+ outputs=[gr.Dataframe(label="Sentiment"), gr.Plot(label="Sentiment Analysis")],
60
+ title="GenAI Project 3: Sentiment Analyzer",
61
+ description="This application is use to analyze the sentiment based on the File uploaded.")
62
+ demo.launch()