Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,32 +8,27 @@ 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",
|
| 12 |
-
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
|
| 13 |
-
|
| 14 |
-
# analyzer = pipeline("text-classification",
|
| 15 |
-
# model=model_path)
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# print(analyzer(["This production is good", "This product was quite expensive"]))
|
| 20 |
|
|
|
|
| 21 |
def sentiment_analyzer(review):
|
| 22 |
sentiment = analyzer(review)
|
| 23 |
return sentiment[0]['label']
|
| 24 |
|
| 25 |
-
def
|
| 26 |
sentiment_counts = df['Sentiment'].value_counts()
|
| 27 |
|
| 28 |
-
# Create a
|
| 29 |
fig, ax = plt.subplots()
|
| 30 |
sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', color=['green', 'red'])
|
| 31 |
ax.set_title('Review Sentiment Counts')
|
| 32 |
-
ax.set_xlabel('Sentiment')
|
| 33 |
-
ax.set_ylabel('Count')
|
| 34 |
-
# ax.set_xticklabels(['Positive', 'Negative'], rotation=0)
|
| 35 |
|
| 36 |
-
# Return the figure object
|
| 37 |
return fig
|
| 38 |
|
| 39 |
|
|
@@ -43,23 +38,19 @@ def read_reviews_and_analyze_sentiment(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 '
|
| 47 |
|
| 48 |
# Apply the get_sentiment function to each review in the DataFrame
|
| 49 |
df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
|
| 50 |
-
chart_object =
|
| 51 |
return df, chart_object
|
| 52 |
|
| 53 |
# result = read_reviews_and_analyze_sentiment("../Files/Prod-review.xlsx")
|
| 54 |
# print(result)
|
| 55 |
-
# Example usage:
|
| 56 |
-
# df = read_reviews_and_analyze_sentiment('path_to_your_excel_file.xlsx')
|
| 57 |
-
# print(df)
|
| 58 |
-
|
| 59 |
|
| 60 |
demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
|
| 61 |
-
inputs=[gr.File(file_types=["xlsx"], label="Upload your
|
| 62 |
outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Analysis")],
|
| 63 |
-
title="
|
| 64 |
-
description="
|
| 65 |
demo.launch()
|
|
|
|
| 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 |
sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', color=['green', 'red'])
|
| 30 |
ax.set_title('Review Sentiment Counts')
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
| 32 |
return fig
|
| 33 |
|
| 34 |
|
|
|
|
| 38 |
|
| 39 |
# Check if 'Review' column is in the DataFrame
|
| 40 |
if 'Reviews' not in df.columns:
|
| 41 |
+
raise ValueError("Excel file must contain a 'Reviews' column.")
|
| 42 |
|
| 43 |
# Apply the get_sentiment function to each review in the DataFrame
|
| 44 |
df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
|
| 45 |
+
chart_object = sentiment_pie_chart(df)
|
| 46 |
return df, chart_object
|
| 47 |
|
| 48 |
# result = read_reviews_and_analyze_sentiment("../Files/Prod-review.xlsx")
|
| 49 |
# print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
|
| 52 |
+
inputs=[gr.File(file_types=["xlsx"], label="Upload your reviews file")],
|
| 53 |
outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Analysis")],
|
| 54 |
+
title="Sentiment Analyzer",
|
| 55 |
+
description="Analyze the sentiment based on file uploaded (Excel file .xlsx must contain a 'Reviews' column)")
|
| 56 |
demo.launch()
|