Spaces:
Running on Zero
Running on Zero
Update app/main.py
Browse files- app/main.py +31 -25
app/main.py
CHANGED
|
@@ -1,14 +1,16 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
from transformers import pipeline
|
| 4 |
from glin_profanity import Filter
|
| 5 |
import re
|
| 6 |
|
| 7 |
-
# 1.
|
| 8 |
toxicity_pipe = pipeline("text-classification", model="textdetox/twitter-xlmr-toxicity-classifier")
|
| 9 |
|
| 10 |
-
# 2.
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
profanity_filter = Filter({"languages": ["english", "spanish", "french"], "replace_with": "***"})
|
| 14 |
|
|
@@ -48,32 +50,36 @@ async def moderate_text(request: TextRequest):
|
|
| 48 |
|
| 49 |
rewritten = None
|
| 50 |
if is_flagged:
|
| 51 |
-
# 3.
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
num_return_sequences=5,
|
| 58 |
-
do_sample=True,
|
| 59 |
-
temperature=0.9,
|
| 60 |
-
top_p=0.9
|
| 61 |
-
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
suggestions = set()
|
| 65 |
-
for out in outputs:
|
| 66 |
-
clean_text = out['generated_text'].strip()
|
| 67 |
-
# Only add if it actually changed the text
|
| 68 |
-
if clean_text.lower() != text.lower() and clean_text:
|
| 69 |
-
suggestions.add(clean_text)
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
#
|
|
|
|
|
|
|
| 75 |
if not rewritten:
|
| 76 |
-
rewritten = ["[AI could not generate a
|
| 77 |
|
| 78 |
return ModerationResponse(
|
| 79 |
original_text=text,
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from transformers import pipeline, AutoTokenizer
|
| 4 |
from glin_profanity import Filter
|
| 5 |
import re
|
| 6 |
|
| 7 |
+
# 1. Toxicity classifier
|
| 8 |
toxicity_pipe = pipeline("text-classification", model="textdetox/twitter-xlmr-toxicity-classifier")
|
| 9 |
|
| 10 |
+
# 2. UPGRADED: Use a highly capable Chat Model (Phi-3)
|
| 11 |
+
model_id = "microsoft/Phi-3-mini-4k-instruct"
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 13 |
+
llm_pipe = pipeline("text-generation", model=model_id, tokenizer=tokenizer)
|
| 14 |
|
| 15 |
profanity_filter = Filter({"languages": ["english", "spanish", "french"], "replace_with": "***"})
|
| 16 |
|
|
|
|
| 50 |
|
| 51 |
rewritten = None
|
| 52 |
if is_flagged:
|
| 53 |
+
# 3. Craft a strict prompt asking for 5 different options
|
| 54 |
+
messages = [
|
| 55 |
+
{"role": "system", "content": "You are a helpful, polite assistant. Rewrite the user's toxic message into exactly 5 different safe, polite, and non-threatening sentences. Number them 1 to 5. Do not include any other text in your response."},
|
| 56 |
+
{"role": "user", "content": text}
|
| 57 |
+
]
|
| 58 |
|
| 59 |
+
# Generate response
|
| 60 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 61 |
+
outputs = llm_pipe(prompt, max_new_tokens=200, do_sample=True, temperature=0.7)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
generated_text = outputs[0]['generated_text'][len(prompt):].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# 4. Parse the output into a clean list
|
| 66 |
+
lines = generated_text.split('\n')
|
| 67 |
+
rewritten = []
|
| 68 |
+
for line in lines:
|
| 69 |
+
line = line.strip()
|
| 70 |
+
if line and line[0].isdigit(): # Only keep lines that start with a number
|
| 71 |
+
# Remove the numbering (e.g., "1. " or "1- ")
|
| 72 |
+
clean_line = re.sub(r'^\d+[\.\-\)]\s*', '', line).strip()
|
| 73 |
+
# Remove quotes if the AI added them
|
| 74 |
+
clean_line = clean_line.strip('"\'')
|
| 75 |
+
if clean_line:
|
| 76 |
+
rewritten.append(clean_line)
|
| 77 |
|
| 78 |
+
# Keep only the first 5, and strip any duplicates
|
| 79 |
+
rewritten = list(dict.fromkeys(rewritten))[:5]
|
| 80 |
+
|
| 81 |
if not rewritten:
|
| 82 |
+
rewritten = ["[AI could not generate a rewrite, please revise manually]"]
|
| 83 |
|
| 84 |
return ModerationResponse(
|
| 85 |
original_text=text,
|