aieeshashafique's picture
Create app.py
0551bb3 verified
raw
history blame
1.17 kB
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()