Spaces:
Running
Running
File size: 1,471 Bytes
8ced4a7 97af386 01fb156 97af386 01fb156 97af386 8ced4a7 97af386 01fb156 97af386 |
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
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)}" |