Spaces:
Running
on
Zero
Running
on
Zero
add html file handling
Browse files- app.py +55 -30
- requirements.txt +2 -1
app.py
CHANGED
@@ -13,6 +13,8 @@ from globe import title, description, modelinfor, joinus
|
|
13 |
import uuid
|
14 |
import tempfile
|
15 |
import time
|
|
|
|
|
16 |
|
17 |
model_name = 'ucaslcl/GOT-OCR2_0'
|
18 |
|
@@ -27,35 +29,51 @@ def image_to_base64(image):
|
|
27 |
image.save(buffered, format="PNG")
|
28 |
return base64.b64encode(buffered.getvalue()).decode()
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
@spaces.GPU
|
34 |
def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None):
|
|
|
|
|
|
|
35 |
unique_id = str(uuid.uuid4())
|
36 |
-
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
res = model.chat(tokenizer, image, ocr_type='format', render=True, save_render_file=str(temp_html_path))
|
44 |
-
elif task == "Fine-grained OCR (Box)":
|
45 |
-
res = model.chat(tokenizer, image, ocr_type=ocr_type, ocr_box=ocr_box, render=True, save_render_file=str(temp_html_path))
|
46 |
-
elif task == "Fine-grained OCR (Color)":
|
47 |
-
res = model.chat(tokenizer, image, ocr_type=ocr_type, ocr_color=ocr_color, render=True, save_render_file=str(temp_html_path))
|
48 |
-
elif task == "Multi-crop OCR":
|
49 |
-
res = model.chat_crop(tokenizer, image, ocr_type='format', render=True, save_render_file=str(temp_html_path))
|
50 |
-
elif task == "Render Formatted OCR":
|
51 |
-
res = model.chat(tokenizer, image, ocr_type='format', render=True, save_render_file=str(temp_html_path))
|
52 |
-
|
53 |
-
if temp_html_path.exists():
|
54 |
-
with open(temp_html_path, 'r') as f:
|
55 |
-
html_content = f.read()
|
56 |
-
return res, html_content, unique_id
|
57 |
-
else:
|
58 |
return res, None, unique_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
def update_inputs(task):
|
61 |
if task in ["Plain Text OCR", "Format Text OCR", "Multi-crop OCR", "Render Formatted OCR"]:
|
@@ -72,22 +90,29 @@ def update_inputs(task):
|
|
72 |
gr.update(visible=False),
|
73 |
gr.update(visible=True, choices=["red", "green", "blue"]),
|
74 |
]
|
|
|
75 |
def ocr_demo(image, task, ocr_type, ocr_box, ocr_color):
|
76 |
res, html_content, unique_id = process_image(image, task, ocr_type, ocr_box, ocr_color)
|
77 |
|
|
|
|
|
|
|
78 |
res = f"$$ {res} $$"
|
79 |
|
80 |
if html_content:
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
84 |
return res, None
|
85 |
|
86 |
def cleanup_old_files():
|
87 |
current_time = time.time()
|
88 |
-
for
|
89 |
-
|
90 |
-
file_path.
|
|
|
91 |
|
92 |
with gr.Blocks() as demo:
|
93 |
gr.Markdown(title)
|
|
|
13 |
import uuid
|
14 |
import tempfile
|
15 |
import time
|
16 |
+
import shutil
|
17 |
+
|
18 |
|
19 |
model_name = 'ucaslcl/GOT-OCR2_0'
|
20 |
|
|
|
29 |
image.save(buffered, format="PNG")
|
30 |
return base64.b64encode(buffered.getvalue()).decode()
|
31 |
|
32 |
+
UPLOAD_FOLDER = "./uploads"
|
33 |
+
RESULTS_FOLDER = "./results"
|
34 |
+
|
35 |
+
for folder in [UPLOAD_FOLDER, RESULTS_FOLDER]:
|
36 |
+
if not os.path.exists(folder):
|
37 |
+
os.makedirs(folder)
|
38 |
|
39 |
+
@spaces.GPU()
|
40 |
def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None):
|
41 |
+
if image is None:
|
42 |
+
return "Error: No image provided", None, None
|
43 |
+
|
44 |
unique_id = str(uuid.uuid4())
|
45 |
+
image_path = os.path.join(UPLOAD_FOLDER, f"{unique_id}.png")
|
46 |
+
result_path = os.path.join(RESULTS_FOLDER, f"{unique_id}.html")
|
47 |
|
48 |
+
shutil.copy(image, image_path)
|
49 |
+
|
50 |
+
try:
|
51 |
+
if task == "Plain Text OCR":
|
52 |
+
res = model.chat(tokenizer, image_path, ocr_type='ocr')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
return res, None, unique_id
|
54 |
+
else:
|
55 |
+
if task == "Format Text OCR":
|
56 |
+
res = model.chat(tokenizer, image_path, ocr_type='format', render=True, save_render_file=result_path)
|
57 |
+
elif task == "Fine-grained OCR (Box)":
|
58 |
+
res = model.chat(tokenizer, image_path, ocr_type=ocr_type, ocr_box=ocr_box, render=True, save_render_file=result_path)
|
59 |
+
elif task == "Fine-grained OCR (Color)":
|
60 |
+
res = model.chat(tokenizer, image_path, ocr_type=ocr_type, ocr_color=ocr_color, render=True, save_render_file=result_path)
|
61 |
+
elif task == "Multi-crop OCR":
|
62 |
+
res = model.chat_crop(tokenizer, image_path, ocr_type='format', render=True, save_render_file=result_path)
|
63 |
+
elif task == "Render Formatted OCR":
|
64 |
+
res = model.chat(tokenizer, image_path, ocr_type='format', render=True, save_render_file=result_path)
|
65 |
+
|
66 |
+
if os.path.exists(result_path):
|
67 |
+
with open(result_path, 'r') as f:
|
68 |
+
html_content = f.read()
|
69 |
+
return res, html_content, unique_id
|
70 |
+
else:
|
71 |
+
return res, None, unique_id
|
72 |
+
except Exception as e:
|
73 |
+
return f"Error: {str(e)}", None, None
|
74 |
+
finally:
|
75 |
+
if os.path.exists(image_path):
|
76 |
+
os.remove(image_path)
|
77 |
|
78 |
def update_inputs(task):
|
79 |
if task in ["Plain Text OCR", "Format Text OCR", "Multi-crop OCR", "Render Formatted OCR"]:
|
|
|
90 |
gr.update(visible=False),
|
91 |
gr.update(visible=True, choices=["red", "green", "blue"]),
|
92 |
]
|
93 |
+
|
94 |
def ocr_demo(image, task, ocr_type, ocr_box, ocr_color):
|
95 |
res, html_content, unique_id = process_image(image, task, ocr_type, ocr_box, ocr_color)
|
96 |
|
97 |
+
if res.startswith("Error:"):
|
98 |
+
return res, None
|
99 |
+
|
100 |
res = f"$$ {res} $$"
|
101 |
|
102 |
if html_content:
|
103 |
+
encoded_html = base64.b64encode(html_content.encode('utf-8')).decode('utf-8')
|
104 |
+
iframe_src = f"data:text/html;base64,{encoded_html}"
|
105 |
+
iframe = f'<iframe src="{iframe_src}" width="100%" height="600px"></iframe>'
|
106 |
+
download_link = f'<a href="data:text/html;base64,{encoded_html}" download="result_{unique_id}.html">Download Full Result</a>'
|
107 |
+
return res, f"{download_link}<br>{iframe}"
|
108 |
return res, None
|
109 |
|
110 |
def cleanup_old_files():
|
111 |
current_time = time.time()
|
112 |
+
for folder in [UPLOAD_FOLDER, RESULTS_FOLDER]:
|
113 |
+
for file_path in Path(folder).glob('*'):
|
114 |
+
if current_time - file_path.stat().st_mtime > 3600: # 1 hour
|
115 |
+
file_path.unlink()
|
116 |
|
117 |
with gr.Blocks() as demo:
|
118 |
gr.Markdown(title)
|
requirements.txt
CHANGED
@@ -10,4 +10,5 @@
|
|
10 |
numpy==1.26.4
|
11 |
loadimg
|
12 |
pillow
|
13 |
-
markdown
|
|
|
|
10 |
numpy==1.26.4
|
11 |
loadimg
|
12 |
pillow
|
13 |
+
markdown
|
14 |
+
py-shutils
|