File size: 5,958 Bytes
f5290c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3e8793
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import streamlit as st
from transformers import pipeline
import numpy as np
import torch

# Set page configuration
st.set_page_config(
    page_title="Sentiment Analysis API",
    page_icon="πŸ˜€",
    layout="centered",
    initial_sidebar_state="collapsed",
)

# App title and description
st.title("Sentiment Analysis API")
st.write("This API uses a pre-trained BERT model to classify text sentiment as positive, negative, or neutral.")

# Load the sentiment analysis model
@st.cache_resource
def load_model():
    return pipeline('sentiment-analysis', model="cardiffnlp/twitter-roberta-base-sentiment")

# Get the model
model = load_model()

# Sample text examples
examples = [
    "I absolutely love this new feature! It's amazing.",
    "This product is terrible and doesn't work as advertised.",
    "The weather is just okay today, nothing special."
]

# Create the UI elements
text_input = st.text_area("Enter text to analyze:", height=150, 
                          placeholder="Type or paste your text here...")

# Add example buttons
st.write("Or try one of these examples:")
col1, col2, col3 = st.columns(3)
with col1:
    if st.button("Positive Example"):
        text_input = examples[0]
with col2:
    if st.button("Negative Example"):
        text_input = examples[1]
with col3:
    if st.button("Neutral Example"):
        text_input = examples[2]

# Function to analyze and display sentiment
def analyze_sentiment(text):
    try:
        result = model(text)[0]
        
        # Map labels to user-friendly sentiment names
        sentiment_mapping = {
            'LABEL_0': 'Negative',
            'LABEL_1': 'Neutral',
            'LABEL_2': 'Positive'
        }
        
        sentiment = sentiment_mapping[result['label']]
        confidence = result['score']
        
        # Display results with color-coded box
        if sentiment == "Positive":
            st.success(f"Sentiment: {sentiment} (Confidence: {confidence:.4f})")
        elif sentiment == "Negative":
            st.error(f"Sentiment: {sentiment} (Confidence: {confidence:.4f})")
        else:
            st.info(f"Sentiment: {sentiment} (Confidence: {confidence:.4f})")
            
        # Display confidence as a progress bar
        st.progress(confidence)
        
        # Show detailed sentiment breakdown
        st.subheader("Sentiment Breakdown")
        sentiment_data = {
            'Sentiment': ['Negative', 'Neutral', 'Positive'],
            'Score': [0, 0, 0]  # Default values
        }
        
        # Update the score for the detected sentiment
        if sentiment == "Positive":
            sentiment_data['Score'][2] = confidence
        elif sentiment == "Negative":
            sentiment_data['Score'][0] = confidence
        else:
            sentiment_data['Score'][1] = confidence
            
        # Display as a horizontal bar chart
        st.bar_chart(sentiment_data, x='Sentiment', y='Score')
        
    except Exception as e:
        st.error(f"An error occurred: {str(e)}")

# Process the text when the analyze button is clicked
if st.button("Analyze Sentiment") and text_input:
    with st.spinner("Analyzing sentiment..."):
        analyze_sentiment(text_input)
elif text_input:
    st.info("Click 'Analyze Sentiment' to process the text.")
else:
    st.info("Please enter some text to analyze.")

def analyze_sentiment(text):
    try:
        if len(text.split()) > 512:
            st.error("Input too long (max 512 words). Please shorten the text.")
            return
        result = model(text)[0]
        sentiment_mapping = {
            'LABEL_0': 'Negative',
            'LABEL_1': 'Neutral',
            'LABEL_2': 'Positive'
        }
        sentiment = sentiment_mapping[result['label']]
        confidence = result['score']
        if sentiment == "Positive":
            st.success(f"Sentiment: {sentiment} (Confidence: {confidence:.4f})")
        elif sentiment == "Negative":
            st.error(f"Sentiment: {sentiment} (Confidence: {confidence:.4f})")
        else:
            st.info(f"Sentiment: {sentiment} (Confidence: {confidence:.4f})")
        st.progress(confidence)
        st.subheader("Sentiment Breakdown")
        sentiment_data = {
            'Sentiment': ['Negative', 'Neutral', 'Positive'],
            'Score': [0, 0, 0]
        }
        if sentiment == "Positive":
            sentiment_data['Score'][2] = confidence
        elif sentiment == "Negative":
            sentiment_data['Score'][0] = confidence
        else:
            sentiment_data['Score'][1] = confidence
        st.bar_chart(sentiment_data, x='Sentiment', y='Score')
    except Exception as e:
        st.error(f"Error analyzing sentiment: {str(e)}. Please try again or use shorter text.")

# Add information about the model
with st.expander("About the Model"):
    st.write("""
    This application uses the `cardiffnlp/twitter-roberta-base-sentiment` model from Hugging Face.
    
    The model is a RoBERTa-base model trained on ~58M tweets and fine-tuned for sentiment analysis 
    with the TweetEval benchmark. It classifies text into three sentiment categories:
    
    - Negative (LABEL_0)
    - Neutral (LABEL_1)
    - Positive (LABEL_2)
    
    Source: [cardiffnlp/twitter-roberta-base-sentiment](https://huggingface.co/cardiffnlp/twitter-roberta-base-sentiment)
    """)

with st.expander("Model Performance"):
    st.write("Tested on 100 samples from `tweet_eval` dataset.")
    if st.button("Show Test Accuracy"):
        from datasets import load_dataset
        dataset = load_dataset("tweet_eval", "sentiment", split="test[:100]")
        correct = sum(1 for text, label in zip(dataset['text'], dataset['label']) if (2 if model(text)[0]['label'] == 'LABEL_2' else 0 if model(text)[0]['label'] == 'LABEL_0' else 1) == label)
        st.write(f"Accuracy: {correct/100:.2f}")
        
# Footer
st.markdown("---")
st.markdown("Created as part of Mini Project 1: Sentiment Analysis API")