File size: 1,819 Bytes
292d77d
321e2dc
292d77d
321e2dc
 
 
 
 
f4b35df
e7103d3
 
 
 
 
f4b35df
 
321e2dc
 
 
 
 
 
f4b35df
e7103d3
 
 
 
 
 
 
 
 
 
 
321e2dc
 
 
 
e7103d3
 
 
 
 
321e2dc
 
 
 
 
e7103d3
 
 
 
 
321e2dc
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# Load the model from Hugging Face Model Hub
model_name = "SamLowe/roberta-base-go_emotions"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

# Define emotion labels used by the model
emotion_labels = ["admiration", "amusement", "anger", "annoyance", "approval",
                  "caring", "confusion", "curiosity", "desire", "disappointment",
                  "disapproval", "disgust", "embarrassment", "excitement",
                  "fear", "gratitude", "grief", "joy", "love", "nervousness",
                  "optimism", "pride", "realization", "relief", "remorse",
                  "sadness", "surprise", "neutral"]

def predict_emotion(text):
    inputs = tokenizer(text, return_tensors="pt")
    outputs = model(**inputs)
    logits = outputs.logits
    predicted_class = logits.argmax().item()

    predicted_emotion = emotion_labels[predicted_class]
    confidence = logits.softmax(dim=1).squeeze()[predicted_class].item()
    return predicted_emotion, confidence  # Return the predicted emotion and confidence

def get_confidence_color(confidence):
    # Define a color scale based on confidence
    if confidence >= 0.8:
        return "green"
    elif confidence >= 0.5:
        return "orange"
    else:
        return "red"

iface = gr.Interface(
    fn=predict_emotion,
    inputs=gr.Textbox(),
    outputs=[
        gr.Textbox(),
        "text",
        gr.Textbox("Confidence Score:", default=""),
    ],
    live=True,
    title="Emotion Prediction",
    description="Enter a sentence for emotion prediction.",
)

# Customize the interface appearance
iface.style(
    confidence_score=get_confidence_color
)

iface.launch()