Views
No views yet
1from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration
2import torch
3from qwen_vl_utils import process_vision_info
4import requests
5from PIL import Image
6
7# 1. Define model and processor names
8model_name = "ydeng9/OpenVLThinker-7B"
9processor_name = "Qwen/Qwen2.5-VL-7B-Instruct"
10
11# 2. Load the OpenVLThinker-7B model and processor
12device = "cuda:0" if torch.cuda.is_available() else "cpu"
13model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
14 model_name,
15 torch_dtype=torch.bfloat16,
16 attn_implementation="flash_attention_2",
17 device_map=device
18)
19processor = AutoProcessor.from_pretrained(processor_name)
20
21# 3. Define a sample image URL and an instruction
22image_url = "https://example.com/sample_image.jpg" # replace with your image URL
23instruction = "Example question"
24
25# 4. Create a multimodal prompt using a chat message structure
26messages = [
27 {
28 "role": "user",
29 "content": [
30 {"type": "image", "image": image_url},
31 {"type": "text", "text": instruction},
32 ],
33 }
34]
35
36# 5. Generate a text prompt from the chat messages
37text_prompt = processor.apply_chat_template(
38 messages, tokenize=False, add_generation_prompt=True
39)
40
41# 6. Process image (and video) inputs from the messages
42image_inputs, video_inputs = process_vision_info(messages)
43inputs = processor(
44 text=[text_prompt],
45 images=image_inputs,
46 videos=video_inputs,
47 padding=True,
48 return_tensors="pt",
49).to(device)
50
51# 7. Generate the model's response (with specified generation parameters)
52generated_ids = model.generate(
53 **inputs,
54 do_sample=True,
55 max_new_tokens=2048,
56 top_p=0.001,
57 top_k=1,
58 temperature=0.01,
59 repetition_penalty=1.0,
60)
61
62# 8. Decode the generated tokens into human-readable text
63generated_text = processor.batch_decode(
64 generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
65)[0]
66
67# 9. Print the generated response
68print("Generated Response:")
69print(generated_text)1@misc{deng2025openvlthinker,
2 title={OpenVLThinker: An Early Exploration to Complex Vision-Language Reasoning via Iterative Self-Improvement},
3 author={Yihe Deng and Hritik Bansal and Fan Yin and Nanyun Peng and Wei Wang and Kai-Wei Chang},
4 year={2025},
5 eprint={2503.17352},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV},
8 url={https://arxiv.org/abs/2503.17352},
9}