Views
No views yet
def construct_messages(image_root, x):
QUESTION_TEMPLATE = "{Question} First output the thinking process in <think> </think> tags and then output the final answer in <answer> </answer> tags. Output the final answer in string format."
image_path = os.path.join(image_root, x['image'])
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": f"file://{image_path}"
},
{
"type": "text",
"text": QUESTION_TEMPLATE.format(Question=x['problem'])
}
]
}]
return messages
class QWEN_VL_Inference():
def __init__(self, base_model_path, adapter_path):
self.base_model_path = base_model_path
self.adapter_path = adapter_path
self.processor = AutoProcessor.from_pretrained(self.base_model_path)
self.load_merge_model()
def load_merge_model(self):
self.base_model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
self.base_model_path,
torch_dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
device_map="auto"
)
self.model = PeftModel.from_pretrained(self.base_model, self.adapter_path)
self.merged_model = self.model.merge_and_unload()
def infer(self, messages):
text = self.processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
image_inputs, _ = process_vision_info(messages)
inputs = self.processor(text=[text], images=image_inputs, return_tensors="pt", padding=True).to('cuda')
generated_ids = self.merged_model.generate(**inputs, max_new_tokens=512)
generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]
output_text = self.processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True,clean_up_tokenization_spaces=False)
return output_text