File size: 972 Bytes
7b38ef8
d247a58
 
 
7b38ef8
d247a58
 
 
 
 
 
7b38ef8
 
d247a58
 
 
 
 
 
 
7b38ef8
 
 
 
 
d247a58
 
7b38ef8
 
 
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 torch.nn.functional as F

# Load RoBERTa model
model_name = "cardiffnlp/twitter-roberta-base-sentiment"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

labels = ["Negative", "Neutral", "Positive"]

def analyze_sentiment(text):
    inputs = tokenizer(text, return_tensors="pt")
    with torch.no_grad():
        logits = model(**inputs).logits
    probs = F.softmax(logits, dim=1)[0]
    pred = torch.argmax(probs).item()
    confidence = probs[pred].item() * 100
    return f"{labels[pred]} ({confidence:.2f}%)"

iface = gr.Interface(
    fn=analyze_sentiment,
    inputs="text",
    outputs="text",
    title="RoBERTa-Based Sentiment Analyzer",
    description="Uses CardiffNLP's sentiment model. Classifies as Positive, Neutral, or Negative with confidence."
)

iface.launch()