from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
from qwen_vl_utils import process_vision_info
import torch, re
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
"zijinghuafen/GM-PRM", torch_dtype="auto", device_map="auto")
processor = AutoProcessor.from_pretrained("zijinghuafen/GM-PRM")
image_path = "problem.png"
response = "<the policy model's step-by-step solution>"
steps = re.split(r"\n\s*\n", response)
prompt = (
"You are an expert in solving multimodal mathematical problems. You will be given:\n"
"1. An image of a multimodal mathematical problem.\n2. A multi-step solution.\n\n"
"**Task**:\nThe tasks you need to do are:\n"
"1. Analyze the purpose of each step and what specific actions were taken.\n"
"2. Analyze each step's correctness in terms of image alignment and reasoning logic.\n"
"- Image alignment: Whether the information and reasoning used in the step are consistent with the content of the provided image.\n"
"- Reasoning logic: Whether the reasoning is logically sound, calculations are correct, and information used matches that from previous steps and question.\n"
"When outputting judgements, you must choose one output from \"Correct\" or \"Incorrect\".\n"
"3. For the first incorrect step, correct it based on your analysis of its error and intent, and output the corrected step at the end of your output.\n\n"
"**Output Format**:\nYou must output your content in the following format:\n"
"### Step 1 ###\nStep intent analysis:[...]\nImage alignment analysis:[...]\n"
"Judgement of image alignment:[Correct/Incorrect]\nReasoning logic analysis:[...]\n"
"Judgement of reasoning logic:[Correct/Incorrect]\nFinal judgement of the current step:[Correct/Incorrect]\n\n"
"### Step 2 ###\n...\n\n"
"Corrected step of the first incorrect step:[If there are incorrect steps, the corrected step of the first incorrect step goes here. Otherwise, omit this line]\n\n"
"**Problem**:\nThe image of problem is as follows:\n<image>\n\n"
"**Solution Steps**:\nSteps you need to analyze and judge are as follows:\n"
)
for j, s in enumerate(steps):
prompt += f"Step {j+1}: {s}\n\n"
messages = [{"role": "user", "content": [
{"type": "image", "image": image_path},
{"type": "text", "text": prompt},
]}]
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
image_inputs, _ = process_vision_info(messages)
inputs = processor(text=[text], images=image_inputs, padding=True, return_tensors="pt").to(model.device)
with torch.no_grad():
out = model.generate(**inputs, max_new_tokens=2048, do_sample=False)
print(processor.batch_decode([out[0][inputs.input_ids.shape[1]:]], skip_special_tokens=True)[0])