File size: 1,232 Bytes
8aa1b26
a45a655
 
 
 
560e476
a45a655
 
 
d7e164d
a45a655
 
 
 
 
 
 
 
560e476
 
 
 
 
 
 
 
 
 
a45a655
560e476
 
a45a655
 
 
 
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
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import torch.nn.functional as F

MODEL_NAME = "imrgurmeet/fine-tuned-sentiment-model"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)

st.title("Sentiment Analyzer(Encoder-Only)")

user_input = st.text_area("Enter text for sentiment analysis:")

if st.button("Analyze"):
    if user_input.strip() != "":
        inputs = tokenizer(user_input, return_tensors="pt")
        outputs = model(**inputs)
        probs = F.softmax(outputs.logits, dim=-1)

        # Dynamically create label list based on model
        num_labels = model.config.num_labels
        if num_labels == 3:
            labels = ["Negative", "Neutral", "Positive"]
        elif num_labels == 2:
            labels = ["Negative", "Positive"]
        else:
            labels = [f"Class {i}" for i in range(num_labels)]

        pred_class = torch.argmax(probs).item()
        sentiment = labels[pred_class]

        st.write(f"**Sentiment:** {sentiment}")
        st.write(f"**Confidence:** {probs[0][pred_class]:.2f}")
    else:
        st.warning("Please enter some text.")