Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,10 @@ 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 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# analyzer = pipeline("text-classification",
|
| 13 |
-
# model=model_path)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# print(analyzer(["This production is good", "This product was quite expensive"]))
|
| 18 |
|
| 19 |
def sentiment_analyzer(review):
|
| 20 |
sentiment = analyzer(review)
|
|
@@ -22,42 +13,30 @@ def sentiment_analyzer(review):
|
|
| 22 |
|
| 23 |
def sentiment_bar_chart(df):
|
| 24 |
sentiment_counts = df['Sentiment'].value_counts()
|
| 25 |
-
|
| 26 |
# Create a bar chart
|
| 27 |
fig, ax = plt.subplots()
|
| 28 |
-
sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%',
|
| 29 |
ax.set_title('Review Sentiment Counts')
|
| 30 |
ax.set_xlabel('Sentiment')
|
| 31 |
ax.set_ylabel('Count')
|
| 32 |
-
# ax.set_xticklabels(['Positive', 'Negative'], rotation=0)
|
| 33 |
-
|
| 34 |
-
# Return the figure object
|
| 35 |
return fig
|
| 36 |
|
| 37 |
-
|
| 38 |
def read_reviews_and_analyze_sentiment(file_object):
|
| 39 |
# Load the Excel file into a DataFrame
|
| 40 |
df = pd.read_excel(file_object)
|
| 41 |
-
|
| 42 |
-
# Check if 'Review' column is in the DataFrame
|
| 43 |
if 'Reviews' not in df.columns:
|
| 44 |
-
raise ValueError("Excel file must contain a '
|
| 45 |
-
|
| 46 |
-
# Apply the get_sentiment function to each review in the DataFrame
|
| 47 |
df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
|
| 48 |
chart_object = sentiment_bar_chart(df)
|
| 49 |
return df, chart_object
|
| 50 |
|
| 51 |
-
|
| 52 |
-
# print(result)
|
| 53 |
-
# Example usage:
|
| 54 |
-
# df = read_reviews_and_analyze_sentiment('path_to_your_excel_file.xlsx')
|
| 55 |
-
# print(df)
|
| 56 |
-
|
| 57 |
|
| 58 |
demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
|
| 59 |
inputs=[gr.File(file_types=["xlsx"], label="Upload your review comment file")],
|
| 60 |
outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Analysis")],
|
| 61 |
title="Sentiment Analyzer",
|
| 62 |
-
description="
|
| 63 |
-
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
+
# Use a pipeline with a model suitable for sentiment analysis
|
| 8 |
+
analyzer = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def sentiment_analyzer(review):
|
| 11 |
sentiment = analyzer(review)
|
|
|
|
| 13 |
|
| 14 |
def sentiment_bar_chart(df):
|
| 15 |
sentiment_counts = df['Sentiment'].value_counts()
|
|
|
|
| 16 |
# Create a bar chart
|
| 17 |
fig, ax = plt.subplots()
|
| 18 |
+
sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', colors=['green', 'red'])
|
| 19 |
ax.set_title('Review Sentiment Counts')
|
| 20 |
ax.set_xlabel('Sentiment')
|
| 21 |
ax.set_ylabel('Count')
|
|
|
|
|
|
|
|
|
|
| 22 |
return fig
|
| 23 |
|
|
|
|
| 24 |
def read_reviews_and_analyze_sentiment(file_object):
|
| 25 |
# Load the Excel file into a DataFrame
|
| 26 |
df = pd.read_excel(file_object)
|
| 27 |
+
# Check if 'Reviews' column is in the DataFrame
|
|
|
|
| 28 |
if 'Reviews' not in df.columns:
|
| 29 |
+
raise ValueError("Excel file must contain a 'Reviews' column.")
|
| 30 |
+
# Apply the sentiment analysis function to each review in the DataFrame
|
|
|
|
| 31 |
df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
|
| 32 |
chart_object = sentiment_bar_chart(df)
|
| 33 |
return df, chart_object
|
| 34 |
|
| 35 |
+
gr.close_all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
|
| 38 |
inputs=[gr.File(file_types=["xlsx"], label="Upload your review comment file")],
|
| 39 |
outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Analysis")],
|
| 40 |
title="Sentiment Analyzer",
|
| 41 |
+
description="This application will be used to analyze the sentiment based on the uploaded file.")
|
| 42 |
+
demo.launch()
|