File size: 2,360 Bytes
8a76365
 
c131857
8a76365
c131857
 
 
 
 
8a76365
 
 
 
 
c131857
 
 
 
 
8a76365
 
c131857
 
8a76365
 
 
 
c131857
 
 
 
 
 
8a76365
 
 
 
 
c131857
8a76365
 
 
c131857
 
 
8a76365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c131857
 
 
 
 
8a76365
 
 
 
c131857
 
8a76365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c131857
 
8a76365
 
c131857
8a76365
 
c131857
 
 
8a76365
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

import gradio as gr
import spaces
import torch

from transformers import (
    AutoTokenizer,
    AutoModelForSequenceClassification
)

MODEL_ID = "Pavan97/pavan_EA_us_latest"
THRESHOLD = 0.25

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)

model = AutoModelForSequenceClassification.from_pretrained(
    MODEL_ID
)

model.eval()


@spaces.GPU(duration=60)
def predict_emotions(text):
    if not text or not text.strip():
        return {}, "Please enter some text."

    device = torch.device(
        "cuda" if torch.cuda.is_available() else "cpu"
    )

    model.to(device)

    inputs = tokenizer(
        text,
        return_tensors="pt",
        truncation=True,
        max_length=256
    ).to(device)

    with torch.inference_mode():
        logits = model(**inputs).logits[0]
        probabilities = torch.sigmoid(
            logits
        ).cpu().tolist()

    scores = {
        model.config.id2label[i]: float(prob)
        for i, prob in enumerate(probabilities)
    }

    detected = {
        emotion: score
        for emotion, score in scores.items()
        if score >= THRESHOLD
    }

    top_five = dict(
        sorted(
            scores.items(),
            key=lambda item: item[1],
            reverse=True
        )[:5]
    )

    if detected:
        summary = ", ".join(
            sorted(
                detected,
                key=detected.get,
                reverse=True
            )
        )
    else:
        summary = "No emotion exceeded the threshold."

    model.to("cpu")

    return top_five, summary


demo = gr.Interface(
    fn=predict_emotions,
    inputs=gr.Textbox(
        lines=4,
        label="Enter your text",
        placeholder="Type something to analyze..."
    ),
    outputs=[
        gr.Label(
            num_top_classes=5,
            label="Top Emotion Scores"
        ),
        gr.Textbox(label="Detected Emotions")
    ],
    title="EA US Pavan — Emotion Analysis",
    description=(
        "AI-powered emotion detection using RoBERTa. "
        "Recognizes 28 emotions from text."
    ),
    examples=[
        ["I am excited about my new job!"],
        ["I feel nervous about my interview tomorrow."],
        ["Thank you so much for helping me!"],
        ["I am angry and disappointed."]
    ],
    cache_examples=False
)

if __name__ == "__main__":
    demo.launch()