Spaces:
Sleeping
Sleeping
usamaferoz12
commited on
Commit
•
643be30
1
Parent(s):
054f76b
Update app.py
Browse files
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 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
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 |
-
|
21 |
-
|
|
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.")
|