ContentAgent / tools /polite_guard.py
yetessam's picture
Update tools/polite_guard.py
01fb156 verified
raw
history blame
1.47 kB
from typing import Any, Optional
from smolagents.tools import Tool
from transformers import pipeline
class PoliteGuardTool(Tool):
"""Wrapper around a text tool that classifies text into politeness categories.
Args:
input_text: A string of English text.
"""
name = "polite_guard"
description = "Calls an tool to classifify text by 4 labels 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)
def __init__(self, *args, **kwargs):
self.is_initialized = False
self.label = None
self.score = None
#@tool
def ask_polite_guard(self, input_text: str) -> dict:
"""
Args:
input_text: The text to classify.
Returns:
dictionary with
a. A in result['label'] which is one of the four classifications
b. A score in result['score']
"""
try:
classifier = pipeline("text-classification", "Intel/polite-guard")
return {
"label": result['label'],
"score": result['score']
}
except Exception as e:
return f"Error fetching classification for text '{input_text}': {str(e)}"