Spaces:
Running
Running
copy: stepfun-ai/GOT_official_online_demo
Browse files- app.py +150 -0
- requirements.txt +10 -0
app.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import io
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import time
|
6 |
+
import uuid
|
7 |
+
from pathlib import Path
|
8 |
+
|
9 |
+
import gradio as gr
|
10 |
+
from modelscope import AutoModel, AutoTokenizer
|
11 |
+
|
12 |
+
# import numpy as np
|
13 |
+
# import tempfile
|
14 |
+
# from PIL import Image
|
15 |
+
|
16 |
+
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained("stepfun-ai/GOT-OCR2_0", trust_remote_code=True)
|
18 |
+
model = AutoModel.from_pretrained("stepfun-ai/GOT-OCR2_0", trust_remote_code=True, low_cpu_mem_usage=True, device_map="cuda", use_safetensors=True)
|
19 |
+
model = model.eval().cuda()
|
20 |
+
|
21 |
+
UPLOAD_FOLDER = "./uploads"
|
22 |
+
RESULTS_FOLDER = "./results"
|
23 |
+
|
24 |
+
for folder in [UPLOAD_FOLDER, RESULTS_FOLDER]:
|
25 |
+
if not os.path.exists(folder):
|
26 |
+
os.makedirs(folder)
|
27 |
+
|
28 |
+
|
29 |
+
def image_to_base64(image):
|
30 |
+
buffered = io.BytesIO()
|
31 |
+
image.save(buffered, format="PNG")
|
32 |
+
return base64.b64encode(buffered.getvalue()).decode()
|
33 |
+
|
34 |
+
|
35 |
+
def run_GOT(image, got_mode, fine_grained_mode="", ocr_color="", ocr_box=""):
|
36 |
+
unique_id = str(uuid.uuid4())
|
37 |
+
image_path = os.path.join(UPLOAD_FOLDER, f"{unique_id}.png")
|
38 |
+
result_path = os.path.join(RESULTS_FOLDER, f"{unique_id}.html")
|
39 |
+
|
40 |
+
shutil.copy(image, image_path)
|
41 |
+
|
42 |
+
try:
|
43 |
+
if got_mode == "plain texts OCR":
|
44 |
+
res = model.chat(tokenizer, image_path, ocr_type="ocr")
|
45 |
+
return res, None
|
46 |
+
elif got_mode == "format texts OCR":
|
47 |
+
res = model.chat(tokenizer, image_path, ocr_type="format", render=True, save_render_file=result_path)
|
48 |
+
elif got_mode == "plain multi-crop OCR":
|
49 |
+
res = model.chat_crop(tokenizer, image_path, ocr_type="ocr")
|
50 |
+
return res, None
|
51 |
+
elif got_mode == "format multi-crop OCR":
|
52 |
+
res = model.chat_crop(tokenizer, image_path, ocr_type="format", render=True, save_render_file=result_path)
|
53 |
+
elif got_mode == "plain fine-grained OCR":
|
54 |
+
res = model.chat(tokenizer, image_path, ocr_type="ocr", ocr_box=ocr_box, ocr_color=ocr_color)
|
55 |
+
return res, None
|
56 |
+
elif got_mode == "format fine-grained OCR":
|
57 |
+
res = model.chat(tokenizer, image_path, ocr_type="format", ocr_box=ocr_box, ocr_color=ocr_color, render=True, save_render_file=result_path)
|
58 |
+
|
59 |
+
# res_markdown = f"$$ {res} $$"
|
60 |
+
res_markdown = res
|
61 |
+
|
62 |
+
if "format" in got_mode and os.path.exists(result_path):
|
63 |
+
with open(result_path, "r") as f:
|
64 |
+
html_content = f.read()
|
65 |
+
encoded_html = base64.b64encode(html_content.encode("utf-8")).decode("utf-8")
|
66 |
+
iframe_src = f"data:text/html;base64,{encoded_html}"
|
67 |
+
iframe = f'<iframe src="{iframe_src}" width="100%" height="600px"></iframe>'
|
68 |
+
download_link = f'<a href="data:text/html;base64,{encoded_html}" download="result_{unique_id}.html">Download Full Result</a>'
|
69 |
+
return res_markdown, f"{download_link}<br>{iframe}"
|
70 |
+
else:
|
71 |
+
return res_markdown, None
|
72 |
+
except Exception as e:
|
73 |
+
return f"Error: {str(e)}", None
|
74 |
+
finally:
|
75 |
+
if os.path.exists(image_path):
|
76 |
+
os.remove(image_path)
|
77 |
+
|
78 |
+
|
79 |
+
def task_update(task):
|
80 |
+
if "fine-grained" in task:
|
81 |
+
return [
|
82 |
+
gr.update(visible=True),
|
83 |
+
gr.update(visible=False),
|
84 |
+
gr.update(visible=False),
|
85 |
+
]
|
86 |
+
else:
|
87 |
+
return [
|
88 |
+
gr.update(visible=False),
|
89 |
+
gr.update(visible=False),
|
90 |
+
gr.update(visible=False),
|
91 |
+
]
|
92 |
+
|
93 |
+
|
94 |
+
def fine_grained_update(task):
|
95 |
+
if task == "box":
|
96 |
+
return [
|
97 |
+
gr.update(visible=False, value=""),
|
98 |
+
gr.update(visible=True),
|
99 |
+
]
|
100 |
+
elif task == "color":
|
101 |
+
return [
|
102 |
+
gr.update(visible=True),
|
103 |
+
gr.update(visible=False, value=""),
|
104 |
+
]
|
105 |
+
|
106 |
+
|
107 |
+
def cleanup_old_files():
|
108 |
+
current_time = time.time()
|
109 |
+
for folder in [UPLOAD_FOLDER, RESULTS_FOLDER]:
|
110 |
+
for file_path in Path(folder).glob("*"):
|
111 |
+
if current_time - file_path.stat().st_mtime > 3600: # 1 hour
|
112 |
+
file_path.unlink()
|
113 |
+
|
114 |
+
|
115 |
+
with gr.Blocks() as demo:
|
116 |
+
with gr.Row():
|
117 |
+
with gr.Column():
|
118 |
+
image_input = gr.Image(type="filepath", label="上传图片")
|
119 |
+
task_dropdown = gr.Dropdown(
|
120 |
+
choices=[
|
121 |
+
"plain texts OCR",
|
122 |
+
"format texts OCR",
|
123 |
+
"plain multi-crop OCR",
|
124 |
+
"format multi-crop OCR",
|
125 |
+
"plain fine-grained OCR",
|
126 |
+
"format fine-grained OCR",
|
127 |
+
],
|
128 |
+
label="选择GOT模式",
|
129 |
+
value="plain texts OCR",
|
130 |
+
)
|
131 |
+
fine_grained_dropdown = gr.Dropdown(choices=["box", "color"], label="fine-grained type", visible=False)
|
132 |
+
color_dropdown = gr.Dropdown(choices=["red", "green", "blue"], label="color list", visible=False)
|
133 |
+
box_input = gr.Textbox(label="input box: [x1,y1,x2,y2]", placeholder="e.g., [0,0,100,100]", visible=False)
|
134 |
+
submit_button = gr.Button("Submit")
|
135 |
+
|
136 |
+
with gr.Column():
|
137 |
+
ocr_result = gr.Textbox(label="GOT output")
|
138 |
+
|
139 |
+
with gr.Column():
|
140 |
+
gr.Markdown("**如果选择带格式的模式,mathpix结果将自动呈现如下:**")
|
141 |
+
html_result = gr.HTML(label="rendered html", show_label=True)
|
142 |
+
|
143 |
+
task_dropdown.change(task_update, inputs=[task_dropdown], outputs=[fine_grained_dropdown, color_dropdown, box_input])
|
144 |
+
fine_grained_dropdown.change(fine_grained_update, inputs=[fine_grained_dropdown], outputs=[color_dropdown, box_input])
|
145 |
+
|
146 |
+
submit_button.click(run_GOT, inputs=[image_input, task_dropdown, fine_grained_dropdown, color_dropdown, box_input], outputs=[ocr_result, html_result])
|
147 |
+
|
148 |
+
if __name__ == "__main__":
|
149 |
+
cleanup_old_files()
|
150 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
verovio
|
2 |
+
gradio
|
3 |
+
numpy
|
4 |
+
modelscope
|
5 |
+
Pillow
|
6 |
+
tiktoken
|
7 |
+
transformers
|
8 |
+
torch
|
9 |
+
torchvision
|
10 |
+
accelerate
|