added image ai-moderation
This commit is contained in:
@@ -1,26 +1,76 @@
|
||||
from app.exceptions.invalid_text_exception import InvalidTextException
|
||||
from app.moderation.profanity.detector import ProfanityDetector
|
||||
from app.ml.text_classifier import TextClassifier
|
||||
from app.ml.prediction_result import PredictionResult
|
||||
from app.config.settings import settings
|
||||
|
||||
|
||||
class TextModerationService:
|
||||
|
||||
TOXIC_THRESHOLD = 0.80
|
||||
def __init__(
|
||||
self,
|
||||
classifier: TextClassifier,
|
||||
profanity_detector: ProfanityDetector
|
||||
):
|
||||
|
||||
self.classifier = classifier
|
||||
self.profanity_detector = profanity_detector
|
||||
|
||||
|
||||
def moderate(
|
||||
self,
|
||||
text: str
|
||||
) -> PredictionResult:
|
||||
|
||||
|
||||
if text is None or not text.strip():
|
||||
|
||||
raise InvalidTextException(
|
||||
"Text is empty"
|
||||
)
|
||||
|
||||
|
||||
detected_words = (
|
||||
self.profanity_detector.detect(text)
|
||||
)
|
||||
|
||||
|
||||
if detected_words:
|
||||
|
||||
|
||||
return PredictionResult(
|
||||
|
||||
label="PROFANITY",
|
||||
|
||||
score=1.0,
|
||||
|
||||
approved=False,
|
||||
|
||||
raw_scores=[],
|
||||
|
||||
reason="PROFANITY",
|
||||
|
||||
detected_words=detected_words
|
||||
|
||||
)
|
||||
|
||||
def __init__(self):
|
||||
self.classifier = TextClassifier()
|
||||
|
||||
def moderate(self, text: str) -> PredictionResult:
|
||||
|
||||
prediction = self.classifier.predict(text)
|
||||
|
||||
|
||||
|
||||
prediction.approved = (
|
||||
prediction.score < self.TOXIC_THRESHOLD
|
||||
prediction.score <
|
||||
settings.TEXT_TOXIC_THRESHOLD
|
||||
)
|
||||
|
||||
|
||||
prediction.reason = (
|
||||
"OK"
|
||||
if prediction.approved
|
||||
else "TOXIC"
|
||||
)
|
||||
|
||||
|
||||
return prediction
|
||||
Reference in New Issue
Block a user