Tonic commited on
Commit
cef4f97
1 Parent(s): 4efa799

initial commit

Browse files
Files changed (2) hide show
  1. app.py +112 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModel, AutoTokenizer
4
+ import os
5
+ import base64
6
+ import spaces
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
9
+ model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, device_map='cuda', use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
10
+ model = model.eval().cuda()
11
+
12
+ @spaces.GPU
13
+ def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None, render=False):
14
+ if task == "Plain Text OCR":
15
+ res = model.chat(tokenizer, image, ocr_type='ocr')
16
+ elif task == "Format Text OCR":
17
+ res = model.chat(tokenizer, image, ocr_type='format')
18
+ elif task == "Fine-grained OCR (Box)":
19
+ res = model.chat(tokenizer, image, ocr_type=ocr_type, ocr_box=ocr_box)
20
+ elif task == "Fine-grained OCR (Color)":
21
+ res = model.chat(tokenizer, image, ocr_type=ocr_type, ocr_color=ocr_color)
22
+ elif task == "Multi-crop OCR":
23
+ res = model.chat_crop(tokenizer, image_file=image)
24
+ elif task == "Render Formatted OCR":
25
+ res = model.chat(tokenizer, image, ocr_type='format', render=True, save_render_file='./demo.html')
26
+ with open('./demo.html', 'r') as f:
27
+ html_content = f.read()
28
+ return res, html_content
29
+
30
+ return res, None
31
+
32
+ def update_inputs(task):
33
+ if task == "Plain Text OCR" or task == "Format Text OCR" or task == "Multi-crop OCR":
34
+ return [gr.update(visible=False)] * 4
35
+ elif task == "Fine-grained OCR (Box)":
36
+ return [
37
+ gr.update(visible=True, choices=["ocr", "format"]),
38
+ gr.update(visible=True),
39
+ gr.update(visible=False),
40
+ gr.update(visible=False)
41
+ ]
42
+ elif task == "Fine-grained OCR (Color)":
43
+ return [
44
+ gr.update(visible=True, choices=["ocr", "format"]),
45
+ gr.update(visible=False),
46
+ gr.update(visible=True, choices=["red", "green", "blue"]),
47
+ gr.update(visible=False)
48
+ ]
49
+ elif task == "Render Formatted OCR":
50
+ return [gr.update(visible=False)] * 3 + [gr.update(visible=True)]
51
+
52
+ def ocr_demo(image, task, ocr_type, ocr_box, ocr_color):
53
+ res, html_content = process_image(image, task, ocr_type, ocr_box, ocr_color)
54
+ if html_content:
55
+ return res, html_content
56
+ return res, None
57
+
58
+ with gr.Blocks() as demo:
59
+ gr.Markdown("#🙋🏻‍♂️Welcome to Tonic's🫴🏻📸GOT-OCR")
60
+ with gr.Row():
61
+ with gr.Column():
62
+ image_input = gr.Image(type="filepath", label="Input Image")
63
+ task_dropdown = gr.Dropdown(
64
+ choices=[
65
+ "Plain Text OCR",
66
+ "Format Text OCR",
67
+ "Fine-grained OCR (Box)",
68
+ "Fine-grained OCR (Color)",
69
+ "Multi-crop OCR",
70
+ "Render Formatted OCR"
71
+ ],
72
+ label="Select Task",
73
+ value="Plain Text OCR"
74
+ )
75
+ ocr_type_dropdown = gr.Dropdown(
76
+ choices=["ocr", "format"],
77
+ label="OCR Type",
78
+ visible=False
79
+ )
80
+ ocr_box_input = gr.Textbox(
81
+ label="OCR Box (x1,y1,x2,y2)",
82
+ placeholder="e.g., 100,100,200,200",
83
+ visible=False
84
+ )
85
+ ocr_color_dropdown = gr.Dropdown(
86
+ choices=["red", "green", "blue"],
87
+ label="OCR Color",
88
+ visible=False
89
+ )
90
+ render_checkbox = gr.Checkbox(
91
+ label="Render Result",
92
+ visible=False
93
+ )
94
+ submit_button = gr.Button("Process")
95
+
96
+ with gr.Column():
97
+ output_text = gr.Textbox(label="OCR Result")
98
+ output_html = gr.HTML(label="Rendered HTML Output")
99
+
100
+ task_dropdown.change(
101
+ update_inputs,
102
+ inputs=[task_dropdown],
103
+ outputs=[ocr_type_dropdown, ocr_box_input, ocr_color_dropdown, render_checkbox]
104
+ )
105
+
106
+ submit_button.click(
107
+ ocr_demo,
108
+ inputs=[image_input, task_dropdown, ocr_type_dropdown, ocr_box_input, ocr_color_dropdown],
109
+ outputs=[output_text, output_html]
110
+ )
111
+
112
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ transformers
4
+ megfile