Spaces:
Running
Running
Create polite_guard.py
Browse files- tools/polite_guard.py +35 -0
tools/polite_guard.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
|
3 |
+
# Define the PoliteGuard tool
|
4 |
+
# Classify text into politeness categories
|
5 |
+
class PoliteGuardTool(Tool):
|
6 |
+
name = "polite_guard"
|
7 |
+
description = "Calls an tool to classifify text by 4 labels from polite to impolite"
|
8 |
+
inputs = {'input_text': {'type': 'any', 'description': 'Enter text for assessing whether it is respectful'}}
|
9 |
+
output_type = "any"
|
10 |
+
|
11 |
+
def forward(self, input_text: Any) -> Any:
|
12 |
+
self.label, self.sccore = self.ask_polite_guard(input_text)
|
13 |
+
|
14 |
+
|
15 |
+
def __init__(self, *args, **kwargs):
|
16 |
+
self.is_initialized = False
|
17 |
+
self.label = None
|
18 |
+
self.score = None
|
19 |
+
|
20 |
+
#@tool
|
21 |
+
def ask_polite_guard(self, input_text: str) -> dict:
|
22 |
+
"""
|
23 |
+
|
24 |
+
Args:
|
25 |
+
input_text: The text to classify.
|
26 |
+
"""
|
27 |
+
try:
|
28 |
+
classifier = pipeline("text-classification", "Intel/polite-guard")
|
29 |
+
return {
|
30 |
+
"label": result['label'],
|
31 |
+
"score": result['score']
|
32 |
+
}
|
33 |
+
|
34 |
+
except Exception as e:
|
35 |
+
return f"Error fetching classification for text '{input_text}': {str(e)}"
|