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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
import base64
import os
import re
import shutil
import time
import uuid
from pathlib import Path

import cv2
import gradio as gr
import numpy as np
import spaces
import torch
from globe import description, title
from PIL import Image
from render import render_ocr_text

from transformers import AutoModelForImageTextToText, AutoProcessor
from transformers.image_utils import load_image

model_name = "yonigozlan/GOT-OCR-2.0-hf"

device = "cuda" if torch.cuda.is_available() else "cpu"

processor = AutoProcessor.from_pretrained(model_name)
model = AutoModelForImageTextToText.from_pretrained(
    model_name, low_cpu_mem_usage=True, device_map=device
)
model = model.eval().to(device)

UPLOAD_FOLDER = "./uploads"
RESULTS_FOLDER = "./results"
stop_str = "<|im_end|>"
for folder in [UPLOAD_FOLDER, RESULTS_FOLDER]:
    if not os.path.exists(folder):
        os.makedirs(folder)

input_index = 0


@spaces.GPU()
def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None):
    if image is None:
        return "Error: No image provided", None, None

    unique_id = str(uuid.uuid4())
    image_path = os.path.join(UPLOAD_FOLDER, f"{unique_id}.png")
    result_path = os.path.join(RESULTS_FOLDER, f"{unique_id}.html")
    try:
        if not isinstance(image, (tuple, list)):
            image = [image]
        else:
            image = [img[0] for img in image]
        for i, img in enumerate(image):
            if isinstance(img, dict):
                composite_image = img.get("composite")
                if composite_image is not None:
                    if isinstance(composite_image, np.ndarray):
                        cv2.imwrite(
                            image_path, cv2.cvtColor(composite_image, cv2.COLOR_RGB2BGR)
                        )
                    elif isinstance(composite_image, Image.Image):
                        composite_image.save(image_path)
                    else:
                        return (
                            "Error: Unsupported image format from ImageEditor",
                            None,
                            None,
                        )
                else:
                    return (
                        "Error: No composite image found in ImageEditor output",
                        None,
                        None,
                    )
            elif isinstance(img, np.ndarray):
                cv2.imwrite(image_path, cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
            elif isinstance(img, str):
                shutil.copy(img, image_path)
            else:
                return "Error: Unsupported image format", None, None

            image[i] = load_image(image_path)

        if task == "Plain Text OCR":
            inputs = processor(image, return_tensors="pt").to("cuda")
            generate_ids = model.generate(
                **inputs,
                do_sample=False,
                tokenizer=processor.tokenizer,
                stop_strings=stop_str,
                max_new_tokens=4096,
            )
            res = processor.decode(
                generate_ids[0, inputs["input_ids"].shape[1] :],
                skip_special_tokens=True,
            )
            return res, None, unique_id
        else:
            if task == "Format Text OCR":
                inputs = processor(image, return_tensors="pt", format=True).to("cuda")
                generate_ids = model.generate(
                    **inputs,
                    do_sample=False,
                    tokenizer=processor.tokenizer,
                    stop_strings=stop_str,
                    max_new_tokens=4096,
                )
                res = processor.decode(
                    generate_ids[0, inputs["input_ids"].shape[1] :],
                    skip_special_tokens=True,
                )
                ocr_type = "format"
            elif task == "Fine-grained OCR (Box)":
                inputs = processor(image, return_tensors="pt", box=ocr_box).to("cuda")
                generate_ids = model.generate(
                    **inputs,
                    do_sample=False,
                    tokenizer=processor.tokenizer,
                    stop_strings=stop_str,
                    max_new_tokens=4096,
                )
                res = processor.decode(
                    generate_ids[0, inputs["input_ids"].shape[1] :],
                    skip_special_tokens=True,
                )
            elif task == "Fine-grained OCR (Color)":
                inputs = processor(image, return_tensors="pt", color=ocr_color).to(
                    "cuda"
                )
                generate_ids = model.generate(
                    **inputs,
                    do_sample=False,
                    tokenizer=processor.tokenizer,
                    stop_strings=stop_str,
                    max_new_tokens=4096,
                )
                res = processor.decode(
                    generate_ids[0, inputs["input_ids"].shape[1] :],
                    skip_special_tokens=True,
                )
            elif task == "Multi-crop OCR":
                inputs = processor(
                    image,
                    return_tensors="pt",
                    format=True,
                    crop_to_patches=True,
                    max_patches=5,
                ).to("cuda")
                generate_ids = model.generate(
                    **inputs,
                    do_sample=False,
                    tokenizer=processor.tokenizer,
                    stop_strings=stop_str,
                    max_new_tokens=4096,
                )
                res = processor.decode(
                    generate_ids[0, inputs["input_ids"].shape[1] :],
                    skip_special_tokens=True,
                )
                ocr_type = "format"
            elif task == "Multi-page OCR":
                inputs = processor(
                    image, return_tensors="pt", multi_page=True, format=True
                ).to("cuda")
                generate_ids = model.generate(
                    **inputs,
                    do_sample=False,
                    tokenizer=processor.tokenizer,
                    stop_strings=stop_str,
                    max_new_tokens=4096,
                )
                res = processor.decode(
                    generate_ids[0, inputs["input_ids"].shape[1] :],
                    skip_special_tokens=True,
                )
                ocr_type = "format"

            render_ocr_text(res, result_path, format_text=ocr_type == "format")
            if os.path.exists(result_path):
                with open(result_path, "r") as f:
                    html_content = f.read()
                return res, html_content, unique_id
            else:
                return res, None, unique_id
    except Exception as e:
        return f"Error: {str(e)}", None, None
    finally:
        if os.path.exists(image_path):
            os.remove(image_path)


def update_image_input(task):
    if task == "Fine-grained OCR (Color)":
        return (
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=True),
            gr.update(visible=False),
            gr.update(visible=False),
        )
    elif task == "Multi-page OCR":
        return (
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=True),
        )
    else:
        return (
            gr.update(visible=True),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
        )


def update_inputs(task):
    if task in [
        "Plain Text OCR",
        "Format Text OCR",
        "Multi-crop OCR",
    ]:
        return [
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
        ]
    elif task == "Fine-grained OCR (Box)":
        return [
            gr.update(visible=True, choices=["ocr", "format"]),
            gr.update(visible=True),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
        ]
    elif task == "Fine-grained OCR (Color)":
        return [
            gr.update(visible=True, choices=["ocr", "format"]),
            gr.update(visible=False),
            gr.update(visible=True, choices=["red", "green", "blue"]),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=False),
            gr.update(visible=False),
        ]
    elif task == "Multi-page OCR":
        return [
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=True),
        ]


def parse_latex_output(res):
    # Split the input, preserving newlines and empty lines
    lines = re.split(r"(\$\$.*?\$\$)", res, flags=re.DOTALL)
    parsed_lines = []
    in_latex = False
    latex_buffer = []

    for line in lines:
        if line == "\n":
            if in_latex:
                latex_buffer.append(line)
            else:
                parsed_lines.append(line)
            continue

        line = line.strip()

        latex_patterns = [r"\{", r"\}", r"\[", r"\]", r"\\", r"\$", r"_", r"^", r'"']
        contains_latex = any(re.search(pattern, line) for pattern in latex_patterns)

        if contains_latex:
            if not in_latex:
                in_latex = True
                latex_buffer = ["$$"]
            latex_buffer.append(line)
        else:
            if in_latex:
                latex_buffer.append("$$")
                parsed_lines.extend(latex_buffer)
                in_latex = False
                latex_buffer = []
            parsed_lines.append(line)

    if in_latex:
        latex_buffer.append("$$")
        parsed_lines.extend(latex_buffer)

    return "$$\\$$\n".join(parsed_lines)


def ocr_demo(image, task, ocr_type, ocr_box, ocr_color):
    res, html_content, unique_id = process_image(
        image, task, ocr_type, ocr_box, ocr_color
    )

    if isinstance(res, str) and res.startswith("Error:"):
        return res, None

    res = res.replace("\\title", "\\title ")
    formatted_res = res
    # formatted_res = parse_latex_output(res)

    if html_content:
        encoded_html = base64.b64encode(html_content.encode("utf-8")).decode("utf-8")
        iframe_src = f"data:text/html;base64,{encoded_html}"
        iframe = f'<iframe src="{iframe_src}" width="100%" height="600px"></iframe>'
        download_link = f'<a href="data:text/html;base64,{encoded_html}" download="result_{unique_id}.html">Download Full Result</a>'
        return formatted_res, f"{download_link}<br>{iframe}"
    return formatted_res, None


def cleanup_old_files():
    current_time = time.time()
    for folder in [UPLOAD_FOLDER, RESULTS_FOLDER]:
        for file_path in Path(folder).glob("*"):
            if current_time - file_path.stat().st_mtime > 3600:  # 1 hour
                file_path.unlink()


with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown(title)
    gr.Markdown(description)

    with gr.Row():
        with gr.Column(scale=1):
            with gr.Group():
                image_input = gr.Image(type="filepath", label="Input Image")
                gallery_input = gr.Gallery(
                    type="filepath", label="Input images", visible=False
                )
                image_editor = gr.ImageEditor(
                    label="Image Editor", type="pil", visible=False
                )
                task_dropdown = gr.Dropdown(
                    choices=[
                        "Plain Text OCR",
                        "Format Text OCR",
                        "Fine-grained OCR (Box)",
                        "Fine-grained OCR (Color)",
                        "Multi-crop OCR",
                        "Multi-page OCR",
                    ],
                    label="Select Task",
                    value="Plain Text OCR",
                )
                ocr_type_dropdown = gr.Dropdown(
                    choices=["ocr", "format"], label="OCR Type", visible=False
                )
                ocr_box_input = gr.Textbox(
                    label="OCR Box (x1,y1,x2,y2)",
                    placeholder="[100,100,200,200]",
                    visible=False,
                )
                ocr_color_dropdown = gr.Dropdown(
                    choices=["red", "green", "blue"], label="OCR Color", visible=False
                )
                # with gr.Row():
                # max_new_tokens_slider = gr.Slider(50, 500, step=10, value=150, label="Max New Tokens")
                # no_repeat_ngram_size_slider = gr.Slider(1, 10, step=1, value=2, label="No Repeat N-gram Size")

                submit_button = gr.Button("Process", variant="primary")
                editor_submit_button = gr.Button("Process Edited Image", visible=False, variant="primary")
                gallery_submit_button = gr.Button(
                    "Process Multiple Images", visible=False, variant="primary"
                )

        with gr.Column(scale=1):
            with gr.Group():
                output_markdown = gr.Textbox(label="Text output")
                output_html = gr.HTML(label="HTML output")

    input_types = [
        image_input,
        image_editor,
        gallery_input,
    ]

    task_dropdown.change(
        update_inputs,
        inputs=[task_dropdown],
        outputs=[
            ocr_type_dropdown,
            ocr_box_input,
            ocr_color_dropdown,
            image_input,
            image_editor,
            submit_button,
            editor_submit_button,
            gallery_input,
            gallery_submit_button,
        ],
    )

    task_dropdown.change(
        update_image_input,
        inputs=[task_dropdown],
        outputs=[
            image_input,
            image_editor,
            editor_submit_button,
            gallery_input,
            gallery_submit_button,
        ],
    )

    submit_button.click(
        ocr_demo,
        inputs=[
            image_input,
            task_dropdown,
            ocr_type_dropdown,
            ocr_box_input,
            ocr_color_dropdown,
        ],
        outputs=[output_markdown, output_html],
    )
    editor_submit_button.click(
        ocr_demo,
        inputs=[
            image_editor,
            task_dropdown,
            ocr_type_dropdown,
            ocr_box_input,
            ocr_color_dropdown,
        ],
        outputs=[output_markdown, output_html],
    )
    gallery_submit_button.click(
        ocr_demo,
        inputs=[
            gallery_input,
            task_dropdown,
            ocr_type_dropdown,
            ocr_box_input,
            ocr_color_dropdown,
        ],
        outputs=[output_markdown, output_html],
    )
    example = gr.Examples(
        examples=[
            [
                "./sheet_music.png",
                "Format Text OCR",
                "format",
                None,
                None,
            ],
            [
                "./latex.png",
                "Format Text OCR",
                "format",
                None,
                None,
            ],
        ],
        inputs=[
            image_input,
            task_dropdown,
            ocr_type_dropdown,
            ocr_box_input,
            ocr_color_dropdown,
        ],
        outputs=[output_markdown, output_html],
    )
    example_finegrained = gr.Examples(
        examples=[
            [
                "./multi_box.png",
                "Fine-grained OCR (Color)",
                "ocr",
                None,
                "red",
            ]
        ],
        inputs=[
            image_editor,
            task_dropdown,
            ocr_type_dropdown,
            ocr_box_input,
            ocr_color_dropdown,
        ],
        outputs=[output_markdown, output_html],
        label="Fine-grained example",
    )

    gr.Markdown(
        "Space based on [Tonic's GOT-OCR](https://huggingface.co./spaces/Tonic/GOT-OCR)"
    )


if __name__ == "__main__":
    cleanup_old_files()
    demo.launch()