File size: 2,242 Bytes
c885400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
import numpy as np
from transformers import BertForSequenceClassification, BertTokenizer
import requests
import json

# Load model and tokenizer from Hugging Face Hub
repo_id = "logasanjeev/goemotions-bert"
model = BertForSequenceClassification.from_pretrained(repo_id)
tokenizer = BertTokenizer.from_pretrained(repo_id)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)
if torch.cuda.device_count() > 1:
    model = nn.DataParallel(model)
model.eval()

# Load optimized thresholds from Hugging Face Hub
thresholds_url = f"https://huggingface.co/{repo_id}/raw/main/thresholds.json"
response = requests.get(thresholds_url)
thresholds_data = json.loads(response.text)
emotion_labels = thresholds_data["emotion_labels"]
best_thresholds = thresholds_data["thresholds"]

# Prediction function
def predict_emotions(text):
    encodings = tokenizer(
        text,
        padding='max_length',
        truncation=True,
        max_length=128,
        return_tensors='pt'
    )
    input_ids = encodings['input_ids'].to(device)
    attention_mask = encodings['attention_mask'].to(device)
    
    with torch.no_grad():
        outputs = model(input_ids, attention_mask=attention_mask)
        logits = torch.sigmoid(outputs.logits).cpu().numpy()[0]
    
    predictions = []
    for i, (logit, thresh) in enumerate(zip(logits, best_thresholds)):
        if logit >= thresh:
            predictions.append((emotion_labels[i], logit))
    
    predictions.sort(key=lambda x: x[1], reverse=True)
    if not predictions:
        return "No emotions predicted above thresholds."
    
    return "\n".join([f"{emotion}: {confidence:.4f}" for emotion, confidence in predictions])

# Gradio interface
interface = gr.Interface(
    fn=predict_emotions,
    inputs=gr.Textbox(lines=2, placeholder="Enter your text here..."),
    outputs="text",
    title="GoEmotions BERT Classifier",
    description="Predict emotions using a fine-tuned BERT-base model from logasanjeev/goemotions-bert.",
    examples=[
        "I’m just chilling today.",
        "Thank you for saving my life!",
        "I’m nervous about my exam tomorrow."
    ]
)

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