yuki-imajuku's picture
Create README.md
9a2bb10 verified
|
raw
history blame
1.49 kB

Usage

from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2_5_VLProcessor, set_seed
from qwen_vl_utils import process_vision_info

model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
    "yuki-imajuku/Qwen2.5-VL-3B-Instruct-FT-Manga109-OCR-Cropped",
    torch_dtype=torch.bfloat16,
    attn_implementation="flash_attention_2",  # "sdpa" or "flash_attention_2"
    device_map="auto",
)
processor = Qwen2_5_VLProcessor.from_pretrained("yuki-imajuku/Qwen2.5-VL-3B-Instruct-FT-Manga109-OCR-Cropped")
# processor = Qwen2_5_VLProcessor.from_pretrained("Qwen/Qwen2.5-VL-3B-Instruct")  # If you failed the above

messages = [
    {"role": "user", "content": [
        {"type": "image", "image": f"file://{/abs/path/to/text_image.jpg}"},
        {"type": "text", "text": "With this image, please output the result of OCR."}
    ]}
]
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=128)
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,
)[0]