Datasets:

ArXiv:
KIE-HVQA / infer_qwen.py
yuti201's picture
add license
ae624b5
# coding=utf-8
# The following code are reused from the QWen project (https://huggingface.co/Qwen/Qwen2.5-VL-72B-Instruct) of Alibaba Cloud.
# Copyright 2025 The Qwen team, Alibaba Group and the HuggingFace Inc. team. All rights reserved.
#
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
# and OPT implementations in this library. It has been modified from its
# original forms to accommodate minor architectural differences compared
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# The code is modified by ByteDance and Tsinghua University from the original implementation of Qwen:
# - Support time series modality for Qwen2 model.
import json
import torch
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
from qwen_vl_utils import process_vision_info
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
"./Qwen2.5-VL-72B-Instruct", torch_dtype="auto", device_map="auto"
)
processor = AutoProcessor.from_pretrained("./Qwen2.5-VL-72B-Instruct",max_pixels = 2097152)
def perform_inference(messages):
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt",
)
inputs = inputs.to(model.device)
generated_ids = model.generate(**inputs, max_new_tokens=1000)
generated_ids_trimmed = [
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
return output_text
input_file = 'path/input'
output_file = 'path/outout'
with open(input_file, 'r') as f_in, open(output_file, 'w') as f_out:
for line in f_in:
data = json.loads(line.strip())
image_url = data['image']
problem = data['problem']
messages = [ {"role": "system", "content": '''You are a highly specialized OCR model that processes images containing text and number. Your task is to answer the question and categorize each character based on its clarity. If you are unable to recognize a character or number, you must explicitly refuse to recognize it. For each other readable character, categorize it as either "clear" or "not clear enough." Finally, generate an output in the following format:
Clear Char-level OCR: List characters in the answer that are clearly recognize, separated by spaces.
Not clear enough Char-level OCR: List characters in the answer that are recognized but not clear enough, separated by spaces.
Clear number: Count of numbers that are clearly recognized.
Not clear enough number: Count of numbers that are recognized but not clear enough.
Final OCR: Compile all recognized characters and numbers in the answer into a single output, maintaining their original order.
#Output Format: Json only(Exclude any other thought process in the output)
{
"clear Char-level OCR": "0 1 5 S t i t i",
"not clear enough Char-level OCR": "2",
"clear number": 8,
"not clear enough number": 1,
"Final OCR": "2015 S ti ti"
}
'''},
{
"role": "user",
"content": [
{"type": "image", "image": image_url},
{"type": "text", "text": problem.replace("Answer this question using the text in the image directly","")},
],
}
]
output_text = perform_inference(messages)
output_data = {
"image": image_url,
"answer": data['answer'],
"response": output_text
}
f_out.write(json.dumps(output_data) + '\n')