File size: 1,760 Bytes
e4ddd05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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}")