File size: 1,318 Bytes
e6c25d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import torch
from All_Model import  BertForMultiLabel ,bert_tokenizer

# model init
model = BertForMultiLabel() 
# Load fine-tuned weights
state_dict = torch.load(BERT_MODEL_PATH, map_location="cpu")
model.load_state_dict(state_dict)
model.eval()

# -------------------------------
#  Streamlit App
# -------------------------------
st.title("Emotion Classification with fine‑tuned  BERT")

# Input text box
text = st.text_area("Enter text to analyze five different emotions:")

if st.button("Predict"):
    if text.strip():
        # Tokenize input
        inputs = bert_tokenizer(text, return_tensors="pt", truncation=True, padding=True)

        with torch.no_grad():
            # logits = model(**inputs) for Ro berta
            logits = model(input_ids=inputs["input_ids"],
                           attention_mask=inputs["attention_mask"])
            
            probs = torch.sigmoid(logits).cpu().numpy().tolist()[0]

        emotions = ["anger", "fear", "joy", "sadness", "surprise"]
        result = dict(zip(emotions, probs))

        # Display results
        st.subheader("Predicted Emotion Probabilities")
        for emotion, prob in result.items():
            st.write(f"{emotion} :  {prob:.4f}")
    else:
        st.warning("Please enter some text before predicting.")