Spaces:
Sleeping
Sleeping
Create app.py
Browse files
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 |
+
|
| 6 |
+
# Use a pipeline as a high-level helper
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
# model_path = ("../Models/models--distilbert--distilbert-base-uncased-finetuned-sst-2-english"
|
| 9 |
+
# "/snapshots/714eb0fa89d2f80546fda750413ed43d93601a13")
|
| 10 |
+
|
| 11 |
+
# analyzer = pipeline("text-classification", model=model_path)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
analyzer = pipeline("text-classification",
|
| 15 |
+
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
|
| 16 |
+
|
| 17 |
+
# print(analyzer(["This production is good", "This product was quite expensive"]))
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def sentiment_analyzer(review):
|
| 21 |
+
sentiment = analyzer(review)
|
| 22 |
+
return sentiment[0]['label']
|
| 23 |
+
|
| 24 |
+
def sentiment_pie_chart(df):
|
| 25 |
+
sentiment_counts = df['Sentiment'].value_counts()
|
| 26 |
+
|
| 27 |
+
# Create a pie chart
|
| 28 |
+
fig, ax = plt.subplots()
|
| 29 |
+
pie = ax.pie(sentiment_counts, autopct='%1.1f%%', colors=['green', 'red'], labels=sentiment_counts.index)
|
| 30 |
+
|
| 31 |
+
# Add count values to each slice
|
| 32 |
+
for i, p in enumerate(pie[0]):
|
| 33 |
+
count = f'({sentiment_counts[i]})'
|
| 34 |
+
ax.annotate(count, xy=p.get_xy(), xytext=(0, 0.5), textcoords='offset points', ha='center', fontsize=10)
|
| 35 |
+
|
| 36 |
+
ax.set_title('Review Sentiment Counts')
|
| 37 |
+
ax.set_xlabel('Sentiment')
|
| 38 |
+
return fig
|
| 39 |
+
|
| 40 |
+
def read_reviews_and_analyze_sentiment(file_object):
|
| 41 |
+
# Load the Excel file into a DataFrame
|
| 42 |
+
df = pd.read_excel(file_object)
|
| 43 |
+
|
| 44 |
+
# Check if 'Review' column is in the DataFrame
|
| 45 |
+
if 'Reviews' not in df.columns:
|
| 46 |
+
raise ValueError("Excel file must contain a 'Review' column.")
|
| 47 |
+
|
| 48 |
+
# Apply the get_sentiment function to each review in the DataFrame
|
| 49 |
+
df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
|
| 50 |
+
chart_object = sentiment_pie_chart(df)
|
| 51 |
+
return df, chart_object
|
| 52 |
+
|
| 53 |
+
# result = read_reviews_and_analyze_sentiment("../Files/Prod-review.xlsx")
|
| 54 |
+
# print(result)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
|
| 58 |
+
inputs=[gr.File(file_types=["xlsx"], label="Upload your reviews file")],
|
| 59 |
+
outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Analysis")],
|
| 60 |
+
title="Sentiment Analyzer",
|
| 61 |
+
description="Analyze the sentiment based on file uploaded (Excel file .xlsx must contain a 'Reviews' column)")
|
| 62 |
+
demo.launch()
|