| import gradio as gr
|
| import torch
|
| from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| import re
|
|
|
|
|
|
|
| MODEL_NAME = "goalgamal/AraBERT-Arabic-Sentiment"
|
|
|
|
|
| LABELS = {
|
| 0: "Negative 😞",
|
| 1: "Neutral 😐",
|
| 2: "Positive 😃"
|
| }
|
|
|
|
|
| print(f"Loading model: {MODEL_NAME}...")
|
| try:
|
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| print("Model loaded successfully!")
|
| except Exception as e:
|
| print(f"Error loading model: {e}")
|
| raise e
|
|
|
|
|
|
|
| def clean_text(text):
|
| if not isinstance(text, str):
|
| return ""
|
|
|
|
|
| text = re.sub(r'http\S+', '', text)
|
| text = re.sub(r'<.*?>', '', text)
|
|
|
|
|
|
|
| text = re.sub(r'[^\w\s\u0600-\u06FF]', ' ', text)
|
|
|
|
|
| text = re.sub(r'[أإآ]', 'ا', text)
|
|
|
| text = re.sub(r'ة', 'ه', text)
|
|
|
| return text.strip()
|
|
|
|
|
| def predict(text):
|
|
|
| cleaned_text = clean_text(text)
|
|
|
|
|
| inputs = tokenizer(
|
| cleaned_text,
|
| return_tensors="pt",
|
| truncation=True,
|
| padding=True,
|
| max_length=128
|
| )
|
|
|
|
|
| with torch.no_grad():
|
| outputs = model(**inputs)
|
|
|
|
|
| probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
|
|
|
|
|
|
| results = {}
|
| for idx, score in enumerate(probs[0]):
|
| label_text = LABELS[idx]
|
| results[label_text] = float(score)
|
|
|
| return results
|
|
|
|
|
|
|
| demo = gr.Interface(
|
| fn=predict,
|
| inputs=gr.Textbox(
|
| label="أدخل تعليق الطالب (Enter Student Feedback)",
|
| placeholder="اكتب هنا... (مثال: الشرح كان ممتاز واستفدت جدا)",
|
| lines=3,
|
| text_align="right"
|
| ),
|
| outputs=gr.Label(label="Sentiment Analysis Result", num_top_classes=3),
|
| title="📊 Arabic Course Feedback Analyzer",
|
| description="""
|
| This is an AI-powered tool to analyze student feedback using **Deep Learning (AraBERT)**.
|
| It detects whether the sentiment is **Positive**, **Negative**, or **Neutral**.
|
| """,
|
| examples=[
|
| ["الكورس ممتاز والشرح كان رائع جدا"],
|
| ["بصراحة ضيعت وقتي، المحتوى ضعيف"],
|
| ["الكورس عادي يعني لا وحش ولا حلو"],
|
| ["الشرح كويس بس الصوت كان واطي في بعض الفيديوهات"]
|
| ],
|
| theme=gr.themes.Soft()
|
| )
|
|
|
|
|
| if __name__ == "__main__":
|
| demo.launch() |