import torchfrom transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfigfrom peft import PeftModel BASE = "Qwen/Qwen3-4B-Instruct-2507"tok = AutoTokenizer.from_pretrained(BASE)# NF4 4-bit, the setup Diapason was trained with (about 3-4 GB of VRAM).# Note: plain BitsAndBytesConfig(load_in_4bit=True) defaults to FP4, which gives noticeably worse answers.bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_use_double_quant=True, bnb_4bit_compute_dtype=torch.bfloat16)model = AutoModelForCausalLM.from_pretrained(BASE, quantization_config=bnb, device_map="auto")model = PeftModel.from_pretrained(model, "JumperV/Diapason") SYSTEM = "You are a knowledgeable tutor in opera and music theory. Answer clearly and accurately."STEPS = '\n\nWork it out briefly step by step, then give the final answer on the last line as "Answer: ...".' def ask(question, calculation=False): msgs = [{"role": "system", "content": SYSTEM}, {"role": "user", "content": question + (STEPS if calculation else "")}] inputs = tok.apply_chat_template(msgs, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device) out = model.generate(**inputs, max_new_tokens=400, do_sample=False) return tok.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True) print(ask("Spell the German augmented sixth chord in C minor.", calculation=True))print(ask("What is a leitmotif, and which composer is most associated with it?"))