Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from transformers import pipeline | |
| import time | |
| # Set page configuration | |
| st.set_page_config(page_title="Sentiment Analysis App", page_icon="π") | |
| def load_sentiment_model(): | |
| """Load the sentiment analysis model.""" | |
| return pipeline('sentiment-analysis') | |
| def analyze_sentiment(text, model): | |
| """Analyze the sentiment of the given text.""" | |
| try: | |
| return model(text) | |
| except Exception as e: | |
| st.error(f"An error occurred during sentiment analysis: {str(e)}") | |
| return None | |
| def main(): | |
| # Page header | |
| st.title("π Sentiment Analysis Tool") | |
| st.write("Enter your text below to analyze its sentiment.") | |
| # Load the model | |
| with st.spinner("Loading sentiment analysis model..."): | |
| sentiment_model = load_sentiment_model() | |
| # Text input | |
| text = st.text_area("Enter some text:", height=150) | |
| if st.button("Analyze Sentiment"): | |
| if not text: | |
| st.warning("Please enter some text to analyze.") | |
| else: | |
| with st.spinner("Analyzing sentiment..."): | |
| result = analyze_sentiment(text, sentiment_model) | |
| if result: | |
| # Display results | |
| sentiment = result[0]['label'] | |
| score = result[0]['score'] | |
| st.subheader("Analysis Result:") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.metric("Sentiment", sentiment) | |
| with col2: | |
| st.metric("Confidence", f"{score:.2%}") | |
| # Visualize the sentiment | |
| if sentiment == "POSITIVE": | |
| st.success("π The text expresses a positive sentiment.") | |
| elif sentiment == "NEGATIVE": | |
| st.error("π The text expresses a negative sentiment.") | |
| else: | |
| st.info("π The text expresses a neutral sentiment.") | |
| # Display raw JSON output | |
| with st.expander("See raw output"): | |
| st.json(result) | |
| # Add a footer | |
| st.markdown("---") | |
| st.markdown("Created with β€οΈ by Ali Nasri") | |
| if __name__ == "__main__": | |
| main() | |
| #pipe = pipeline('sentiment-analysis') | |
| #text = st.text_area('Enter some text') | |
| #if text: | |
| # out = pipe(text) | |
| # st.json(out) |