Spaces:
Sleeping
Sleeping
File size: 2,556 Bytes
3e37b99 4dfda8e 3e37b99 4dfda8e 3e37b99 4dfda8e 3e37b99 4dfda8e 3e37b99 4dfda8e 3e37b99 4dfda8e 3e37b99 4dfda8e 3e37b99 4dfda8e 3e37b99 4dfda8e 3e37b99 4dfda8e 3e37b99 4dfda8e | 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | import gradio as gr
import torch
import re
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from arabert.preprocess import ArabertPreprocessor
import torch.nn.functional as F
# =========================
# Model configuration
# =========================
MODEL_PATH = "arabert_sentiment_model"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
model.to(device)
model.eval()
arabert_prep = ArabertPreprocessor(
model_name="aubmindlab/bert-base-arabertv02"
)
label_map = {0: "negative", 1: "positive"}
# =========================
# Arabic validation (REGEX)
# =========================
def is_mostly_arabic(text, threshold=0.6):
if not isinstance(text, str) or len(text.strip()) == 0:
return False
arabic_pattern = re.compile(
r'[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]'
)
arabic_chars = arabic_pattern.findall(text)
letters = re.findall(r'\w', text)
if len(letters) == 0:
return False
return len(arabic_chars) / len(letters) >= threshold
# =========================
# Prediction function
# =========================
def predict(text):
text = text.strip()
# 🔒 Validation arabe
if not is_mostly_arabic(text):
return "Arabic text only ❌", "—"
if len(text.split()) < 3:
return "Sentence too short ❌", "—"
# 🧹 AraBERT preprocessing
clean_text = arabert_prep.preprocess(text)
# 🔢 Tokenization
inputs = tokenizer(
clean_text,
return_tensors="pt",
truncation=True,
padding=True,
max_length=128
)
inputs = {k: v.to(device) for k, v in inputs.items()}
# 🤖 Prediction
with torch.no_grad():
probs = torch.softmax(model(**inputs).logits, dim=1)
conf, pred = torch.max(probs, dim=1)
return (
label_map[pred.item()].capitalize(),
f"{round(conf.item() * 100, 2)} %"
)
# =========================
# Gradio Interface
# =========================
gr.Interface(
fn=predict,
inputs=gr.Textbox(
lines=3,
placeholder="أدخل جملة عربية هنا..."
),
outputs=[
gr.Textbox(label="Sentiment"),
gr.Textbox(label="Confidence")
],
title="Arabic Sentiment Analysis (AraBERT)",
description="تحليل المشاعر للنصوص العربية باستخدام AraBERT"
).launch() |