GLiNER_file / gliner_file.py
oliviercaron's picture
Update gliner_file.py
5adda08 verified
raw
history blame contribute delete
816 Bytes
from gliner import GLiNER
from typing import List
def run_ner(model, texts: List[str], labels_list: List[str], threshold=0.4):
"""
Executes named entity recognition (NER) on a list of texts.
Parameters:
- model: loaded GLiNER model.
- texts: list of texts to analyze.
- labels_list: list of NER labels to detect.
- threshold: confidence threshold for predictions.
Returns:
- ner_results: list of dictionaries containing detected entities for each text.
"""
ner_results = []
for text in texts:
try:
# Predict entities for the text
entities = model.predict_entities(text, labels_list, threshold=threshold)
ner_results.append(entities)
except Exception as e:
ner_results.append([])
return ner_results