Views
No views yet
1from transformers import AutoProcessor
2import torch
3from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
4from PIL import Image
5
6MODEL_PATH = "bunny127/SophiaVL-R1-7B"
7# Example usage:
8 # {
9 # "problem_id": 1,
10 # "problem": "Subtract 0 cyan cubes. How many objects are left?",
11 # "data_type": "image",
12 # "problem_type": "numerical",
13 # "options": [],
14 # "process": "",
15 # "solution": "<answer>5</answer>",
16 # "path": "./Math/CLEVR-Math/images/CLEVR_train_036427.png",
17 # "data_source": "CLEVR-Math"
18 # },
19image_path = "/path/to/dataset/Math/CLEVR-Math/images/CLEVR_train_036427.png"
20prompt = "Subtract 0 cyan cubes. How many objects are left?"
21question_type = "numerical"
22
23
24model = Qwen2_5_VLForConditionalGeneration.from_pretrained(MODEL_PATH, torch_dtype=torch.bfloat16, attn_implementation="flash_attention_2",device_map="auto")
25processor = AutoProcessor.from_pretrained(MODEL_PATH)
26
27SYS_PROMPT = """You FIRST think about the reasoning process as an internal monologue and then provide the final answer.
28The reasoning process MUST BE enclosed within <think> </think> tagsdd. The final answer MUST BE enclosed within <answer> </answer> tags, for example <think>your_thinking_process</think><answer>your_final_answer</answer>. If you use formula, please use LaTeX format."""
29
30QUESTION_TEMPLATE = (
31 "{Question}\n"
32 "Please think about this question as if you were a human pondering deeply. "
33 "Engage in an internal dialogue using expressions such as 'let me think', 'wait', 'Hmm', 'oh, I see', 'let's break it down', etc, or other natural language thought expressions "
34 "It's encouraged to include self-reflection or verification in the reasoning process. "
35 "Provide your detailed reasoning between the <think> and </think> tags, and then give your final answer between the <answer> and </answer> tags."
36)
37
38TYPE_TEMPLATE = {
39 "multiple choice": " Please provide only the single option letter (e.g., A, B, C, D, etc.) within the <answer> </answer> tags.",
40 "numerical": " Please provide the numerical value (e.g., 42 or 3.14) within the <answer> </answer> tags.",
41 "OCR": " Please transcribe text from the image/video clearly and provide your text answer within the <answer> </answer> tags.",
42 "free-form": " Please provide your text answer within the <answer> </answer> tags."
43}
44
45def inference(image_path, question, problem_type = "numerical", sys_prompt="You are a helpful assistant.", max_new_tokens=4096, return_input=False):
46 image = Image.open(image_path)
47 image_local_path = "file://" + image_path
48 messages = [
49 {"role": "system", "content": sys_prompt},
50 {"role": "user", "content": [
51 {"type": "text", "text": QUESTION_TEMPLATE.format(Question=question) + TYPE_TEMPLATE[problem_type]},
52 {"image": image_local_path},
53 ]
54 },
55 ]
56 text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
57 print("text:", text)
58 # image_inputs, video_inputs = process_vision_info([messages])
59 inputs = processor(text=[text], images=[image], padding=True, return_tensors="pt")
60 inputs = inputs.to('cuda')
61
62 output_ids = model.generate(**inputs, max_new_tokens=max_new_tokens)
63 generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, output_ids)]
64 output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)
65 if return_input:
66 return output_text[0], inputs
67 else:
68 return output_text[0]
69
70response = inference(image_path, prompt, question_type, sys_prompt=SYS_PROMPT, max_new_tokens=2048)
71print(response)