ContentAgent / tools /polite_guard.py
yetessam's picture
Update tools/polite_guard.py
c704fac verified
raw
history blame
1.83 kB
from typing import Any, Optional
from smolagents.tools import Tool
from transformers import pipeline
class PoliteGuardTool(Tool):
"""
Takes the input text from users and then evaluates it against Polite Guard to return specific information
about whether the content is polite.
Args:
input_text: Text that the user inputs into the agent and should then be evaluated.
Returns:
A classification label about whether the content is polite, somewhat polite, neutral or impolite.
"""
name = "polite_guard"
description = "Uses Polite guard to classify input text from polite to impolite"
inputs = {'input_text': {'type': 'any', 'description': 'Enter text for assessing whether it is respectful'}}
output_type = "any"
def forward(self, input_text: Any) -> Any:
self.label, self.score = self.ask_polite_guard(input_text)
print(f"forward sets the following: {self.label } and {self.score}")
return self.label
def __init__(self, *args, **kwargs):
self.is_initialized = False
self.label = None
self.score = None
def ask_polite_guard(self, input_text: str) -> tuple[str, float]:
"""
Args:
input_text: The text to classify.
Returns:
tuple: with classification label and the score
"""
try:
classifier = pipeline("text-classification", "Intel/polite-guard")
result = classifier(input_text)
print(f"return {str(result)}")
output = result[0]
return output['label'], output['score']
except Exception as e:
return f"Error fetching classification for text '{input_text}': {str(e)}"