Views
No views yet
pip install torch transformers1import torch
2import torch.nn as nn
3import torch.nn.functional as F
4from transformers import AutoModel, AutoTokenizer, AutoConfig
5
6class Qwen3ForSequenceClassification(nn.Module):
7 """Qwen3-Embedding with classification head using last-token pooling."""
8
9 def __init__(self, model_name="Qwen/Qwen3-Embedding-0.6B", num_labels=2):
10 super().__init__()
11 self.encoder = AutoModel.from_pretrained(
12 model_name,
13 torch_dtype=torch.bfloat16,
14 trust_remote_code=True,
15 )
16 hidden_size = AutoConfig.from_pretrained(model_name).hidden_size
17 self.classifier = nn.Linear(hidden_size, num_labels)
18
19 def forward(self, input_ids, attention_mask):
20 outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
21 # Last-token pooling (not CLS token)
22 pooled = outputs.last_hidden_state[:, -1]
23 return self.classifier(pooled)
24
25
26# Load model and weights
27device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
28model = Qwen3ForSequenceClassification()
29model.load_state_dict(torch.load("weights/model.pt", map_location=device, weights_only=True))
30model.to(device).eval()
31
32# Load tokenizer
33tokenizer = AutoTokenizer.from_pretrained("weights/", padding_side="left", trust_remote_code=True)
34if tokenizer.pad_token is None:
35 tokenizer.pad_token = tokenizer.eos_token
36
37# Inference
38text = "Your text to classify here."
39inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=4096)
40inputs = {k: v.to(device) for k, v in inputs.items()}
41
42with torch.no_grad():
43 logits = model(inputs["input_ids"], inputs["attention_mask"])
44 probs = F.softmax(logits.float(), dim=-1)[0]
45
46label = "ai" if probs[1] > probs[0] else "human"
47confidence = probs[1].item() if label == "ai" else probs[0].item()
48
49print(f"Label: {label}, Confidence: {confidence:.2%}")
50# Example output: Label: human, Confidence: 94.32%| Component | Details |
|---|---|
| Base Model | Qwen/Qwen3-Embedding-0.6B |
| Hidden Size | 1024 |
| Parameters | ~600M |
| Pooling | Last-token (not CLS) |
| Classification Head | Linear (1024 → 2) |
| Precision | bfloat16 (CUDA) / float32 (CPU) |
| Metric | Score |
|---|---|
| Accuracy | 98.86% |
1@software{,
2 title={Argus: AI-Generated Text Detection Classifier},
3 author={Xi Nai Lai},
4 year={2026},
5 url={https://huggingface.co/johnbean393/argus/}
6}