Spaces:
Running
Running
File size: 1,837 Bytes
5ac2d56 |
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 |
"""
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"]
def extract_entities(text: str) -> str:
"""
Extract named entities from Arabic text using GLiNER.
Args:
text (str): Input Arabic text.
Returns:
str: Formatted entity extraction result or error message.
"""
if not text.strip():
return "يرجى إدخال نص للتحليل / Please enter text to analyze"
try:
entities = model.predict_entities(text, LABELS)
if not entities:
return "لم يتم العثور على أي كيانات / No entities found"
result = "الكيانات المستخرجة:\n\n"
for ent in entities:
result += f"{ent['text']} => {ent['label']} (confidence: {ent.get('score', 0):.3f})\n"
return result
except Exception as e:
return f"خطأ في التحليل / Analysis Error: {str(e)}"
|