Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ LOCAL_DIR = os.path.join(os.getcwd(), "local_model")
|
|
| 11 |
|
| 12 |
# CPU optimizasyonları
|
| 13 |
os.environ.setdefault("HF_HUB_ENABLE_HF_TRANSFER", "1")
|
| 14 |
-
os.environ.setdefault("OMP_NUM_THREADS", str(os.cpu_count() or
|
| 15 |
os.environ.setdefault("MKL_NUM_THREADS", os.environ["OMP_NUM_THREADS"])
|
| 16 |
os.environ.setdefault("OMP_PROC_BIND", "TRUE")
|
| 17 |
|
|
@@ -48,42 +48,46 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 48 |
)
|
| 49 |
model.eval()
|
| 50 |
|
| 51 |
-
# Çok
|
| 52 |
MODERATION_SYSTEM_PROMPT = (
|
| 53 |
"You are a multilingual content moderation classifier. "
|
| 54 |
-
"You
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
-
"
|
| 59 |
)
|
| 60 |
|
| 61 |
-
def build_prompt(message, max_ctx_tokens=
|
| 62 |
-
|
| 63 |
{"role": "system", "content": MODERATION_SYSTEM_PROMPT},
|
| 64 |
{"role": "user", "content": message}
|
| 65 |
]
|
| 66 |
-
|
| 67 |
-
{{ m['role'] }}: {{ m['content'] }}
|
| 68 |
-
{% endfor %}
|
| 69 |
-
Assistant:"""
|
| 70 |
text = tokenizer.apply_chat_template(
|
| 71 |
-
|
| 72 |
-
chat_template=chat_template,
|
| 73 |
tokenize=False,
|
| 74 |
add_generation_prompt=True
|
| 75 |
)
|
| 76 |
# Token sınırını aşarsa kısalt
|
| 77 |
-
while len(tokenizer(text, add_special_tokens=False).input_ids) > max_ctx_tokens and len(
|
| 78 |
-
|
| 79 |
text = tokenizer.apply_chat_template(
|
| 80 |
-
|
| 81 |
-
chat_template=chat_template,
|
| 82 |
tokenize=False,
|
| 83 |
add_generation_prompt=True
|
| 84 |
)
|
| 85 |
return text
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
def respond_stream(message, history, max_tokens, temperature, top_p):
|
| 88 |
text = build_prompt(message)
|
| 89 |
inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
|
@@ -118,25 +122,26 @@ def respond_stream(message, history, max_tokens, temperature, top_p):
|
|
| 118 |
start_time = time.time()
|
| 119 |
partial_text += chunk
|
| 120 |
token_count += 1
|
| 121 |
-
yield partial_text.strip()
|
| 122 |
finally:
|
| 123 |
thread.join()
|
| 124 |
|
|
|
|
|
|
|
| 125 |
end_time = time.time() if start_time else time.time()
|
| 126 |
duration = max(1e-6, end_time - start_time)
|
| 127 |
tps = token_count / duration if duration > 0 else 0.0
|
| 128 |
-
yield
|
| 129 |
|
| 130 |
demo = gr.ChatInterface(
|
| 131 |
respond_stream,
|
| 132 |
chatbot=False,
|
| 133 |
additional_inputs=[
|
| 134 |
-
gr.Slider(minimum=1, maximum=
|
| 135 |
gr.Slider(minimum=0.0, maximum=1.0, value=0.0, step=0.1, label="Temperature"),
|
| 136 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
| 137 |
],
|
| 138 |
-
title="Multilingual Moderation Classifier",
|
| 139 |
-
description="Enter any text in any language. The model will output only '
|
| 140 |
)
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
|
|
|
| 11 |
|
| 12 |
# CPU optimizasyonları
|
| 13 |
os.environ.setdefault("HF_HUB_ENABLE_HF_TRANSFER", "1")
|
| 14 |
+
os.environ.setdefault("OMP_NUM_THREADS", str(os.cpu_count() or 2))
|
| 15 |
os.environ.setdefault("MKL_NUM_THREADS", os.environ["OMP_NUM_THREADS"])
|
| 16 |
os.environ.setdefault("OMP_PROC_BIND", "TRUE")
|
| 17 |
|
|
|
|
| 48 |
)
|
| 49 |
model.eval()
|
| 50 |
|
| 51 |
+
# Çok katı moderasyon system prompt (yalnızca "s" veya "u")
|
| 52 |
MODERATION_SYSTEM_PROMPT = (
|
| 53 |
"You are a multilingual content moderation classifier. "
|
| 54 |
+
"You MUST respond with exactly one lowercase letter: 's' for safe, 'u' for unsafe. "
|
| 55 |
+
"No explanations, no punctuation, no extra words. "
|
| 56 |
+
"If the message contains hate speech, harassment, sexual content involving minors, "
|
| 57 |
+
"extreme violence, self-harm encouragement, or other unsafe material, respond 'u'. "
|
| 58 |
+
"Otherwise respond 's'."
|
| 59 |
)
|
| 60 |
|
| 61 |
+
def build_prompt(message, max_ctx_tokens=128):
|
| 62 |
+
messages = [
|
| 63 |
{"role": "system", "content": MODERATION_SYSTEM_PROMPT},
|
| 64 |
{"role": "user", "content": message}
|
| 65 |
]
|
| 66 |
+
# Doğru chat template kullanımı
|
|
|
|
|
|
|
|
|
|
| 67 |
text = tokenizer.apply_chat_template(
|
| 68 |
+
messages,
|
|
|
|
| 69 |
tokenize=False,
|
| 70 |
add_generation_prompt=True
|
| 71 |
)
|
| 72 |
# Token sınırını aşarsa kısalt
|
| 73 |
+
while len(tokenizer(text, add_special_tokens=False).input_ids) > max_ctx_tokens and len(messages) > 2:
|
| 74 |
+
messages.pop(1)
|
| 75 |
text = tokenizer.apply_chat_template(
|
| 76 |
+
messages,
|
|
|
|
| 77 |
tokenize=False,
|
| 78 |
add_generation_prompt=True
|
| 79 |
)
|
| 80 |
return text
|
| 81 |
|
| 82 |
+
def enforce_s_u(text: str) -> str:
|
| 83 |
+
"""Model çıktısını kesin olarak 's' veya 'u' ile sınırla."""
|
| 84 |
+
text_lower = text.strip().lower()
|
| 85 |
+
if "u" in text_lower and not "s" in text_lower:
|
| 86 |
+
return "u"
|
| 87 |
+
if "unsafe" in text_lower:
|
| 88 |
+
return "u"
|
| 89 |
+
return "s"
|
| 90 |
+
|
| 91 |
def respond_stream(message, history, max_tokens, temperature, top_p):
|
| 92 |
text = build_prompt(message)
|
| 93 |
inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
|
|
|
| 122 |
start_time = time.time()
|
| 123 |
partial_text += chunk
|
| 124 |
token_count += 1
|
|
|
|
| 125 |
finally:
|
| 126 |
thread.join()
|
| 127 |
|
| 128 |
+
# Çıktıyı kesin olarak s/u'ya indir
|
| 129 |
+
final_label = enforce_s_u(partial_text)
|
| 130 |
end_time = time.time() if start_time else time.time()
|
| 131 |
duration = max(1e-6, end_time - start_time)
|
| 132 |
tps = token_count / duration if duration > 0 else 0.0
|
| 133 |
+
yield f"{final_label}\n\n⚡ Speed: {tps:.2f} token/s"
|
| 134 |
|
| 135 |
demo = gr.ChatInterface(
|
| 136 |
respond_stream,
|
| 137 |
chatbot=False,
|
| 138 |
additional_inputs=[
|
| 139 |
+
gr.Slider(minimum=1, maximum=4, value=1, step=1, label="Max new tokens"),
|
| 140 |
gr.Slider(minimum=0.0, maximum=1.0, value=0.0, step=0.1, label="Temperature"),
|
| 141 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
| 142 |
],
|
| 143 |
+
title="Strict Multilingual Moderation Classifier (s/u)",
|
| 144 |
+
description="Enter any text in any language. The model will output only 's' (safe) or 'u' (unsafe)."
|
| 145 |
)
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|