Keyurjotaniya007 commited on
Commit
e4ddd05
·
verified ·
1 Parent(s): d0ab8dd

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +61 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from textblob import TextBlob
3
+
4
+ def classify_mood(text, pos_thr=0.30, neg_thr=-0.30):
5
+ bad_words = ["stupid", "hate", "idiot", "dumb", "kill", "moron", "loser"]
6
+
7
+ if text is None or text.strip() == "":
8
+ return "Empty Text", "Please write something to analyze"
9
+
10
+ lower_text = text.lower()
11
+ for bw in bad_words:
12
+ if bw in lower_text:
13
+ return "inappropriate", "Inappropriate Words"
14
+
15
+ blob = TextBlob(text)
16
+ polarity = blob.sentiment.polarity
17
+
18
+ if polarity >= pos_thr:
19
+ return "happy", "Happy"
20
+ elif polarity <= neg_thr:
21
+ return "sad", "Sad"
22
+ else:
23
+ return "neutral", "Neutral"
24
+
25
+ st.set_page_config(page_title="Kid-safe Mood Detector", layout="centered")
26
+ st.title("Kid-safe Text-Mood Detector")
27
+ st.write("Pick sensitivity level --> Type a text --> Press Analyze --> Get Emotion")
28
+
29
+ preset = st.selectbox("Sensitivity Level:", ("Balanced (±0.30)", "Strict (±0.35)", "Sensitive (±0.25)"), index=0)
30
+
31
+ if preset == "Strict (±0.35)":
32
+ pos_thr = 0.35
33
+ neg_thr = -0.35
34
+ elif preset == "Sensitive (±0.25)":
35
+ pos_thr = 0.25
36
+ neg_thr = -0.25
37
+ else:
38
+ pos_thr = 0.30
39
+ neg_thr = -0.30
40
+
41
+ st.markdown(f"**Current thresholds:** Positive ≥ +{pos_thr:.2f} | Negative ≤ {neg_thr:.2f}")
42
+
43
+ with st.form("input_form"):
44
+ user_text = st.text_input("Write a text:")
45
+ analyze = st.form_submit_button("Analyze")
46
+
47
+ if analyze:
48
+ mood, message = classify_mood(user_text, pos_thr, neg_thr)
49
+
50
+ if mood == "empty":
51
+ emoji = "🥱"
52
+ elif mood == "inappropriate":
53
+ emoji = "⚠️"
54
+ elif mood == "happy":
55
+ emoji = "😀"
56
+ elif mood == "sad":
57
+ emoji = "😞"
58
+ else:
59
+ emoji = "😐"
60
+
61
+ st.markdown(f"### {emoji} {message}")
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit>=1.20
2
+ textblob>=0.17