Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
from pathlib import Path
|
|
@@ -12,9 +13,10 @@ from image_utils import load_image
|
|
| 12 |
|
| 13 |
MODEL_PATH = os.getenv("MODEL_PATH", "baidu/Qianfan-OCR")
|
| 14 |
MAX_TILES_PER_IMAGE = int(os.getenv("MAX_TILES_PER_IMAGE", "12"))
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
DEFAULT_PROMPT = "Please extract the text from the image."
|
| 19 |
IMAGE_FILE_TYPES = [".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tif", ".tiff"]
|
| 20 |
APP_DIR = Path(__file__).resolve().parent
|
|
@@ -29,6 +31,7 @@ LATEX_FENCE_RE = re.compile(
|
|
| 29 |
r"(^|\n)```(?:latex|tex)[ \t]*\n(?P<body>.*?)(?:\n```)(?=\n|$)",
|
| 30 |
re.IGNORECASE | re.DOTALL,
|
| 31 |
)
|
|
|
|
| 32 |
|
| 33 |
EXAMPLE_ITEMS = [
|
| 34 |
{
|
|
@@ -106,6 +109,13 @@ def validate_prompt(prompt: str) -> str:
|
|
| 106 |
return prompt
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def preview_uploaded_images(file_paths):
|
| 110 |
if not file_paths:
|
| 111 |
return []
|
|
@@ -113,7 +123,7 @@ def preview_uploaded_images(file_paths):
|
|
| 113 |
|
| 114 |
|
| 115 |
def clear_form():
|
| 116 |
-
return None, [], DEFAULT_PROMPT, DEFAULT_MAX_NEW_TOKENS, ""
|
| 117 |
|
| 118 |
|
| 119 |
def load_example(example_index: int):
|
|
@@ -126,6 +136,7 @@ def load_example(example_index: int):
|
|
| 126 |
image_paths,
|
| 127 |
image_paths,
|
| 128 |
example["prompt"],
|
|
|
|
| 129 |
DEFAULT_MAX_NEW_TOKENS,
|
| 130 |
"",
|
| 131 |
)
|
|
@@ -148,13 +159,35 @@ def normalize_markdown_math(text: str) -> str:
|
|
| 148 |
return LATEX_FENCE_RE.sub(replace_latex_fence, text)
|
| 149 |
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
if not file_paths:
|
| 154 |
raise gr.Error("Please upload at least one image.")
|
| 155 |
|
| 156 |
pixel_values = load_images(file_paths, _MODEL.device, _MODEL.dtype)
|
| 157 |
-
question =
|
| 158 |
generation_config = {"max_new_tokens": int(max_new_tokens)}
|
| 159 |
|
| 160 |
with torch.no_grad():
|
|
@@ -164,7 +197,7 @@ def run_inference(file_paths, prompt, max_new_tokens):
|
|
| 164 |
question=question,
|
| 165 |
generation_config=generation_config,
|
| 166 |
)
|
| 167 |
-
return normalize_markdown_math(response)
|
| 168 |
|
| 169 |
|
| 170 |
def build_demo():
|
|
@@ -204,6 +237,10 @@ def build_demo():
|
|
| 204 |
value=DEFAULT_PROMPT,
|
| 205 |
placeholder="Describe the task for the uploaded image(s).",
|
| 206 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
max_new_tokens_input = gr.Slider(
|
| 208 |
label="max_new_tokens",
|
| 209 |
minimum=256,
|
|
@@ -249,6 +286,7 @@ def build_demo():
|
|
| 249 |
image_input,
|
| 250 |
image_preview,
|
| 251 |
prompt_input,
|
|
|
|
| 252 |
max_new_tokens_input,
|
| 253 |
output_box,
|
| 254 |
],
|
|
@@ -261,7 +299,7 @@ def build_demo():
|
|
| 261 |
)
|
| 262 |
submit_button.click(
|
| 263 |
fn=run_inference,
|
| 264 |
-
inputs=[image_input, prompt_input, max_new_tokens_input],
|
| 265 |
outputs=output_box,
|
| 266 |
)
|
| 267 |
clear_button.click(
|
|
@@ -270,6 +308,7 @@ def build_demo():
|
|
| 270 |
image_input,
|
| 271 |
image_preview,
|
| 272 |
prompt_input,
|
|
|
|
| 273 |
max_new_tokens_input,
|
| 274 |
output_box,
|
| 275 |
],
|
|
|
|
| 1 |
+
import math
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
from pathlib import Path
|
|
|
|
| 13 |
|
| 14 |
MODEL_PATH = os.getenv("MODEL_PATH", "baidu/Qianfan-OCR")
|
| 15 |
MAX_TILES_PER_IMAGE = int(os.getenv("MAX_TILES_PER_IMAGE", "12"))
|
| 16 |
+
DEFAULT_MAX_NEW_TOKENS = 2048
|
| 17 |
+
MAX_NEW_TOKENS_LIMIT = 8192
|
| 18 |
+
ZEROGPU_DURATION_MIN = 60
|
| 19 |
+
ZEROGPU_DURATION_MAX = 300
|
| 20 |
DEFAULT_PROMPT = "Please extract the text from the image."
|
| 21 |
IMAGE_FILE_TYPES = [".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tif", ".tiff"]
|
| 22 |
APP_DIR = Path(__file__).resolve().parent
|
|
|
|
| 31 |
r"(^|\n)```(?:latex|tex)[ \t]*\n(?P<body>.*?)(?:\n```)(?=\n|$)",
|
| 32 |
re.IGNORECASE | re.DOTALL,
|
| 33 |
)
|
| 34 |
+
THINK_BLOCK_RE = re.compile(r"<think>.*?</think>", re.DOTALL | re.IGNORECASE)
|
| 35 |
|
| 36 |
EXAMPLE_ITEMS = [
|
| 37 |
{
|
|
|
|
| 109 |
return prompt
|
| 110 |
|
| 111 |
|
| 112 |
+
def build_question(prompt: str, layout_as_thought: bool) -> str:
|
| 113 |
+
question = validate_prompt(prompt)
|
| 114 |
+
if layout_as_thought and not question.endswith("<think>"):
|
| 115 |
+
question = f"{question}<think>"
|
| 116 |
+
return question
|
| 117 |
+
|
| 118 |
+
|
| 119 |
def preview_uploaded_images(file_paths):
|
| 120 |
if not file_paths:
|
| 121 |
return []
|
|
|
|
| 123 |
|
| 124 |
|
| 125 |
def clear_form():
|
| 126 |
+
return None, [], DEFAULT_PROMPT, False, DEFAULT_MAX_NEW_TOKENS, ""
|
| 127 |
|
| 128 |
|
| 129 |
def load_example(example_index: int):
|
|
|
|
| 136 |
image_paths,
|
| 137 |
image_paths,
|
| 138 |
example["prompt"],
|
| 139 |
+
False,
|
| 140 |
DEFAULT_MAX_NEW_TOKENS,
|
| 141 |
"",
|
| 142 |
)
|
|
|
|
| 159 |
return LATEX_FENCE_RE.sub(replace_latex_fence, text)
|
| 160 |
|
| 161 |
|
| 162 |
+
def wrap_think_blocks(text: str) -> str:
|
| 163 |
+
if not text:
|
| 164 |
+
return text
|
| 165 |
+
|
| 166 |
+
def replace_think_block(match: re.Match[str]) -> str:
|
| 167 |
+
block = match.group(0).strip()
|
| 168 |
+
return f"\n```text\n{block}\n```\n"
|
| 169 |
+
|
| 170 |
+
return THINK_BLOCK_RE.sub(replace_think_block, text)
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def estimate_zerogpu_duration(file_paths, prompt, layout_as_thought, max_new_tokens):
|
| 174 |
+
del file_paths, prompt, layout_as_thought
|
| 175 |
+
|
| 176 |
+
estimated_duration = math.ceil(int(max_new_tokens) / 25 + 15)
|
| 177 |
+
final_duration = max(
|
| 178 |
+
ZEROGPU_DURATION_MIN,
|
| 179 |
+
min(ZEROGPU_DURATION_MAX, estimated_duration),
|
| 180 |
+
)
|
| 181 |
+
return final_duration
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
@spaces.GPU(duration=estimate_zerogpu_duration)
|
| 185 |
+
def run_inference(file_paths, prompt, layout_as_thought, max_new_tokens):
|
| 186 |
if not file_paths:
|
| 187 |
raise gr.Error("Please upload at least one image.")
|
| 188 |
|
| 189 |
pixel_values = load_images(file_paths, _MODEL.device, _MODEL.dtype)
|
| 190 |
+
question = build_question(prompt, layout_as_thought)
|
| 191 |
generation_config = {"max_new_tokens": int(max_new_tokens)}
|
| 192 |
|
| 193 |
with torch.no_grad():
|
|
|
|
| 197 |
question=question,
|
| 198 |
generation_config=generation_config,
|
| 199 |
)
|
| 200 |
+
return normalize_markdown_math(wrap_think_blocks(response))
|
| 201 |
|
| 202 |
|
| 203 |
def build_demo():
|
|
|
|
| 237 |
value=DEFAULT_PROMPT,
|
| 238 |
placeholder="Describe the task for the uploaded image(s).",
|
| 239 |
)
|
| 240 |
+
layout_as_thought_input = gr.Checkbox(
|
| 241 |
+
label="Layout-as-Thought",
|
| 242 |
+
value=False,
|
| 243 |
+
)
|
| 244 |
max_new_tokens_input = gr.Slider(
|
| 245 |
label="max_new_tokens",
|
| 246 |
minimum=256,
|
|
|
|
| 286 |
image_input,
|
| 287 |
image_preview,
|
| 288 |
prompt_input,
|
| 289 |
+
layout_as_thought_input,
|
| 290 |
max_new_tokens_input,
|
| 291 |
output_box,
|
| 292 |
],
|
|
|
|
| 299 |
)
|
| 300 |
submit_button.click(
|
| 301 |
fn=run_inference,
|
| 302 |
+
inputs=[image_input, prompt_input, layout_as_thought_input, max_new_tokens_input],
|
| 303 |
outputs=output_box,
|
| 304 |
)
|
| 305 |
clear_button.click(
|
|
|
|
| 308 |
image_input,
|
| 309 |
image_preview,
|
| 310 |
prompt_input,
|
| 311 |
+
layout_as_thought_input,
|
| 312 |
max_new_tokens_input,
|
| 313 |
output_box,
|
| 314 |
],
|