Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,26 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
demo.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 the model and tokenizer
|
| 7 |
+
model_name = "tabularisai/multilingual-sentiment-analysis"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 10 |
|
| 11 |
+
# Define labels
|
| 12 |
+
labels = ["Negative", "Neutral", "Positive"]
|
| 13 |
+
|
| 14 |
+
# Define inference function
|
| 15 |
+
def analyze_sentiment(text):
|
| 16 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True)
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
outputs = model(**inputs)
|
| 19 |
+
probs = F.softmax(outputs.logits, dim=1)
|
| 20 |
+
confidence, predicted_class = torch.max(probs, dim=1)
|
| 21 |
+
label = labels[predicted_class.item()]
|
| 22 |
+
return f"{label} ({confidence.item():.2%} confidence)"
|
| 23 |
+
|
| 24 |
+
# Gradio interface
|
| 25 |
+
demo = gr.Interface(fn=analyze_sentiment, inputs="text", outputs="text")
|
| 26 |
demo.launch()
|