import refrom pathlib import Path from huggingface_hub import hf_hub_downloadfrom PIL import Imagefrom peft import PeftModelfrom transformers import AutoProcessor, Qwen3VLForConditionalGeneration REPO = "theluka2/xplainverse-qwen3vl-complex-grpo"BASE = "Qwen/Qwen3-VL-8B-Instruct"SECTION = re.compile(r"^===\s*(.+?)\s*===\s*$") def load_prompt_section(prompt_path: Path, section: str) -> str: current = None buf: list[str] = [] sections: dict[str, str] = {} for line in prompt_path.read_text(encoding="utf-8").splitlines(): m = SECTION.match(line) if m: name = m.group(1).strip() if current is not None: sections[current] = "\n".join(buf).strip() current = None if name.upper() == "END" else name buf = [] continue if current is not None: buf.append(line) if current is not None: sections[current] = "\n".join(buf).strip() return sections[section] prompt_path = Path( hf_hub_download(REPO, "prompt_v2.txt", local_dir="."))pass1_label = "fake" # or "real"key = ( "VLM_USER_PROMPT_HYPOTHETICAL_FAKE" if pass1_label == "fake" else "VLM_USER_PROMPT_HYPOTHETICAL_REAL")prompt_text = load_prompt_section(prompt_path, key) model = Qwen3VLForConditionalGeneration.from_pretrained( BASE, torch_dtype="bfloat16", device_map="auto")model = PeftModel.from_pretrained(model, REPO)processor = AutoProcessor.from_pretrained(BASE) image = Image.open("image.jpg").convert("RGB")messages = [{ "role": "user", "content": [ {"type": "image", "image": image}, {"type": "text", "text": prompt_text}, ],}]inputs = processor.apply_chat_template( messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt",).to(model.device)generated = model.generate(**inputs, max_new_tokens=512)print(processor.batch_decode(generated, skip_special_tokens=True)[0])