sumonta056's picture
feat: working with negatiive emotion
88ba180
raw
history blame
3.23 kB
import streamlit as st
from transformers import BertForSequenceClassification, BertTokenizerFast
from emotion_utils import predict # Custom module for prediction
# Load the BERT model and tokenizer
model_path = "./model/"
model = BertForSequenceClassification.from_pretrained(model_path)
tokenizer = BertTokenizerFast.from_pretrained(model_path)
# Function to update sentiment analysis
def analyze_sentiment(text):
if text.strip():
probs, _, label = predict(text, model, tokenizer)
score = probs.max().item() # Get the highest probability score
return label, score
else:
return None, None
# Function to get emoji based on emotion
def get_emoji(label):
if label == "Anger":
return "😠"
elif label == "Astonished":
return "😲"
elif label == "Optimistic":
return "😊"
elif label == "Sadness":
return "😒"
else:
return "πŸ™‚"
# Streamlit app configuration
st.set_page_config(
page_title="G-Bert: Emotion Analysis",
page_icon="😊",
layout="centered"
)
# Custom CSS for a modern UI
st.markdown("""
<style>
body {
background: linear-gradient(to right, #6a11cb, #2575fc);
color: white;
font-family: 'Segoe UI', sans-serif;
}
.stButton button {
color: white;
border-radius: 8px;
font-size: 16px;
font-weight: bold;
}
.stTextArea textarea {
border-radius: 8px;
}
footer {
font-size: 14px;
text-align: center;
padding: 10px;
}
footer a {
color: #2575fc;
text-decoration: none;
}
</style>
""", unsafe_allow_html=True)
# Title and description
st.title("🌟 G-Bert: Emotion Analysis")
st.markdown("""
G-Bert is a Bangla sentiment analysis tool that uses a pre-trained BERT model to analyze the emotion of any Bengali or religious (Gita) text.
It can detect emotions like Anger, Astonished, Optimistic, and Sadness with a confidence score.
""")
# Text input
st.write("Enter some text below, and G-Bert will analyze its emotion for you!")
text = st.text_area("Input Text", height=150, placeholder="Type your text here...")
# Analyze button
if st.button("✨ Analyze Emotion ✨"):
if text.strip():
label, score = analyze_sentiment(text)
if label and score:
emoji = get_emoji(label)
st.markdown(f"""
<h2 style="text-align:center;">{emoji} Emotion: {label} {emoji}</h2>
<p style="text-align:center; font-size:20px;">Confidence Score: <strong>{score:.2f}</strong></p>
""", unsafe_allow_html=True)
else:
st.error("🚨 Something went wrong with the analysis.")
else:
st.warning("⚠️ Please enter some text to analyze.")
# Footer with authorship
st.markdown("---")
st.markdown("""
<footer>
Built with ❀️ by
<a href="https://github.com/sumonta056" target="_blank">Sumonta Saha Mridul</a>,
<a href="https://github.com/promimojumder08" target="_blank">Promi Mojumder</a>.
</footer>
""", unsafe_allow_html=True)