import streamlit as st from transformers import pipeline # Load sentiment analysis model @st.cache_resource def load_model(): return pipeline( "sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english" ) sentiment_model = load_model() # Main app def main(): # UI setup st.title("Sentiment Analyzer") st.write("Enter a sentence to analyze its sentiment (Positive/Negative)") # Text input user_input = st.text_area("Your Text", placeholder="Type your sentence here...", height=100) # Analyze button and result display if st.button("Analyze"): if user_input: try: result = sentiment_model(user_input)[0] sentiment = result['label'] confidence = result['score'] # Display results st.success(f"Sentiment: {sentiment}") st.info(f"Confidence: {confidence:.4f}") # Footer shown only after analysis st.markdown("""
""", unsafe_allow_html=True) except Exception as e: st.error(f"An error occurred: {str(e)}") else: st.warning("Please enter some text to analyze!") if __name__ == "__main__": main()