Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,48 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
-
# Load SST
|
5 |
sst_classifier = pipeline(
|
6 |
"text-classification",
|
7 |
-
model="SamanthaStorm/tether-sst", # replace with your
|
8 |
top_k=None,
|
9 |
truncation=True
|
10 |
)
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def classify_sentiment(text):
|
|
|
13 |
result = sst_classifier(text)[0]
|
14 |
label = result["label"]
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
|
17 |
if label == "LABEL_0":
|
18 |
-
return f"Supportive ({
|
19 |
elif label == "LABEL_1":
|
20 |
-
return f"Undermining ({
|
21 |
else:
|
22 |
return f"Unknown label ({label})"
|
23 |
|
@@ -25,8 +50,8 @@ iface = gr.Interface(
|
|
25 |
fn=classify_sentiment,
|
26 |
inputs=gr.Textbox(lines=4, placeholder="Paste message here..."),
|
27 |
outputs="text",
|
28 |
-
title="Tether SST
|
29 |
-
description="
|
30 |
)
|
31 |
|
32 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
import re
|
4 |
|
5 |
+
# Load your SST model pipeline
|
6 |
sst_classifier = pipeline(
|
7 |
"text-classification",
|
8 |
+
model="SamanthaStorm/tether-sst", # replace with your model name
|
9 |
top_k=None,
|
10 |
truncation=True
|
11 |
)
|
12 |
|
13 |
+
# Define lexicons
|
14 |
+
negations = {"not", "never", "no", "none", "hardly", "rarely", "barely", "doesn’t", "isn’t", "wasn’t"}
|
15 |
+
amplifiers = {"very", "extremely", "really", "absolutely", "so", "incredibly", "deeply", "profoundly"}
|
16 |
+
diminishers = {"somewhat", "slightly", "barely", "kind of", "sort of", "a little"}
|
17 |
+
|
18 |
+
def apply_lexicon_rules(text):
|
19 |
+
tokens = text.lower().split()
|
20 |
+
lexicon_score = 0
|
21 |
+
|
22 |
+
for i, token in enumerate(tokens):
|
23 |
+
if token in negations:
|
24 |
+
lexicon_score -= 1
|
25 |
+
if token in amplifiers:
|
26 |
+
lexicon_score += 1
|
27 |
+
if token in diminishers:
|
28 |
+
lexicon_score -= 0.5
|
29 |
+
|
30 |
+
return lexicon_score
|
31 |
+
|
32 |
def classify_sentiment(text):
|
33 |
+
lex_score = apply_lexicon_rules(text)
|
34 |
result = sst_classifier(text)[0]
|
35 |
label = result["label"]
|
36 |
+
base_score = result["score"]
|
37 |
+
|
38 |
+
# Adjust model score based on lexicon features
|
39 |
+
adjusted_score = min(max(base_score + (lex_score * 0.05), 0), 1)
|
40 |
+
score_pct = round(adjusted_score * 100, 2)
|
41 |
|
42 |
if label == "LABEL_0":
|
43 |
+
return f"Supportive ({score_pct}%)"
|
44 |
elif label == "LABEL_1":
|
45 |
+
return f"Undermining ({score_pct}%)"
|
46 |
else:
|
47 |
return f"Unknown label ({label})"
|
48 |
|
|
|
50 |
fn=classify_sentiment,
|
51 |
inputs=gr.Textbox(lines=4, placeholder="Paste message here..."),
|
52 |
outputs="text",
|
53 |
+
title="Tether SST (Lexicon-Enhanced)",
|
54 |
+
description="Combines transformer model with lexicon-based adjustment to classify Supportive or Undermining tone."
|
55 |
)
|
56 |
|
57 |
iface.launch()
|