Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def analyze_sentiment(text):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
iface = gr.Interface(
|
| 15 |
fn=analyze_sentiment,
|
| 16 |
inputs="text",
|
| 17 |
outputs="text",
|
| 18 |
-
title="
|
| 19 |
-
description="
|
| 20 |
)
|
| 21 |
|
| 22 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
|
| 6 |
+
# Load RoBERTa model
|
| 7 |
+
model_name = "cardiffnlp/twitter-roberta-base-sentiment"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
labels = ["Negative", "Neutral", "Positive"]
|
| 12 |
|
| 13 |
def analyze_sentiment(text):
|
| 14 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
logits = model(**inputs).logits
|
| 17 |
+
probs = F.softmax(logits, dim=1)[0]
|
| 18 |
+
pred = torch.argmax(probs).item()
|
| 19 |
+
confidence = probs[pred].item() * 100
|
| 20 |
+
return f"{labels[pred]} ({confidence:.2f}%)"
|
| 21 |
|
| 22 |
iface = gr.Interface(
|
| 23 |
fn=analyze_sentiment,
|
| 24 |
inputs="text",
|
| 25 |
outputs="text",
|
| 26 |
+
title="RoBERTa-Based Sentiment Analyzer",
|
| 27 |
+
description="Uses CardiffNLP's sentiment model. Classifies as Positive, Neutral, or Negative with confidence."
|
| 28 |
)
|
| 29 |
|
| 30 |
iface.launch()
|