File size: 760 Bytes
c5922b9
 
 
 
 
 
 
05fc524
c5922b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from transformers import pipeline
from transformers import Tool

class NamedEntityRecognitionTool(Tool):
    name = "ner_tool"
    description = "Identifies and labels entities such as persons, organizations, and locations in a given text."
    inputs = ["text"]
    outputs = ["text"]

    def __call__(self, text: str):
        # Initialize the named entity recognition pipeline
        ner_analyzer = pipeline("ner")

        # Perform named entity recognition on the input text
        entities = ner_analyzer(text)

        # Print the identified entities
        print(f"Identified Entities: {entities}")

        # Extract entity labels and return as a list
        entity_labels = [entity["label"] for entity in entities]

        return entity_labels