Hwilner commited on
Commit
5b11708
·
verified ·
1 Parent(s): 0e72e73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
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 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)
12
- return sentiment[0]['label']
 
 
 
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
- # 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
 
 
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