Spaces:
Running on Zero
Running on Zero
Update app/main.py
Browse files- app/main.py +24 -10
app/main.py
CHANGED
|
@@ -2,6 +2,7 @@ from fastapi import FastAPI
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from transformers import pipeline
|
| 4 |
from glin_profanity import Filter
|
|
|
|
| 5 |
|
| 6 |
toxicity_pipe = pipeline("text-classification", model="textdetox/twitter-xlmr-toxicity-classifier")
|
| 7 |
detox_pipe = pipeline("text2text-generation", model="s-nlp/mt0-xl-detox-orpo")
|
|
@@ -18,25 +19,38 @@ class ModerationResponse(BaseModel):
|
|
| 18 |
is_flagged: bool
|
| 19 |
rewritten_text: list[str] | None
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@app.post("/moderate", response_model=ModerationResponse)
|
| 22 |
async def moderate_text(request: TextRequest):
|
| 23 |
text = request.text
|
| 24 |
|
|
|
|
| 25 |
profanity_result = profanity_filter.check_profanity(text)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
tox_result = toxicity_pipe(text)[0]
|
| 29 |
-
|
| 30 |
-
# 2. Check if the LABEL is actually toxic AND the confidence is high
|
| 31 |
-
# (The model usually outputs 'toxic' or 'neutral' / 'LABEL_1' or 'LABEL_0')
|
| 32 |
is_toxic_by_model = (tox_result['label'].lower() in ['toxic', 'label_1']) and (tox_result['score'] > 0.5)
|
| 33 |
|
| 34 |
-
# 3
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
rewritten = None
|
| 38 |
if is_flagged:
|
| 39 |
-
# Added temperature=0.8 to make the 5 sentences more diverse
|
| 40 |
outputs = detox_pipe(
|
| 41 |
text,
|
| 42 |
max_length=100,
|
|
@@ -50,9 +64,9 @@ async def moderate_text(request: TextRequest):
|
|
| 50 |
return ModerationResponse(
|
| 51 |
original_text=text,
|
| 52 |
detected_categories={
|
| 53 |
-
"profanity": profanity_result["contains_profanity"],
|
| 54 |
-
"
|
| 55 |
-
"
|
| 56 |
},
|
| 57 |
is_flagged=is_flagged,
|
| 58 |
rewritten_text=rewritten
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from transformers import pipeline
|
| 4 |
from glin_profanity import Filter
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
toxicity_pipe = pipeline("text-classification", model="textdetox/twitter-xlmr-toxicity-classifier")
|
| 8 |
detox_pipe = pipeline("text2text-generation", model="s-nlp/mt0-xl-detox-orpo")
|
|
|
|
| 19 |
is_flagged: bool
|
| 20 |
rewritten_text: list[str] | None
|
| 21 |
|
| 22 |
+
# 1. Define explicit threat keywords (You can expand this list)
|
| 23 |
+
THREAT_KEYWORDS = [
|
| 24 |
+
r"\bguns?\b", r"\bweapons?\b", r"\bbombs?\b", r"\bexplosives?\b",
|
| 25 |
+
r"\bkill\b", r"\bmurder\b", r"\bshoot\b", r"\bstab\b",
|
| 26 |
+
r"\bdie\b", r"\bdeath\b", r"\bhurt\b"
|
| 27 |
+
]
|
| 28 |
+
# Compile the regex pattern for fast matching
|
| 29 |
+
threat_pattern = re.compile("|".join(THREAT_KEYWORDS), re.IGNORECASE)
|
| 30 |
+
|
| 31 |
@app.post("/moderate", response_model=ModerationResponse)
|
| 32 |
async def moderate_text(request: TextRequest):
|
| 33 |
text = request.text
|
| 34 |
|
| 35 |
+
# Check 1: Profanity filter
|
| 36 |
profanity_result = profanity_filter.check_profanity(text)
|
| 37 |
|
| 38 |
+
# Check 2: AI Toxicity model
|
| 39 |
tox_result = toxicity_pipe(text)[0]
|
|
|
|
|
|
|
|
|
|
| 40 |
is_toxic_by_model = (tox_result['label'].lower() in ['toxic', 'label_1']) and (tox_result['score'] > 0.5)
|
| 41 |
|
| 42 |
+
# Check 3: Threat Keyword Regex
|
| 43 |
+
is_threat_by_keyword = bool(threat_pattern.search(text))
|
| 44 |
+
|
| 45 |
+
# Final Decision: Flag if ANY check is true
|
| 46 |
+
is_flagged = (
|
| 47 |
+
profanity_result["contains_profanity"] or
|
| 48 |
+
is_toxic_by_model or
|
| 49 |
+
is_threat_by_keyword
|
| 50 |
+
)
|
| 51 |
|
| 52 |
rewritten = None
|
| 53 |
if is_flagged:
|
|
|
|
| 54 |
outputs = detox_pipe(
|
| 55 |
text,
|
| 56 |
max_length=100,
|
|
|
|
| 64 |
return ModerationResponse(
|
| 65 |
original_text=text,
|
| 66 |
detected_categories={
|
| 67 |
+
"profanity": profanity_result["contains_profanity"],
|
| 68 |
+
"ai_toxicity": is_toxic_by_model,
|
| 69 |
+
"threat_keyword": is_threat_by_keyword
|
| 70 |
},
|
| 71 |
is_flagged=is_flagged,
|
| 72 |
rewritten_text=rewritten
|