Views
No views yet
| Metric | Score |
|---|---|
| Accuracy | 93.13% |
| F1 Score | 94.58% |
| Precision | 94.85% |
| Recall | 94.30% |
| AUC-ROC | 98.24% |
1import torch
2import numpy as np
3import re
4from transformers import AutoModelForSequenceClassification, AutoTokenizer
5
6class ArabicSemanticHighlighter:
7 def __init__(self, model_path):
8 self.model = AutoModelForSequenceClassification.from_pretrained(
9 model_path,
10 num_labels=1,
11 )
12 self.tokenizer = AutoTokenizer.from_pretrained(model_path)
13 self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
14 self.model.to(self.device)
15 self.model.eval()
16
17 def _split_sentences(self, text, language="ar"):
18 if language == "ar":
19 sentences = re.split(r'[.؟!。\n]', text)
20 else:
21 sentences = re.split(r'[.?!\n]', text)
22 return [s.strip() for s in sentences if s.strip() and len(s.strip()) > 5]
23
24 def _score_sentence(self, question, sentence):
25 inputs = self.tokenizer(
26 question, sentence,
27 truncation=True,
28 max_length=256,
29 padding='max_length',
30 return_tensors='pt'
31 ).to(self.device)
32
33 with torch.no_grad():
34 logit = self.model(**inputs).logits.squeeze().item()
35 return 1 / (1 + np.exp(-logit))
36
37 def process(self, question, context, threshold=0.5, language="auto", return_sentence_metrics=False):
38 """
39 Highlight relevant sentences in context based on the question.
40
41 Args:
42 question: Query string
43 context: Text to search for relevant sentences
44 threshold: Minimum probability for relevance (default: 0.5)
45 language: "ar", "en", or "auto"
46 return_sentence_metrics: Include probability scores
47
48 Returns:
49 dict with highlighted_sentences, all_sentences, and optionally sentence_probabilities
50 """
51 if language == "auto":
52 arabic_chars = len(re.findall(r'[\u0600-\u06FF]', context))
53 language = "ar" if arabic_chars > len(context) * 0.3 else "en"
54
55 sentences = self._split_sentences(context, language)
56 probabilities = []
57 highlighted = []
58
59 for sentence in sentences:
60 prob = self._score_sentence(question, sentence)
61 probabilities.append(prob)
62 if prob >= threshold:
63 highlighted.append(sentence)
64
65 result = {
66 "highlighted_sentences": highlighted,
67 "all_sentences": sentences,
68 }
69
70 if return_sentence_metrics:
71 result["sentence_probabilities"] = probabilities
72
73 return result
74
75# Load model
76highlighter = ArabicSemanticHighlighter("path/to/model")
77
78# Example usage
79question = "ما هي فوائد الذكاء الاصطناعي في التعليم؟"
80context = """الذكاء الاصطناعي يحدث ثورة في قطاع التعليم.
81يساعد الذكاء الاصطناعي المعلمين في تخصيص المحتوى التعليمي لكل طالب.
82الطقس اليوم مشمس ودافئ."""
83
84result = highlighter.process(
85 question=question,
86 context=context,
87 threshold=0.5,
88 return_sentence_metrics=True
89)
90
91print("Highlighted sentences:", result["highlighted_sentences"])
92# Output: Relevant sentences about AI in education (excludes weather sentence)1@misc{arabic-semantic-highlighter,
2 author = {Hesham Haroon},
3 title = {Arabic Semantic Highlighter},
4 year = {2026},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/HeshamHaroon/arabic-semantic-highlighter}}
7}