from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
from PIL import Image
import torch
model_path = "dmis-lab/Qwen3-VL-8B-Instruct-MRPO"
model = Qwen3VLForConditionalGeneration.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
device_map="auto",
)
processor = AutoProcessor.from_pretrained(model_path)
image_path = "path/to/medical/image.jpg"
question = "What can you see in this medical image?"
question_text = (
f"{question} Think step-by-step and enclose your reasoning in "
"<thinking>...</thinking> tags. Then provide your answer in <answer>...</answer> tags."
)
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": image_path},
{"type": "text", "text": question_text},
],
}
]
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
inputs = processor(
text=[text],
images=[Image.open(image_path)],
padding=True,
padding_side="left",
return_tensors="pt",
)
inputs = inputs.to(model.device)
generated_ids = model.generate(**inputs, use_cache=True, max_new_tokens=512, do_sample=False)
generated_ids_trimmed = [
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)