Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,16 +4,20 @@ import pandas as pd
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
from transformers import pipeline
|
6 |
|
7 |
-
# Use a pipeline with
|
8 |
-
|
|
|
|
|
9 |
|
10 |
def sentiment_analyzer(review):
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
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')
|
@@ -22,15 +26,15 @@ def sentiment_bar_chart(df):
|
|
22 |
return fig
|
23 |
|
24 |
def read_reviews_and_analyze_sentiment(file_object):
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
|
35 |
gr.close_all()
|
36 |
|
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
from transformers import pipeline
|
6 |
|
7 |
+
# Use a pipeline with an appropriate model for sentiment analysis
|
8 |
+
# Replace with a correct model suitable for your needs
|
9 |
+
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
|
10 |
+
analyzer = pipeline("text-classification", model=model_name)
|
11 |
|
12 |
def sentiment_analyzer(review):
|
13 |
+
try:
|
14 |
+
sentiment = analyzer(review)
|
15 |
+
return sentiment[0]['label']
|
16 |
+
except Exception as e:
|
17 |
+
return f"Error: {e}"
|
18 |
|
19 |
def sentiment_bar_chart(df):
|
20 |
sentiment_counts = df['Sentiment'].value_counts()
|
|
|
21 |
fig, ax = plt.subplots()
|
22 |
sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', colors=['green', 'red'])
|
23 |
ax.set_title('Review Sentiment Counts')
|
|
|
26 |
return fig
|
27 |
|
28 |
def read_reviews_and_analyze_sentiment(file_object):
|
29 |
+
try:
|
30 |
+
df = pd.read_excel(file_object)
|
31 |
+
if 'Reviews' not in df.columns:
|
32 |
+
raise ValueError("Excel file must contain a 'Reviews' column.")
|
33 |
+
df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
|
34 |
+
chart_object = sentiment_bar_chart(df)
|
35 |
+
return df, chart_object
|
36 |
+
except Exception as e:
|
37 |
+
return f"Error: {e}", None
|
38 |
|
39 |
gr.close_all()
|
40 |
|