File size: 6,234 Bytes
349cf97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b4c26b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
---
language:
  - ar
license: other
license_name: non-commercial
license_link: LICENSE
library_name: transformers
tags:
  - semantic-highlighting
  - arabic
  - sentence-relevance
  - rag
  - reranker
  - text-classification
datasets:
  - HeshamHaroon/arabic-semantic-relevance
base_model: BAAI/bge-reranker-base
pipeline_tag: text-classification
metrics:
  - accuracy
  - f1
  - precision
  - recall
model-index:
  - name: arabic-semantic-highlighter
    results:
      - task:
          type: text-classification
          name: Sentence Relevance Classification
        dataset:
          name: Arabic Semantic Relevance
          type: HeshamHaroon/arabic-semantic-relevance
        metrics:
          - type: accuracy
            value: 0.9313
          - type: f1
            value: 0.9458
          - type: precision
            value: 0.9485
          - type: recall
            value: 0.9430
---

# Arabic Semantic Highlighter

A sentence-level semantic highlighting model for Arabic text, designed for RAG (Retrieval-Augmented Generation) systems.

## Model Description

This model identifies and highlights sentences in Arabic text that are relevant to a given query. It was fine-tuned on the [HeshamHaroon/arabic-semantic-relevance](https://huggingface.co/datasets/HeshamHaroon/arabic-semantic-relevance) dataset using span annotations.

### Model Details

- **Base Model:** BAAI/bge-reranker-base
- **Task:** Sentence-level semantic relevance classification
- **Language:** Arabic (العربية)
- **Training Data:** ~66,000 query-sentence pairs extracted from span annotations

### Performance Metrics

| Metric | Score |
|--------|-------|
| Accuracy | 93.13% |
| F1 Score | 94.58% |
| Precision | 94.85% |
| Recall | 94.30% |
| AUC-ROC | 98.24% |

## Usage

```python
import torch
import numpy as np
import re
from transformers import AutoModelForSequenceClassification, AutoTokenizer

class ArabicSemanticHighlighter:
    def __init__(self, model_path):
        self.model = AutoModelForSequenceClassification.from_pretrained(
            model_path,
            num_labels=1,
        )
        self.tokenizer = AutoTokenizer.from_pretrained(model_path)
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        self.model.to(self.device)
        self.model.eval()

    def _split_sentences(self, text, language="ar"):
        if language == "ar":
            sentences = re.split(r'[.؟!。\n]', text)
        else:
            sentences = re.split(r'[.?!\n]', text)
        return [s.strip() for s in sentences if s.strip() and len(s.strip()) > 5]

    def _score_sentence(self, question, sentence):
        inputs = self.tokenizer(
            question, sentence,
            truncation=True,
            max_length=256,
            padding='max_length',
            return_tensors='pt'
        ).to(self.device)

        with torch.no_grad():
            logit = self.model(**inputs).logits.squeeze().item()
            return 1 / (1 + np.exp(-logit))

    def process(self, question, context, threshold=0.5, language="auto", return_sentence_metrics=False):
        """
        Highlight relevant sentences in context based on the question.

        Args:
            question: Query string
            context: Text to search for relevant sentences
            threshold: Minimum probability for relevance (default: 0.5)
            language: "ar", "en", or "auto"
            return_sentence_metrics: Include probability scores

        Returns:
            dict with highlighted_sentences, all_sentences, and optionally sentence_probabilities
        """
        if language == "auto":
            arabic_chars = len(re.findall(r'[\u0600-\u06FF]', context))
            language = "ar" if arabic_chars > len(context) * 0.3 else "en"

        sentences = self._split_sentences(context, language)
        probabilities = []
        highlighted = []

        for sentence in sentences:
            prob = self._score_sentence(question, sentence)
            probabilities.append(prob)
            if prob >= threshold:
                highlighted.append(sentence)

        result = {
            "highlighted_sentences": highlighted,
            "all_sentences": sentences,
        }

        if return_sentence_metrics:
            result["sentence_probabilities"] = probabilities

        return result

# Load model
highlighter = ArabicSemanticHighlighter("path/to/model")

# Example usage
question = "ما هي فوائد الذكاء الاصطناعي في التعليم؟"
context = """الذكاء الاصطناعي يحدث ثورة في قطاع التعليم.
يساعد الذكاء الاصطناعي المعلمين في تخصيص المحتوى التعليمي لكل طالب.
الطقس اليوم مشمس ودافئ."""

result = highlighter.process(
    question=question,
    context=context,
    threshold=0.5,
    return_sentence_metrics=True
)

print("Highlighted sentences:", result["highlighted_sentences"])
# Output: Relevant sentences about AI in education (excludes weather sentence)
```

## Training Details

- **Epochs:** 3
- **Batch Size:** 8
- **Learning Rate:** 2e-5
- **Max Sequence Length:** 256
- **Gradient Accumulation Steps:** 4
- **Optimizer:** AdamW with weight decay 0.01
- **Training Time:** ~73 minutes on NVIDIA RTX 5060

## Use Cases

- **RAG Systems:** Highlight relevant passages for LLM context
- **Search Results:** Show users which parts of documents match their query
- **Document QA:** Identify answer-containing sentences
- **Content Filtering:** Extract relevant information from long documents

## Limitations

- Optimized for Arabic text; may work on other languages but not tested
- Best performance on sentences 10-200 characters in length
- Requires GPU for efficient inference on large documents

## Citation

If you use this model, please cite:

```bibtex
@misc{arabic-semantic-highlighter,
  author = {Hesham Haroon},
  title = {Arabic Semantic Highlighter},
  year = {2026},
  publisher = {HuggingFace},
  howpublished = {\url{https://huggingface.co/HeshamHaroon/arabic-semantic-highlighter}}
}
```

## License

This model is released under a **Non-Commercial License**. See [LICENSE](LICENSE) for details.