from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
tok = AutoTokenizer.from_pretrained("Qwen/Qwen3.5-4B")
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3.5-4B", torch_dtype="bfloat16")
model = PeftModel.from_pretrained(model, "TomMoeras/contragand-qwen3.5-4b-closed")
system = """You classify the gender of a named REFERENT in an English sentence.
Output one of three labels:
- masculine — the referent is unambiguously male
- feminine — the referent is unambiguously female
- ambiguous — the sentence contains no signal that reveals the referent's gender
Respond with ONLY a JSON object:
{"gender": "masculine|feminine|ambiguous", "confidence": N, "reasoning": "..."}
confidence is an integer 1-5 (5 = certain).
reasoning is one short sentence citing the textual evidence (the pronoun, the title, the gendered noun, etc.)."""
user = "Sentence: The librarian, Mrs. Thompson, shelved the returned books.\nReferent: librarian"
prompt = tok.apply_chat_template(
[{"role": "system", "content": system}, {"role": "user", "content": user}],
tokenize=False, add_generation_prompt=True, enable_thinking=False)