File size: 1,839 Bytes
5ac2d56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff148a8
 
 
 
 
 
 
ae77d9c
5ac2d56
 
95b8d91
 
 
 
5ac2d56
467e6c6
5ac2d56
 
467e6c6
5ac2d56
 
 
 
467e6c6
5ac2d56
95b8d91
ff148a8
5ac2d56
467e6c6
 
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
"""
Arabic Mental Health Named Entity Recognition Module
Model: GLiNER (fine-tuned for Arabic mental health by AhmadDarif)

This module loads the GLiNER model and exposes a function to extract named entities
related to mental health from Arabic input text.

Entity labels include:
- MEDICATION: ุฃุณู…ุงุก ุงู„ุฃุฏูˆูŠุฉ
- DOSAGE: ุงู„ุฌุฑุนุงุช
- DURATION: ู…ุฏุฉ ุงู„ุงุณุชุฎุฏุงู…

Dependencies:
- gliner
- torch
"""

from gliner import GLiNER

# Load the model
print("๐Ÿš€ Loading GLiNER model for Arabic mental health NER...")
try:
    model = GLiNER.from_pretrained("AhmadDarif/Arabic-Mental-NER")
    print("โœ… Loaded fine-tuned model: AhmadDarif/Arabic-Mental-NER")
except Exception as e:
    print(f"โš ๏ธ Could not load fine-tuned model. Falling back. Error: {str(e)}")
    model = GLiNER.from_pretrained("urchade/gliner_multi-v2.1")
    print("โœ… Loaded fallback model: urchade/gliner_multi-v2.1")

# Entity labels to extract
LABELS = ["MEDICATION", "DOSAGE", "DURATION"]

# Mapping from English to Arabic labels
label_map = {
    "MEDICATION": "ุงุณู… ุงู„ุฏูˆุงุก",
    "DOSAGE": "ุงู„ุฌุฑุนุฉ",
    "DURATION": "ู…ุฏุฉ ุงู„ุงุณุชุฎุฏุงู…"
}

def extract_entities(text: str):
    """
    Extract named entities from Arabic text using GLiNER.

    Args:
        text (str): Input Arabic text.

    Returns:
        list[list[str]]: Table of extracted entities [text, arabic_label] or empty list.
    """
    if not text.strip():
        return []

    try:
        entities = model.predict_entities(text, LABELS)
        if not entities:
            return []

        # Return results with Arabic labels
        return [[ent["text"], label_map.get(ent["label"], ent["label"])] for ent in entities]

    except Exception:
        return []  # โœ… ู„ุง ู†ุฑุฌุน ุฑุณุงู„ุฉ ู†ุตูŠุฉ ุจู„ ู†ุฑุฌุน ู‚ุงุฆู…ุฉ ูุงุถูŠุฉ ูู‚ุท