Spaces:
Sleeping
Sleeping
Update tools/polite_guard.py
Browse files- tools/polite_guard.py +15 -15
tools/polite_guard.py
CHANGED
|
@@ -23,7 +23,7 @@ class PoliteGuardTool(Tool):
|
|
| 23 |
|
| 24 |
def forward(self, input_text: Any) -> Any:
|
| 25 |
self.label, self.score = self.ask_polite_guard(input_text)
|
| 26 |
-
print(f"forward sets the following: {self.label } and {self.score}"
|
| 27 |
return self.label
|
| 28 |
|
| 29 |
def __init__(self, *args, **kwargs):
|
|
@@ -32,18 +32,18 @@ class PoliteGuardTool(Tool):
|
|
| 32 |
self.score = None
|
| 33 |
|
| 34 |
#@tool
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
Args:
|
| 39 |
-
input_text: The text to classify.
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def forward(self, input_text: Any) -> Any:
|
| 25 |
self.label, self.score = self.ask_polite_guard(input_text)
|
| 26 |
+
print(f"forward sets the following: {self.label } and {self.score}")
|
| 27 |
return self.label
|
| 28 |
|
| 29 |
def __init__(self, *args, **kwargs):
|
|
|
|
| 32 |
self.score = None
|
| 33 |
|
| 34 |
#@tool
|
| 35 |
+
def ask_polite_guard(self, input_text: str) -> tuple[str,int]:
|
| 36 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
Args:
|
| 39 |
+
input_text: The text to classify.
|
| 40 |
+
|
| 41 |
+
Returns:
|
| 42 |
+
tuple: with classification label and the score
|
| 43 |
+
"""
|
| 44 |
+
try:
|
| 45 |
+
classifier = pipeline("text-classification", "Intel/polite-guard")
|
| 46 |
+
return classifier(input_text)
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error fetching classification for text '{input_text}': {str(e)}"
|