mayankchugh-learning
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use a pipeline as a high-level helper
|
2 |
+
from transformers import pipeline
|
3 |
+
import gradio as gr
|
4 |
+
import pandas as pd
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
|
7 |
+
analyser = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
|
8 |
+
|
9 |
+
# model_path = ("./Models/models--distilbert--distilbert-base-uncased-finetuned-sst-2-english/snapshots/714eb0fa89d2f80546fda750413ed43d93601a13")
|
10 |
+
|
11 |
+
# analyser = pipeline("text-classification", model=model_path)
|
12 |
+
|
13 |
+
# print(analyser(["This product is good!", "This product is expensive!"]))
|
14 |
+
|
15 |
+
|
16 |
+
def sentiment_analysis(text_to_review):
|
17 |
+
sentiment = analyser(text_to_review)
|
18 |
+
return sentiment[0]['label']
|
19 |
+
|
20 |
+
# print(sentiment_analysis(["This product is good!", "This product is expensive!"]))
|
21 |
+
|
22 |
+
def plot_sentiment_pie(df):
|
23 |
+
# Count the number of positive and negative reviews
|
24 |
+
sentiment_counts = df['Sentiment'].value_counts()
|
25 |
+
|
26 |
+
# Create the pie chart
|
27 |
+
fig, ax = plt.subplots(figsize=(6, 6))
|
28 |
+
ax.pie(sentiment_counts.values, labels=sentiment_counts.index, autopct='%1.1f%%')
|
29 |
+
ax.set_title('Sentiment Distribution')
|
30 |
+
|
31 |
+
# Convert the Matplotlib figure to a Gradio Plots component
|
32 |
+
return fig
|
33 |
+
|
34 |
+
def read_excel_and_get_sentiment(file):
|
35 |
+
try:
|
36 |
+
df = pd.read_excel(file)
|
37 |
+
if 'Review' not in df.columns:
|
38 |
+
raise KeyError("'Review' column not found in the Excel file.")
|
39 |
+
df['Sentiment'] = df['Review'].apply(sentiment_analysis)
|
40 |
+
|
41 |
+
chart_object = plot_sentiment_pie(df)
|
42 |
+
|
43 |
+
return df, chart_object
|
44 |
+
except FileNotFoundError:
|
45 |
+
print(f"Error: {file} not found.")
|
46 |
+
raise
|
47 |
+
except Exception as e:
|
48 |
+
print(f"Error: {e}")
|
49 |
+
raise
|
50 |
+
|
51 |
+
gr.close_all()
|
52 |
+
|
53 |
+
demo = gr.Interface(fn=read_excel_and_get_sentiment,
|
54 |
+
inputs=[gr.File(file_types= ['xlsx'],label="upload your review comment excel file.")],
|
55 |
+
outputs=[gr.DataFrame(label="Reviewed text"), gr.Plot(label="Sentiment Analysis")],
|
56 |
+
title="@IT AI Enthusiast (https://www.youtube.com/@itaienthusiast/) - Sentiment Analysis",
|
57 |
+
description="THIS APPLICATION WILL BE USED TO ANALYZER THE SENTIMENT BASED ON THE COMMENT PROVIDER.",
|
58 |
+
concurrency_limit=16)
|
59 |
+
demo.launch()
|
60 |
+
|