File size: 884 Bytes
3ccf31a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
os.environ["TRANSFORMERS_NO_TF"] = "1"  # Still useful just in case

from transformers import pipeline

# ✅ Explicitly set framework='pt' to skip Keras/TensorFlow
classifier = pipeline(
    "sentiment-analysis",
    model="cardiffnlp/twitter-roberta-base-sentiment",
    framework="pt"
)

def classify_text(text: str) -> str:
    if not text.strip():
        return "No input"

    result = classifier(text)[0]
    label_map = {
        "LABEL_0": "Negative 😡", # <-- emojis for fun, fun, fun
        "LABEL_1": "Neutral 😐",
        "LABEL_2": "Positive 😍"
    }
    label = label_map.get(result["label"], result["label"])
    score = round(result["score"] * 100, 1)

    if score >= 80:
        confidence = "Definitely"
    elif score >= 60:
        confidence = "Likely"
    else:
        confidence = "Possibly"

    return f"{confidence} {label} ({score}%)"