import torchfrom peft import PeftModelfrom transformers import AutoModelForCausalLM, AutoTokenizer base_id = "Qwen/Qwen2.5-1.5B-Instruct"adapter_id = "Tuan050901/vulgar-en-qlora" tokenizer = AutoTokenizer.from_pretrained(adapter_id, trust_remote_code=True)base = AutoModelForCausalLM.from_pretrained( base_id, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True,)model = PeftModel.from_pretrained(base, adapter_id)model.eval() system = ( "You are an English vulgar-language detector. " "Read the user text and return JSON only with keys " "is_vulgar, severity, and vulgar_terms. " "is_vulgar is true when the text uses English profanity, slurs, or obscene slang. " "severity is one of none, mild, strong, explicit. " "vulgar_terms is a list of the vulgar words or phrases found, in lowercase, " "normalized without leetspeak or masking characters. " "If the text is clean, set is_vulgar to false, severity to none, and vulgar_terms to []. " "Do not add commentary outside JSON.")text = "What the fuck is wrong with this compiler?"messages = [ {"role": "system", "content": system}, {"role": "user", "content": f"Text: {text}"},]prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)inputs = tokenizer(prompt, return_tensors="pt").to(model.device)with torch.inference_mode(): out = model.generate(**inputs, max_new_tokens=128, do_sample=False)print(tokenizer.decode(out[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True))