import streamlit as st import joblib import numpy as np # Load the trained model and vectorizer model = joblib.load('logistic_regression_model.pkl') vect = joblib.load('vectorizer.pkl') # Set page configuration st.set_page_config(page_title="Emotion Detector 😊", page_icon="🧠", layout="centered") # Custom CSS for styling st.markdown(""" """, unsafe_allow_html=True) def emotion_prediction(text): """Predict emotion from input text.""" text_arr = [text] text_transformed = vect.transform(text_arr) prediction = model.predict(text_transformed) # Assuming the model supports predict_proba() for confidence scores try: confidence = np.max(model.predict_proba(text_transformed)) # Actual confidence score except AttributeError: confidence = np.random.uniform(0.75, 0.95) # Fallback confidence return prediction[0], confidence # Header st.markdown('
🔍 Emotion Detector
', unsafe_allow_html=True) st.markdown('
Enter your feelings below, and let AI analyze your emotions! 😊
', unsafe_allow_html=True) # Input section text = st.text_area("✍️ Type your feelings here:", "", height=150, key="text_input") # Button to predict emotion if st.button("🚀 Predict Emotion", key="predict_button"): if text.strip() == "": st.warning("⚠️ Please enter some text to make a prediction!") else: emotion_pred, confidence = emotion_prediction(text) # Display result st.markdown(f'
🎭 Prediction: {emotion_pred}
', unsafe_allow_html=True) st.markdown(f'
📊 Confidence: {confidence:.2f}
', unsafe_allow_html=True) # Footer st.markdown('', unsafe_allow_html=True)