|
import streamlit as st |
|
from transformers import BertForSequenceClassification, BertTokenizerFast |
|
from emotion_utils import predict |
|
|
|
|
|
model_path = "./model/" |
|
model = BertForSequenceClassification.from_pretrained(model_path) |
|
tokenizer = BertTokenizerFast.from_pretrained(model_path) |
|
|
|
|
|
def analyze_sentiment(text): |
|
if text.strip(): |
|
probs, _, label = predict(text, model, tokenizer) |
|
score = probs.max().item() |
|
return label, score |
|
else: |
|
return None, None |
|
|
|
|
|
def get_emoji(label): |
|
if label == "Anger": |
|
return "π " |
|
elif label == "Astonished": |
|
return "π²" |
|
elif label == "Optimistic": |
|
return "π" |
|
elif label == "Sadness": |
|
return "π’" |
|
else: |
|
return "π" |
|
|
|
|
|
st.set_page_config( |
|
page_title="G-Bert: Emotion Analysis", |
|
page_icon="π", |
|
layout="centered" |
|
) |
|
|
|
|
|
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) |
|
|
|
|
|
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. |
|
""") |
|
|
|
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...") |
|
|
|
|
|
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.") |
|
|
|
|
|
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) |
|
|