Spaces:
Running on Zero
Running on Zero
Update app/main.py
Browse files- app/main.py +21 -24
app/main.py
CHANGED
|
@@ -3,53 +3,50 @@ from pydantic import BaseModel
|
|
| 3 |
from transformers import pipeline
|
| 4 |
from glin_profanity import Filter
|
| 5 |
|
| 6 |
-
|
| 7 |
-
toxicity_pipe = pipeline(
|
| 8 |
-
"text-classification", model="textdetox/twitter-xlmr-toxicity-classifier"
|
| 9 |
-
)
|
| 10 |
detox_pipe = pipeline("text2text-generation", model="s-nlp/mt0-xl-detox-orpo")
|
| 11 |
-
profanity_filter = Filter(
|
| 12 |
-
{"languages": ["english", "spanish", "french"], "replace_with": "***"}
|
| 13 |
-
)
|
| 14 |
|
| 15 |
app = FastAPI(title="Multilingual Moderation API")
|
| 16 |
|
| 17 |
-
|
| 18 |
class TextRequest(BaseModel):
|
| 19 |
text: str
|
| 20 |
|
| 21 |
-
|
| 22 |
class ModerationResponse(BaseModel):
|
| 23 |
original_text: str
|
| 24 |
detected_categories: dict
|
| 25 |
is_flagged: bool
|
| 26 |
-
rewritten_text: str | None
|
| 27 |
-
|
| 28 |
|
| 29 |
@app.post("/moderate", response_model=ModerationResponse)
|
| 30 |
async def moderate_text(request: TextRequest):
|
| 31 |
text = request.text
|
|
|
|
| 32 |
profanity_result = profanity_filter.check_profanity(text)
|
| 33 |
toxicity_scores = toxicity_pipe(text)[0]
|
| 34 |
-
|
| 35 |
-
is_flagged =
|
| 36 |
-
|
| 37 |
-
)
|
| 38 |
rewritten = None
|
| 39 |
if is_flagged:
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
return ModerationResponse(
|
| 43 |
original_text=text,
|
| 44 |
-
detected_categories={
|
| 45 |
-
"profanity": profanity_result["contains_profanity"],
|
| 46 |
-
"toxicity_model": toxicity_scores,
|
| 47 |
-
},
|
| 48 |
is_flagged=is_flagged,
|
| 49 |
-
rewritten_text=rewritten
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
| 53 |
@app.get("/health")
|
| 54 |
async def health_check():
|
| 55 |
-
return {"status": "ok"}
|
|
|
|
| 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")
|
| 8 |
+
profanity_filter = Filter({"languages": ["english", "spanish", "french"], "replace_with": "***"})
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI(title="Multilingual Moderation API")
|
| 11 |
|
|
|
|
| 12 |
class TextRequest(BaseModel):
|
| 13 |
text: str
|
| 14 |
|
|
|
|
| 15 |
class ModerationResponse(BaseModel):
|
| 16 |
original_text: str
|
| 17 |
detected_categories: dict
|
| 18 |
is_flagged: bool
|
| 19 |
+
rewritten_text: list[str] | None # <--- CHANGED TO LIST
|
|
|
|
| 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 |
toxicity_scores = toxicity_pipe(text)[0]
|
| 27 |
+
|
| 28 |
+
is_flagged = profanity_result["contains_profanity"] or toxicity_scores['score'] > 0.5
|
| 29 |
+
|
|
|
|
| 30 |
rewritten = None
|
| 31 |
if is_flagged:
|
| 32 |
+
# Generate 5 different suggestions using sampling
|
| 33 |
+
outputs = detox_pipe(
|
| 34 |
+
text,
|
| 35 |
+
max_length=100,
|
| 36 |
+
num_return_sequences=5,
|
| 37 |
+
do_sample=True,
|
| 38 |
+
top_p=0.9
|
| 39 |
+
)
|
| 40 |
+
# Extract just the text from the output dictionaries
|
| 41 |
+
rewritten = [out['generated_text'] for out in outputs]
|
| 42 |
+
|
| 43 |
return ModerationResponse(
|
| 44 |
original_text=text,
|
| 45 |
+
detected_categories={"profanity": profanity_result["contains_profanity"], "toxicity_model": toxicity_scores},
|
|
|
|
|
|
|
|
|
|
| 46 |
is_flagged=is_flagged,
|
| 47 |
+
rewritten_text=rewritten
|
| 48 |
)
|
| 49 |
|
|
|
|
| 50 |
@app.get("/health")
|
| 51 |
async def health_check():
|
| 52 |
+
return {"status": "ok"}
|