| import streamlit as st | |
| from textblob import TextBlob | |
| def classify_mood(text, pos_thr=0.30, neg_thr=-0.30): | |
| bad_words = ["stupid", "hate", "idiot", "dumb", "kill", "moron", "loser"] | |
| if text is None or text.strip() == "": | |
| return "Empty Text", "Please write something to analyze" | |
| lower_text = text.lower() | |
| for bw in bad_words: | |
| if bw in lower_text: | |
| return "inappropriate", "Inappropriate Words" | |
| blob = TextBlob(text) | |
| polarity = blob.sentiment.polarity | |
| if polarity >= pos_thr: | |
| return "happy", "Happy" | |
| elif polarity <= neg_thr: | |
| return "sad", "Sad" | |
| else: | |
| return "neutral", "Neutral" | |
| st.set_page_config(page_title="Kid-safe Mood Detector", layout="centered") | |
| st.title("Kid-safe Text-Mood Detector") | |
| st.write("Pick sensitivity level --> Type a text --> Press Analyze --> Get Emotion") | |
| preset = st.selectbox("Sensitivity Level:", ("Balanced (±0.30)", "Strict (±0.35)", "Sensitive (±0.25)"), index=0) | |
| if preset == "Strict (±0.35)": | |
| pos_thr = 0.35 | |
| neg_thr = -0.35 | |
| elif preset == "Sensitive (±0.25)": | |
| pos_thr = 0.25 | |
| neg_thr = -0.25 | |
| else: | |
| pos_thr = 0.30 | |
| neg_thr = -0.30 | |
| st.markdown(f"**Current thresholds:** Positive ≥ +{pos_thr:.2f} | Negative ≤ {neg_thr:.2f}") | |
| with st.form("input_form"): | |
| user_text = st.text_input("Write a text:") | |
| analyze = st.form_submit_button("Analyze") | |
| if analyze: | |
| mood, message = classify_mood(user_text, pos_thr, neg_thr) | |
| if mood == "empty": | |
| emoji = "🥱" | |
| elif mood == "inappropriate": | |
| emoji = "⚠️" | |
| elif mood == "happy": | |
| emoji = "😀" | |
| elif mood == "sad": | |
| emoji = "😞" | |
| else: | |
| emoji = "😐" | |
| st.markdown(f"### {emoji} {message}") | |