|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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') |
|
|