Spaces:
Sleeping
Sleeping
| 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() | |