ydeng9 commited on
Commit
50bb8d4
·
verified ·
1 Parent(s): 8583fc8

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +83 -0
README.md ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ base_model:
4
+ - Qwen/Qwen2.5-VL-7B-Instruct
5
+ ---
6
+
7
+ ## Overview
8
+ OpenVLThinker-7B is a vision-language reasoning model designed to handle multimodal tasks. It is especially tuned for visual mathematical problem-solving.
9
+
10
+ For more details: [Blog](https://yihe-deng.notion.site/openvlthinker), [GitHub](https://github.com/yihedeng9/OpenVLThinker)
11
+
12
+ ## How to use
13
+ ```
14
+ from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration
15
+ import torch
16
+ from qwen_vl_utils import process_vision_info
17
+ import requests
18
+ from PIL import Image
19
+
20
+ # 1. Define model and processor names
21
+ model_name = "ydeng9/OpenVLThinker-7B"
22
+ processor_name = "Qwen/Qwen2.5-VL-7B-Instruct"
23
+
24
+ # 2. Load the OpenVLThinker-7B model and processor
25
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
26
+ model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
27
+ model_name,
28
+ torch_dtype=torch.bfloat16,
29
+ attn_implementation="flash_attention_2",
30
+ device_map=device
31
+ )
32
+ processor = AutoProcessor.from_pretrained(processor_name)
33
+
34
+ # 3. Define a sample image URL and an instruction
35
+ image_url = "https://example.com/sample_image.jpg" # replace with your image URL
36
+ instruction = "Example question"
37
+
38
+ # 4. Create a multimodal prompt using a chat message structure
39
+ messages = [
40
+ {
41
+ "role": "user",
42
+ "content": [
43
+ {"type": "image", "image": image_url},
44
+ {"type": "text", "text": instruction},
45
+ ],
46
+ }
47
+ ]
48
+
49
+ # 5. Generate a text prompt from the chat messages
50
+ text_prompt = processor.apply_chat_template(
51
+ messages, tokenize=False, add_generation_prompt=True
52
+ )
53
+
54
+ # 6. Process image (and video) inputs from the messages
55
+ image_inputs, video_inputs = process_vision_info(messages)
56
+ inputs = processor(
57
+ text=[text_prompt],
58
+ images=image_inputs,
59
+ videos=video_inputs,
60
+ padding=True,
61
+ return_tensors="pt",
62
+ ).to(device)
63
+
64
+ # 7. Generate the model's response (with specified generation parameters)
65
+ generated_ids = model.generate(
66
+ **inputs,
67
+ do_sample=True,
68
+ max_new_tokens=2048,
69
+ top_p=0.001,
70
+ top_k=1,
71
+ temperature=0.01,
72
+ repetition_penalty=1.0,
73
+ )
74
+
75
+ # 8. Decode the generated tokens into human-readable text
76
+ generated_text = processor.batch_decode(
77
+ generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
78
+ )[0]
79
+
80
+ # 9. Print the generated response
81
+ print("Generated Response:")
82
+ print(generated_text)
83
+ ```