File size: 980 Bytes
aafc4c1
 
 
 
 
 
 
 
266535f
aafc4c1
22d0c4f
 
aafc4c1
6d97e8d
266535f
aafc4c1
 
266535f
22d0c4f
6d97e8d
 
 
22d0c4f
6d97e8d
aafc4c1
22d0c4f
 
 
6d97e8d
22d0c4f
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import numpy as np

model_name = "Ploypatcha/my-model-upload"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
model.eval()

labels = ["happy", "love", "angry", "sadness", "fear", "trust",
          "disgust", "surprise", "anticipation", "optimism", "pessimism"]

def predict(text):
    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(model.device)
    with torch.no_grad():
        outputs = model(**inputs)
        probs = torch.sigmoid(outputs.logits)[0].cpu().numpy()

    max_idx = int(np.argmax(probs))
    max_label = labels[max_idx]
    max_score = round(probs[max_idx] * 100)

    return f"{max_label} ({max_score}%)"

gr.Interface(
    fn=predict,
    inputs=gr.Textbox(label="Enter english comment"),
    outputs=gr.Text(label="Top Emotion")
).launch()