Views
No views yet
<think> </think> tags and final answers in \boxed{} notation:1from PIL import Image
2from qwen_vl_utils import process_vision_info
3from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration
4
5# Load model and processor
6model_name = "path/to/cppo-7B"
7processor = AutoProcessor.from_pretrained(model_name)
8model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
9 model_name, torch_dtype="auto", device_map="auto"
10)
11
12# Instruction template
13instruction_following = (
14 r"You FIRST think about the reasoning process as an internal monologue and then provide the final answer. "
15 r"The reasoning process MUST BE enclosed within <think> </think> tags. "
16 r"The final answer MUST BE put in \boxed{}."
17)
18
19# Prepare prompt with instruction following
20prompt = "Your question here. " + instruction_following
21messages = [
22 {
23 "role": "user",
24 "content": [
25 {
26 "type": "image",
27 "image": Image.open("path/to/image.jpg"),
28 },
29 {"type": "text", "text": prompt},
30 ],
31 }
32]
33
34# Preparation for inference
35text = processor.apply_chat_template(
36 messages, tokenize=False, add_generation_prompt=True
37)
38image_inputs, video_inputs = process_vision_info(messages)
39
40# Generate output
41inputs = processor(text=[text], images=image_inputs, padding=True, return_tensors="pt").to("cuda")
42outputs = model.generate(**inputs, max_new_tokens=4096)
43generated_ids = [
44 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, outputs)
45]
46response = processor.decode(generated_ids[0], skip_special_tokens=True)
47print(response)