File size: 1,829 Bytes
0144081
c178054
 
 
0144081
c178054
 
 
 
 
 
0144081
c178054
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0144081
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
import streamlit as st
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch.nn.functional as F

# Load trained model & tokenizer
@st.cache_resource
def load_model():
    model = AutoModelForSequenceClassification.from_pretrained("models/sentiment_model")
    tokenizer = AutoTokenizer.from_pretrained("models/sentiment_model")
    return model, tokenizer

model, tokenizer = load_model()

# Streamlit UI
st.set_page_config(page_title="Sentiment Analyzer", page_icon="πŸ’¬", layout="wide")

st.title("πŸ’¬ Sentiment Analyzer")
st.write("Analyze the sentiment of any text! Enter a sentence below and get an instant analysis.")

user_input = st.text_area("Enter your text:", "")

if st.button("Analyze Sentiment"):
    if user_input:
        with st.spinner("Analyzing..."):
            inputs = tokenizer(user_input, return_tensors="pt", truncation=True, padding=True)
            outputs = model(**inputs)
            probs = F.softmax(outputs.logits, dim=-1)
            sentiment_index = torch.argmax(probs).item()
            confidence = round(probs[0][sentiment_index].item() * 100, 2)

        # Map index to label
        labels = ["Negative", "Neutral", "Positive"]  # Adjust this based on your training labels
        sentiment = labels[sentiment_index]

        # Display result
        st.subheader("πŸ” Result")
        if sentiment == "Positive":
            st.success(f"😊 **Positive Sentiment** ({confidence}%)")
        elif sentiment == "Negative":
            st.error(f"😠 **Negative Sentiment** ({confidence}%)")
        else:
            st.warning(f"😐 **Neutral Sentiment** ({confidence}%)")

    else:
        st.warning("⚠️ Please enter some text.")

st.markdown("---")
st.markdown("πŸ”— Built with Streamlit | Model: DistilBERT (Fine-tuned)")