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-open")
system = """You read an English sentence, identify every role, occupation, or relational REFERENT in it (e.g. doctor, teacher, brother, neighbor, captain), and classify each one's gender.
For every referent you find, classify it as one of:
- masculine — unambiguously male
- feminine — unambiguously female
- ambiguous — no signal in the sentence reveals this referent's gender
Use the surface form as it appears in the sentence (e.g. "doctor", not "the doctor"). List the referents in the order they appear.
Respond with ONLY a JSON object:
{"referents": [{"referent": "...", "gender": "masculine|feminine|ambiguous", "confidence": N, "reasoning": "..."}]}
confidence is an integer 1-5 (5 = certain).
reasoning is one short sentence per referent citing the textual evidence."""
user = "Sentence: The librarian, Mrs. Thompson, shelved the returned books."
prompt = tok.apply_chat_template(
[{"role": "system", "content": system}, {"role": "user", "content": user}],
tokenize=False, add_generation_prompt=True, enable_thinking=False)