import streamlit as st from transformers import pipeline # Load the sentiment analysis pipeline pipe = pipeline("text-classification", model="finiteautomata/bertweet-base-sentiment-analysis") # Define a function to perform sentiment analysis def analyze_sentiment(text): try: results = pipe(text) sentiment = results[0]['label'] confidence = results[0]['score'] return sentiment, confidence except Exception as e: return "ERROR", 0.0 # Handle errors gracefully # Create a Streamlit app st.title("Sentiment Analysis App") # Get the user input text = st.text_input("Enter text for sentiment analysis") if text: # Perform sentiment analysis sentiment, confidence = analyze_sentiment(text) # Set a confidence threshold confidence_threshold = 0.5 # Display the output based on the sentiment and confidence if confidence >= confidence_threshold: if sentiment == "POSITIVE": st.success(f"Sentiment: Positive, Confidence: {confidence:.2f}") elif sentiment == "NEGATIVE": st.error(f"Sentiment: Negative, Confidence: {confidence:.2f}") else: st.info(f"Sentiment: Neutral, Confidence: {confidence:.2f}") else: st.warning("Low confidence. Sentiment result may not be reliable.")