Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
import re | |
# Load your SST model pipeline | |
sst_classifier = pipeline( | |
"text-classification", | |
model="SamanthaStorm/tether-sst", # replace with your model name | |
top_k=None, | |
truncation=True | |
) | |
# Define lexicons | |
negations = {"not", "never", "no", "none", "hardly", "rarely", "barely", "doesn’t", "isn’t", "wasn’t"} | |
amplifiers = {"very", "extremely", "really", "absolutely", "so", "incredibly", "deeply", "profoundly"} | |
diminishers = {"somewhat", "slightly", "barely", "kind of", "sort of", "a little"} | |
def apply_lexicon_rules(text): | |
tokens = text.lower().split() | |
lexicon_score = 0 | |
for i, token in enumerate(tokens): | |
if token in negations: | |
lexicon_score -= 1 | |
if token in amplifiers: | |
lexicon_score += 1 | |
if token in diminishers: | |
lexicon_score -= 0.5 | |
return lexicon_score | |
def classify_sentiment(text): | |
lex_score = apply_lexicon_rules(text) | |
result = sst_classifier(text)[0] | |
label = result["label"] | |
base_score = result["score"] | |
# Adjust model score based on lexicon features | |
adjusted_score = min(max(base_score + (lex_score * 0.05), 0), 1) | |
score_pct = round(adjusted_score * 100, 2) | |
if label == "LABEL_0": | |
return f"Supportive ({score_pct}%)" | |
elif label == "LABEL_1": | |
return f"Undermining ({score_pct}%)" | |
else: | |
return f"Unknown label ({label})" | |
iface = gr.Interface( | |
fn=classify_sentiment, | |
inputs=gr.Textbox(lines=4, placeholder="Paste message here..."), | |
outputs="text", | |
title="Tether SST (Lexicon-Enhanced)", | |
description="Combines transformer model with lexicon-based adjustment to classify Supportive or Undermining tone." | |
) | |
iface.launch() |