Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
-
|
2 |
import re
|
3 |
from transformers import pipeline as hf_pipeline
|
4 |
|
5 |
-
# Load SST model
|
6 |
sst_classifier = hf_pipeline(
|
7 |
"text-classification",
|
8 |
model="distilbert-base-uncased-finetuned-sst-2-english",
|
9 |
-
top_k=
|
10 |
truncation=True
|
11 |
)
|
12 |
|
@@ -18,7 +18,7 @@ emotion_pipeline = hf_pipeline(
|
|
18 |
truncation=True
|
19 |
)
|
20 |
|
21 |
-
# Lexicon
|
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"}
|
@@ -49,14 +49,12 @@ def preprocess_sentiment_text(text):
|
|
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)
|
@@ -82,19 +80,22 @@ def get_emotional_tone_tag(emotions, sentiment, patterns, abuse_score=0):
|
|
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)
|
89 |
-
sentiment = sst_output[0]
|
90 |
-
sentiment_label = "supportive" if sentiment["label"] == "POSITIVE" else "undermining"
|
91 |
-
sentiment_score = round(sentiment["score"] * 100, 2)
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
emotions = get_emotion_profile(text)
|
94 |
emotion_summary = "\n".join([f"{k.title()}: {v:.2f}" for k, v in emotions.items()])
|
95 |
|
96 |
-
#
|
97 |
-
|
|
|
98 |
tone_output = tone_tag if tone_tag else "None detected"
|
99 |
|
100 |
return (
|
@@ -102,13 +103,13 @@ def analyze_message(text):
|
|
102 |
f"🎭 Emotional Profile:\n{emotion_summary}\n\n"
|
103 |
f"🔍 Tone Tag: {tone_output}"
|
104 |
)
|
105 |
-
|
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
|
112 |
)
|
113 |
|
114 |
iface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
import re
|
3 |
from transformers import pipeline as hf_pipeline
|
4 |
|
5 |
+
# Load SST model
|
6 |
sst_classifier = hf_pipeline(
|
7 |
"text-classification",
|
8 |
model="distilbert-base-uncased-finetuned-sst-2-english",
|
9 |
+
top_k=1,
|
10 |
truncation=True
|
11 |
)
|
12 |
|
|
|
18 |
truncation=True
|
19 |
)
|
20 |
|
21 |
+
# Lexicon rules
|
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"}
|
|
|
49 |
|
50 |
return " ".join(modified)
|
51 |
|
|
|
52 |
def get_emotion_profile(text):
|
53 |
emotions = emotion_pipeline(text)
|
54 |
if isinstance(emotions, list) and isinstance(emotions[0], list):
|
55 |
emotions = emotions[0]
|
56 |
return {e['label'].lower(): round(e['score'], 3) for e in emotions}
|
57 |
|
|
|
58 |
def get_emotional_tone_tag(emotions, sentiment, patterns, abuse_score=0):
|
59 |
sadness = emotions.get("sadness", 0)
|
60 |
joy = emotions.get("joy", 0)
|
|
|
80 |
|
81 |
return None
|
82 |
|
|
|
83 |
def analyze_message(text):
|
84 |
preprocessed = preprocess_sentiment_text(text)
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
# Run sentiment classification
|
87 |
+
sst_result = sst_classifier(preprocessed)
|
88 |
+
sentiment_output = sst_result[0] if isinstance(sst_result, list) else sst_result
|
89 |
+
sentiment_label = "supportive" if sentiment_output["label"] == "POSITIVE" else "undermining"
|
90 |
+
sentiment_score = round(sentiment_output["score"] * 100, 2)
|
91 |
+
|
92 |
+
# Run emotion model
|
93 |
emotions = get_emotion_profile(text)
|
94 |
emotion_summary = "\n".join([f"{k.title()}: {v:.2f}" for k, v in emotions.items()])
|
95 |
|
96 |
+
# No abuse patterns yet
|
97 |
+
patterns = []
|
98 |
+
tone_tag = get_emotional_tone_tag(emotions, sentiment_label, patterns)
|
99 |
tone_output = tone_tag if tone_tag else "None detected"
|
100 |
|
101 |
return (
|
|
|
103 |
f"🎭 Emotional Profile:\n{emotion_summary}\n\n"
|
104 |
f"🔍 Tone Tag: {tone_output}"
|
105 |
)
|
106 |
+
|
107 |
iface = gr.Interface(
|
108 |
fn=analyze_message,
|
109 |
inputs=gr.Textbox(lines=4, placeholder="Paste a message here..."),
|
110 |
outputs="text",
|
111 |
title="Tether SST + Emotional Tone Tagger",
|
112 |
+
description="Applies lexicon-enhanced preprocessing, classifies sentiment, profiles emotion, and infers tone tags using behavioral logic."
|
113 |
)
|
114 |
|
115 |
iface.launch()
|