DL_Project / app.py
mugu5's picture
Update app.py
59f0acb verified
import streamlit as st
import torch
from All_Model import BertForMultiLabel ,bert_tokenizer,BERT_MODEL_PATH
# 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.")