Text Classification
Transformers
Safetensors
English
roberta
toxicity
llada
distillation
custom_code
text-embeddings-inference

Run this model with HF Jobs

Run kl1/roberta_toxicity_classifier_LLaDA on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs to fine-tune https://huggingface.co/kl1/roberta_toxicity_classifier_LLaDA. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/kl1/roberta_toxicity_classifier_LLaDA on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
kl1's picture
Upload LLaDA-tokenized toxicity classifier
f850fbb verified
Raw History Blame Contribute Delete
1.52 kB
from __future__ import annotations
import argparse
from pathlib import Path
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"model_path",
nargs="?",
default=Path(__file__).resolve().parent,
help="Local path or Hugging Face model id.",
)
args = parser.parse_args()
tokenizer = AutoTokenizer.from_pretrained(
args.model_path,
trust_remote_code=True,
use_fast=True,
)
model = AutoModelForSequenceClassification.from_pretrained(
args.model_path,
trust_remote_code=True,
).eval()
texts = [
"I hope you have a wonderful day.",
"You are disgusting and should disappear.",
]
inputs = tokenizer(
texts,
padding=True,
truncation=True,
max_length=512,
return_tensors="pt",
)
cls_token_id = getattr(model.config, "student_cls_token_id", tokenizer.cls_token_id)
if cls_token_id is not None and int(inputs.input_ids[0, 0]) != int(cls_token_id):
raise RuntimeError("Tokenizer did not prepend the expected CLS token.")
with torch.inference_mode():
probs = torch.softmax(model(**inputs).logits, dim=-1)
toxic_id = int(model.config.label2id.get("toxic", 1))
for text, score in zip(texts, probs[:, toxic_id].tolist()):
print(f"{score:.6f}\t{text}")
if __name__ == "__main__":
main()