File size: 1,166 Bytes
0551bb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import torch.nn.functional as F

# Load model and tokenizer
model_name = "Omartificial-Intelligence-Space/SA-BERT-Classifier"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

# Put model in eval mode
model.eval()

# Inference function
def classify_sentiment(text):
    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits
        probs = F.softmax(logits, dim=1).squeeze()

    # Map class indices to human-readable labels if known (example below)
    labels = ["negative", "neutral", "positive"]
    top_class = torch.argmax(probs).item()
    return {labels[i]: float(probs[i]) for i in range(len(labels))}

# Gradio Interface
interface = gr.Interface(
    fn=classify_sentiment,
    inputs=gr.Textbox(lines=2, placeholder="Enter your text here..."),
    outputs=gr.Label(num_top_classes=3),
    title="Arabic Sentiment Classifier (SA-BERT)"
)

interface.launch()