File size: 902 Bytes
f866660
 
c5922b9
 
 
 
 
 
 
05fc524
c5922b9
 
 
 
 
 
 
 
 
 
 
 
f866660
c5922b9
f866660
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
# Updated NamedEntityRecognitionTool in ner_tool.py

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.get("entity", "UNKNOWN") for entity in entities]

        return {"entities": entity_labels}  # Return a dictionary with the specified output component