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(""" """, 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 # 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"""
Confidence Score: {score:.2f}
""", unsafe_allow_html=True) # Add issue reporting section st.markdown(""" """, 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(""" """, unsafe_allow_html=True)