usamaferoz12 commited on
Commit
643be30
1 Parent(s): 054f76b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -6,10 +6,13 @@ pipe = pipeline("text-classification", model="finiteautomata/bertweet-base-senti
6
 
7
  # Define a function to perform sentiment analysis
8
  def analyze_sentiment(text):
9
- results = pipe(text)
10
- sentiment = results[0]['label']
11
- confidence = results[0]['score']
12
- return sentiment, confidence
 
 
 
13
 
14
  # Create a Streamlit app
15
  st.title("Sentiment Analysis App")
@@ -17,13 +20,20 @@ st.title("Sentiment Analysis App")
17
  # Get the user input
18
  text = st.text_input("Enter text for sentiment analysis")
19
 
20
- # Perform sentiment analysis
21
- sentiment, confidence = analyze_sentiment(text)
 
22
 
23
- # Display the output
24
- if sentiment == "POSITIVE":
25
- st.success(f"Sentiment: Positive, Confidence: {confidence:.2f}")
26
- elif sentiment == "NEGATIVE":
27
- st.error(f"Sentiment: Negative, Confidence: {confidence:.2f}")
28
- else:
29
- st.info(f"Sentiment: Neutral, Confidence: {confidence:.2f}")
 
 
 
 
 
 
 
6
 
7
  # Define a function to perform sentiment analysis
8
  def analyze_sentiment(text):
9
+ try:
10
+ results = pipe(text)
11
+ sentiment = results[0]['label']
12
+ confidence = results[0]['score']
13
+ return sentiment, confidence
14
+ except Exception as e:
15
+ return "ERROR", 0.0 # Handle errors gracefully
16
 
17
  # Create a Streamlit app
18
  st.title("Sentiment Analysis App")
 
20
  # Get the user input
21
  text = st.text_input("Enter text for sentiment analysis")
22
 
23
+ if text:
24
+ # Perform sentiment analysis
25
+ sentiment, confidence = analyze_sentiment(text)
26
 
27
+ # Set a confidence threshold
28
+ confidence_threshold = 0.5
29
+
30
+ # Display the output based on the sentiment and confidence
31
+ if confidence >= confidence_threshold:
32
+ if sentiment == "POSITIVE":
33
+ st.success(f"Sentiment: Positive, Confidence: {confidence:.2f}")
34
+ elif sentiment == "NEGATIVE":
35
+ st.error(f"Sentiment: Negative, Confidence: {confidence:.2f}")
36
+ else:
37
+ st.info(f"Sentiment: Neutral, Confidence: {confidence:.2f}")
38
+ else:
39
+ st.warning("Low confidence. Sentiment result may not be reliable.")