Spaces:
Running on Zero
Running on Zero
Update app/main.py
Browse files- app/main.py +10 -9
app/main.py
CHANGED
|
@@ -9,8 +9,15 @@ toxicity_pipe = pipeline("text-classification", model="textdetox/twitter-xlmr-to
|
|
| 9 |
|
| 10 |
# 2. UPGRADED: Use a highly capable Chat Model (Phi-3)
|
| 11 |
model_id = "microsoft/Phi-3-mini-4k-instruct"
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
profanity_filter = Filter({"languages": ["english", "spanish", "french"], "replace_with": "***"})
|
| 16 |
|
|
@@ -50,32 +57,26 @@ async def moderate_text(request: TextRequest):
|
|
| 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():
|
| 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:
|
|
|
|
| 9 |
|
| 10 |
# 2. UPGRADED: Use a highly capable Chat Model (Phi-3)
|
| 11 |
model_id = "microsoft/Phi-3-mini-4k-instruct"
|
| 12 |
+
|
| 13 |
+
# --- ADD trust_remote_code=True HERE ---
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 15 |
+
llm_pipe = pipeline(
|
| 16 |
+
"text-generation",
|
| 17 |
+
model=model_id,
|
| 18 |
+
tokenizer=tokenizer,
|
| 19 |
+
trust_remote_code=True # <--- THE FIX
|
| 20 |
+
)
|
| 21 |
|
| 22 |
profanity_filter = Filter({"languages": ["english", "spanish", "french"], "replace_with": "***"})
|
| 23 |
|
|
|
|
| 57 |
|
| 58 |
rewritten = None
|
| 59 |
if is_flagged:
|
|
|
|
| 60 |
messages = [
|
| 61 |
{"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."},
|
| 62 |
{"role": "user", "content": text}
|
| 63 |
]
|
| 64 |
|
|
|
|
| 65 |
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 66 |
outputs = llm_pipe(prompt, max_new_tokens=200, do_sample=True, temperature=0.7)
|
| 67 |
|
| 68 |
generated_text = outputs[0]['generated_text'][len(prompt):].strip()
|
| 69 |
|
|
|
|
| 70 |
lines = generated_text.split('\n')
|
| 71 |
rewritten = []
|
| 72 |
for line in lines:
|
| 73 |
line = line.strip()
|
| 74 |
+
if line and line[0].isdigit():
|
|
|
|
| 75 |
clean_line = re.sub(r'^\d+[\.\-\)]\s*', '', line).strip()
|
|
|
|
| 76 |
clean_line = clean_line.strip('"\'')
|
| 77 |
if clean_line:
|
| 78 |
rewritten.append(clean_line)
|
| 79 |
|
|
|
|
| 80 |
rewritten = list(dict.fromkeys(rewritten))[:5]
|
| 81 |
|
| 82 |
if not rewritten:
|