SamanthaStorm commited on
Commit
fe6b66c
·
verified ·
1 Parent(s): 31f28ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -1
app.py CHANGED
@@ -1,3 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def analyze_message(text):
2
  preprocessed = preprocess_sentiment_text(text)
3
  sst_output = sst_classifier(preprocessed)
@@ -16,4 +101,14 @@ def analyze_message(text):
16
  f"🧠 Sentiment: {sentiment_label.title()} ({sentiment_score}%)\n\n"
17
  f"🎭 Emotional Profile:\n{emotion_summary}\n\n"
18
  f"🔍 Tone Tag: {tone_output}"
19
- )
 
 
 
 
 
 
 
 
 
 
 
1
+ : import gradio as gr
2
+ import re
3
+ from transformers import pipeline as hf_pipeline
4
+
5
+ # Load SST model (temporary baseline)
6
+ sst_classifier = hf_pipeline(
7
+ "text-classification",
8
+ model="distilbert-base-uncased-finetuned-sst-2-english",
9
+ top_k=None,
10
+ truncation=True
11
+ )
12
+
13
+ # Load emotion classifier
14
+ emotion_pipeline = hf_pipeline(
15
+ "text-classification",
16
+ model="j-hartmann/emotion-english-distilroberta-base",
17
+ top_k=None,
18
+ truncation=True
19
+ )
20
+
21
+ # Lexicon enhancement preprocessing
22
+ negations = {"not", "never", "no", "none", "nobody", "nothing", "neither", "nowhere", "hardly", "scarcely", "barely"}
23
+ amplifiers = {"very", "really", "extremely", "so", "totally", "completely", "absolutely", "utterly", "super"}
24
+ softeners = {"slightly", "somewhat", "a bit", "a little", "mildly", "fairly", "kind of"}
25
+
26
+ def preprocess_sentiment_text(text):
27
+ words = text.lower().split()
28
+ modified = []
29
+ negate = False
30
+
31
+ for word in words:
32
+ stripped = re.sub(r'\W+', '', word)
33
+
34
+ if stripped in negations:
35
+ negate = True
36
+ modified.append("<NEG>")
37
+ continue
38
+ if stripped in amplifiers:
39
+ modified.append(f"<AMP>{word}")
40
+ continue
41
+ if stripped in softeners:
42
+ modified.append(f"<SOFT>{word}")
43
+ continue
44
+ if negate:
45
+ modified.append(f"<NEG>{word}")
46
+ negate = False
47
+ else:
48
+ modified.append(word)
49
+
50
+ return " ".join(modified)
51
+
52
+ # Emotion mapping
53
+ def get_emotion_profile(text):
54
+ emotions = emotion_pipeline(text)
55
+ if isinstance(emotions, list) and isinstance(emotions[0], list):
56
+ emotions = emotions[0]
57
+ return {e['label'].lower(): round(e['score'], 3) for e in emotions}
58
+
59
+ # Tone tagging logic
60
+ def get_emotional_tone_tag(emotions, sentiment, patterns, abuse_score=0):
61
+ sadness = emotions.get("sadness", 0)
62
+ joy = emotions.get("joy", 0)
63
+ neutral = emotions.get("neutral", 0)
64
+ disgust = emotions.get("disgust", 0)
65
+ anger = emotions.get("anger", 0)
66
+ fear = emotions.get("fear", 0)
67
+
68
+ if sadness > 0.4 and any(p in patterns for p in ["blame shifting", "guilt tripping", "recovery phase"]) and (sentiment == "undermining" or abuse_score > 40):
69
+ return "performative regret"
70
+ if (joy > 0.3 or sadness > 0.4) and any(p in patterns for p in ["control", "gaslighting"]) and sentiment == "undermining":
71
+ return "coercive warmth"
72
+ if (neutral + disgust) > 0.5 and any(p in patterns for p in ["dismissiveness", "projection", "obscure language"]) and sentiment == "undermining":
73
+ return "cold invalidation"
74
+ if (sadness + fear) > 0.5 and sentiment == "supportive" and all(p in ["recovery phase"] for p in patterns):
75
+ return "genuine vulnerability"
76
+ if (anger + disgust) > 0.5 and any(p in patterns for p in ["control", "threat", "insults", "dismissiveness"]) and sentiment == "undermining":
77
+ return "emotional threat"
78
+ if sadness > 0.6 and any(p in patterns for p in ["guilt tripping", "projection"]) and sentiment == "undermining":
79
+ return "weaponized sadness"
80
+ if neutral > 0.5 and any(p in patterns for p in ["dismissiveness", "obscure language"]) and sentiment == "undermining":
81
+ return "toxic resignation"
82
+
83
+ return None
84
+
85
+ # Main function
86
  def analyze_message(text):
87
  preprocessed = preprocess_sentiment_text(text)
88
  sst_output = sst_classifier(preprocessed)
 
101
  f"🧠 Sentiment: {sentiment_label.title()} ({sentiment_score}%)\n\n"
102
  f"🎭 Emotional Profile:\n{emotion_summary}\n\n"
103
  f"🔍 Tone Tag: {tone_output}"
104
+ )
105
+ # Interface
106
+ iface = gr.Interface(
107
+ fn=analyze_message,
108
+ inputs=gr.Textbox(lines=4, placeholder="Paste a message here..."),
109
+ outputs="text",
110
+ title="Tether SST + Emotional Tone Tagger",
111
+ description="Applies lexicon-enhanced preprocessing, classifies sentiment, profiles emotion, and infers tone tags based on behavior logic."
112
+ )
113
+
114
+ iface.launch()