Source code for swarmauri.community.tools.concrete.SentimentAnalysisTool

from transformers import pipeline
from transformers import logging as hf_logging

from swarmauri.standard.tools.base.ToolBase import ToolBase
from swarmauri.standard.tools.concrete.Parameter import Parameter

hf_logging.set_verbosity_error()

[docs] class SentimentAnalysisTool(ToolBase): def __init__(self): super().__init__("SentimentAnalysisTool", "Analyzes the sentiment of the given text.", parameters=[ Parameter("text", "string", "The text for sentiment analysis", True) ]) def __call__(self, text: str) -> str: try: self.analyzer = pipeline("sentiment-analysis") result = self.analyzer(text) return result[0]['label'] except: raise finally: del self.analyzer