Views
No views yet

pdftex, mathpix, matplotlib, tikz, verovio or pyecharts.
The model can also be used for interactive OCR, where the user can specify the region to be recognized by providing the coordinates or the color of the region's bounding box.1>>> from transformers import AutoProcessor, AutoModelForImageTextToText
2
3>>> device = "cuda" if torch.cuda.is_available() else "cpu"
4>>> model = AutoModelForImageTextToText.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf", device_map=device)
5>>> processor = AutoProcessor.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf")
6
7>>> image = "https://huggingface.co/datasets/hf-internal-testing/fixtures_got_ocr/resolve/main/image_ocr.jpg"
8>>> inputs = processor(image, return_tensors="pt").to(device)
9
10>>> generate_ids = model.generate(
11... **inputs,
12... do_sample=False,
13... tokenizer=processor.tokenizer,
14... stop_strings="<|im_end|>",
15... max_new_tokens=4096,
16... )
17
18>>> processor.decode(generate_ids[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True)
19"R&D QUALITY IMPROVEMENT\nSUGGESTION/SOLUTION FORM\nName/Phone Ext. : (...)"1>>> from transformers import AutoProcessor, AutoModelForImageTextToText
2
3>>> device = "cuda" if torch.cuda.is_available() else "cpu"
4>>> model = AutoModelForImageTextToText.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf", device_map=device)
5>>> processor = AutoProcessor.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf")
6
7>>> image1 = "https://huggingface.co/datasets/hf-internal-testing/fixtures_got_ocr/resolve/main/multi_box.png"
8>>> image2 = "https://huggingface.co/datasets/hf-internal-testing/fixtures_got_ocr/resolve/main/image_ocr.jpg"
9
10>>> inputs = processor([image1, image2], return_tensors="pt").to(device)
11
12>>> generate_ids = model.generate(
13... **inputs,
14... do_sample=False,
15... tokenizer=processor.tokenizer,
16... stop_strings="<|im_end|>",
17... max_new_tokens=4,
18... )
19
20>>> processor.batch_decode(generate_ids[:, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
21["Reducing the number", "R&D QUALITY"]1>>> from transformers import AutoProcessor, AutoModelForImageTextToText
2
3>>> device = "cuda" if torch.cuda.is_available() else "cpu"
4>>> model = AutoModelForImageTextToText.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf", device_map=device)
5>>> processor = AutoProcessor.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf")
6
7>>> image = "https://huggingface.co/datasets/hf-internal-testing/fixtures_got_ocr/resolve/main/latex.png"
8>>> inputs = processor(image, return_tensors="pt", format=True).to(device)
9
10>>> generate_ids = model.generate(
11... **inputs,
12... do_sample=False,
13... tokenizer=processor.tokenizer,
14... stop_strings="<|im_end|>",
15... max_new_tokens=4096,
16... )
17
18>>> processor.decode(generate_ids[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True)
19"\\author{\nHanwen Jiang* \\(\\quad\\) Arjun Karpur \\({ }^{\\dagger} \\quad\\) Bingyi Cao \\({ }^{\\dagger} \\quad\\) (...)"1>>> from transformers import AutoProcessor, AutoModelForImageTextToText
2
3>>> device = "cuda" if torch.cuda.is_available() else "cpu"
4>>> model = AutoModelForImageTextToText.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf", device_map=device)
5>>> processor = AutoProcessor.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf")
6
7>>> image1 = "https://huggingface.co/datasets/hf-internal-testing/fixtures_got_ocr/resolve/main/page1.png"
8>>> image2 = "https://huggingface.co/datasets/hf-internal-testing/fixtures_got_ocr/resolve/main/page2.png"
9>>> inputs = processor([image1, image2], return_tensors="pt", multi_page=True, format=True).to(device)
10
11>>> generate_ids = model.generate(
12... **inputs,
13... do_sample=False,
14... tokenizer=processor.tokenizer,
15... stop_strings="<|im_end|>",
16... max_new_tokens=4096,
17... )
18
19>>> processor.decode(generate_ids[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True)
20"\\title{\nGeneral OCR Theory: Towards OCR-2.0 via a Unified End-to-end Model\n}\n\\author{\nHaoran Wei (...)"1>>> import torch
2>>> from transformers import AutoProcessor, AutoModelForImageTextToText
3
4>>> device = "cuda" if torch.cuda.is_available() else "cpu"
5>>> model = AutoModelForImageTextToText.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf", torch_dtype=torch.bfloat16, device_map=device)
6>>> processor = AutoProcessor.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf")
7
8>>> image = "https://huggingface.co/datasets/hf-internal-testing/fixtures_got_ocr/resolve/main/one_column.png"
9>>> inputs = processor(image, return_tensors="pt", format=True, crop_to_patches=True, max_patches=3).to(device)
10
11>>> generate_ids = model.generate(
12... **inputs,
13... do_sample=False,
14... tokenizer=processor.tokenizer,
15... stop_strings="<|im_end|>",
16... max_new_tokens=4096,
17... )
18
19>>> processor.decode(generate_ids[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True)
20"on developing architectural improvements to make learnable matching methods generalize.\nMotivated by the above observations, (...)"1>>> from transformers import AutoProcessor, AutoModelForImageTextToText
2
3>>> device = "cuda" if torch.cuda.is_available() else "cpu"
4>>> model = AutoModelForImageTextToText.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf", device_map=device)
5>>> processor = AutoProcessor.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf")
6
7>>> image = "https://huggingface.co/datasets/hf-internal-testing/fixtures_got_ocr/resolve/main/multi_box.png"
8>>> inputs = processor(image, return_tensors="pt", color="green").to(device) # or box=[x1, y1, x2, y2] for coordinates (image pixels)
9
10>>> generate_ids = model.generate(
11... **inputs,
12... do_sample=False,
13... tokenizer=processor.tokenizer,
14... stop_strings="<|im_end|>",
15... max_new_tokens=4096,
16... )
17
18>>> processor.decode(generate_ids[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True)
19"You should keep in mind what features from the module should be used, especially \nwhen you’re planning to sell a template."pdftex, mathpix, matplotlib, tikz, verovio or pyecharts.
Here is an example of how to process sheet music:1>>> from transformers import AutoProcessor, AutoModelForImageTextToText
2>>> import verovio
3
4>>> device = "cuda" if torch.cuda.is_available() else "cpu"
5>>> model = AutoModelForImageTextToText.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf", device_map=device)
6>>> processor = AutoProcessor.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf")
7
8>>> image = "https://huggingface.co/datasets/hf-internal-testing/fixtures_got_ocr/resolve/main/sheet_music.png"
9>>> inputs = processor(image, return_tensors="pt", format=True).to(device)
10
11>>> generate_ids = model.generate(
12... **inputs,
13... do_sample=False,
14... tokenizer=processor.tokenizer,
15... stop_strings="<|im_end|>",
16... max_new_tokens=4096,
17... )
18
19>>> outputs = processor.decode(generate_ids[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True)
20>>> tk = verovio.toolkit()
21>>> tk.loadData(outputs)
22>>> tk.setOptions(
23... {
24... "pageWidth": 2100,
25... "pageHeight": 800,
26... "footer": "none",
27... "barLineWidth": 0.5,
28... "beamMaxSlope": 15,
29... "staffLineWidth": 0.2,
30... "spacingStaff": 6,
31... }
32... )
33>>> tk.getPageCount()
34>>> svg = tk.renderToSVG()
35>>> svg = svg.replace('overflow="inherit"', 'overflow="visible"')
36>>> with open("output.svg", "w") as f:
37>>> f.write(svg)1@article{wei2024general,
2 title={General OCR Theory: Towards OCR-2.0 via a Unified End-to-end Model},
3 author={Wei, Haoran and Liu, Chenglong and Chen, Jinyue and Wang, Jia and Kong, Lingyu and Xu, Yanming and Ge, Zheng and Zhao, Liang and Sun, Jianjian and Peng, Yuang and others},
4 journal={arXiv preprint arXiv:2409.01704},
5 year={2024}
6}
7@article{liu2024focus,
8 title={Focus Anywhere for Fine-grained Multi-page Document Understanding},
9 author={Liu, Chenglong and Wei, Haoran and Chen, Jinyue and Kong, Lingyu and Ge, Zheng and Zhu, Zining and Zhao, Liang and Sun, Jianjian and Han, Chunrui and Zhang, Xiangyu},
10 journal={arXiv preprint arXiv:2405.14295},
11 year={2024}
12}
13@article{wei2023vary,
14 title={Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models},
15 author={Wei, Haoran and Kong, Lingyu and Chen, Jinyue and Zhao, Liang and Ge, Zheng and Yang, Jinrong and Sun, Jianjian and Han, Chunrui and Zhang, Xiangyu},
16 journal={arXiv preprint arXiv:2312.06109},
17 year={2023}
18}