Views
No views yet
1import torch
2from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
3
4# Load the fine-tuned model
5model = Qwen3VLForConditionalGeneration.from_pretrained(
6 "chengyewang/TexOCR-RL",
7 dtype="auto",
8 device_map="auto"
9)
10
11
12processor = AutoProcessor.from_pretrained("Qwen/Qwen3-VL-2B-Instruct")
13
14# Input document page image
15image_path = "path/to/your/document_page.png"
16
17messages = [
18 {
19 "role": "user",
20 "content": [
21 {
22 "type": "image",
23 "image": image_path,
24 },
25 {
26 "type": "text",
27 "text": (
28 "Convert this document page image into compilable LaTeX code. "
29 ),
30 },
31 ],
32 }
33]
34
35# Preparation for inference
36inputs = processor.apply_chat_template(
37 messages,
38 tokenize=True,
39 add_generation_prompt=True,
40 return_dict=True,
41 return_tensors="pt"
42)
43inputs = inputs.to(model.device)
44
45# Inference: generate LaTeX output
46generated_ids = model.generate(
47 **inputs,
48 max_new_tokens=2048,
49 do_sample=False
50)
51
52# Remove input tokens from the generated sequence
53generated_ids_trimmed = [
54 out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
55]
56
57# Decode the generated LaTeX
58latex_output = processor.batch_decode(
59 generated_ids_trimmed,
60 skip_special_tokens=True,
61 clean_up_tokenization_spaces=False
62)
63
64print(latex_output[0])