Spaces:
Running
Running
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.ask_polite_guard(input_text) | |
return self.label | |
def __init__(self, *args, **kwargs): | |
self.is_initialized = False | |
self.label = None | |
self.score = None | |
#@tool | |
def ask_polite_guard(self, input_text: str) -> str: | |
""" | |
Args: | |
input_text: The text to classify. | |
Returns: | |
string with classification label | |
""" | |
try: | |
classifier = pipeline("text-classification", "Intel/polite-guard") | |
return classifier(input_text) | |
except Exception as e: | |
return f"Error fetching classification for text '{input_text}': {str(e)}" |