import streamlit as st import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification import pandas as pd # --- Page Configuration --- st.set_page_config( page_title="SENTINEL AI", page_icon="🤖", layout="wide" ) # --- Caching the Model --- @st.cache_resource def load_model(): print("🚀 Loading the fine-tuned RoBERTa model...") MODEL_PATH = "Shifterr/sentiment_model_roberta" # !! Make sure this matches your folder name !! tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH) model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH) print("✅ Model loaded successfully!") return tokenizer, model # Load the model and tokenizer tokenizer, model = load_model() labels = ['Negative emotion', 'Neutral', 'Positive emotion'] # --- Prediction Function (Upgraded to return probabilities) --- def predict_sentiment(text): if not text.strip(): return None, None inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True) with torch.no_grad(): logits = model(**inputs).logits probabilities = torch.nn.functional.softmax(logits, dim=-1)[0] predicted_class_id = probabilities.argmax().item() return labels[predicted_class_id], probabilities.numpy() # --- Building the User Interface --- # Custom CSS for a cool look st.markdown(""" """, unsafe_allow_html=True) # --- THIS IS THE NEW, UPGRADED TITLE SECTION --- st.markdown("

🤖 SENTINEL AI

", unsafe_allow_html=True) st.markdown("

A Twitter Sentiment Analyzer powered by RoBERTa

", unsafe_allow_html=True) st.write("---") # Adds a horizontal line for separation # --- User Input Section --- user_input = st.text_area("Enter a tweet to analyze:", "The new iPhone is amazing, I love it so much!", height=100) if st.button("Analyze Sentiment"): if not user_input.strip(): st.warning("Please enter some text to analyze.") else: with st.spinner("🧠 Sentinel is thinking..."): prediction, probs = predict_sentiment(user_input) st.header("Analysis Result") # Use columns for a cleaner layout col1, col2 = st.columns([1, 2]) # Column 1: Display the main prediction and emoji with col1: st.subheader("Predicted Sentiment") if prediction == 'Positive emotion': st.markdown(f"

{prediction} 👍

", unsafe_allow_html=True) elif prediction == 'Negative emotion': st.markdown(f"

{prediction} 👎

", unsafe_allow_html=True) else: # Neutral st.markdown(f"

{prediction} 😐

", unsafe_allow_html=True) # Column 2: Display the confidence score (probabilities) with col2: st.subheader("Confidence Score") prob_df = pd.DataFrame(probs, index=labels, columns=['Probability']) prob_df = prob_df.sort_values(by='Probability', ascending=False) for label, row in prob_df.iterrows(): st.write(f"**{label}:**") st.progress(float(row['Probability']))