aieeshashafique commited on
Commit
0551bb3
·
verified ·
1 Parent(s): 78812ca

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+ import torch.nn.functional as F
5
+
6
+ # Load model and tokenizer
7
+ model_name = "Omartificial-Intelligence-Space/SA-BERT-Classifier"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
10
+
11
+ # Put model in eval mode
12
+ model.eval()
13
+
14
+ # Inference function
15
+ def classify_sentiment(text):
16
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
17
+ with torch.no_grad():
18
+ outputs = model(**inputs)
19
+ logits = outputs.logits
20
+ probs = F.softmax(logits, dim=1).squeeze()
21
+
22
+ # Map class indices to human-readable labels if known (example below)
23
+ labels = ["negative", "neutral", "positive"]
24
+ top_class = torch.argmax(probs).item()
25
+ return {labels[i]: float(probs[i]) for i in range(len(labels))}
26
+
27
+ # Gradio Interface
28
+ interface = gr.Interface(
29
+ fn=classify_sentiment,
30
+ inputs=gr.Textbox(lines=2, placeholder="Enter your text here..."),
31
+ outputs=gr.Label(num_top_classes=3),
32
+ title="Arabic Sentiment Classifier (SA-BERT)"
33
+ )
34
+
35
+ interface.launch()