File size: 2,412 Bytes
ad6ce8b
 
9ea7870
ad6ce8b
9ea7870
 
ad6ce8b
9ea7870
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e260cb
9ea7870
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import streamlit as st
from transformers import pipeline
import time

# Set page configuration
st.set_page_config(page_title="Sentiment Analysis App", page_icon="😊")

@st.cache_resource
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)