Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,388 +18,227 @@ from transformers import (
|
|
| 18 |
AutoProcessor,
|
| 19 |
TextIteratorStreamer,
|
| 20 |
)
|
| 21 |
-
from qwen_vl_utils import process_vision_info
|
| 22 |
|
| 23 |
-
# Constants
|
| 24 |
-
|
| 25 |
-
MAX_PIXELS = 11289600
|
| 26 |
-
IMAGE_FACTOR = 28
|
| 27 |
-
MAX_INPUT_TOKEN_LENGTH = 2048
|
| 28 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 29 |
|
| 30 |
-
#
|
| 31 |
prompt = """Please output the layout information from the image, including each layout element's bbox, its category, and the corresponding text content within the bbox.
|
| 32 |
|
| 33 |
1. Bbox format: [x1, y1, x2, y2]
|
| 34 |
-
|
| 35 |
2. Layout Categories: The possible categories are ['Caption', 'Footnote', 'Formula', 'List-item', 'Page-footer', 'Page-header', 'Picture', 'Section-header', 'Table', 'Text', 'Title'].
|
| 36 |
-
|
| 37 |
3. Text Extraction & Formatting Rules:
|
| 38 |
- Picture: For the 'Picture' category, the text field should be omitted.
|
| 39 |
- Formula: Format its text as LaTeX.
|
| 40 |
-
- Table:
|
| 41 |
- All Others (Text, Title, etc.): Format their text as Markdown.
|
| 42 |
-
|
| 43 |
4. Constraints:
|
| 44 |
- The output text must be the original text from the image, with no translation.
|
| 45 |
- All layout elements must be sorted according to human reading order.
|
| 46 |
-
|
| 47 |
-
5. Final Output: The entire output must be a single JSON object.
|
| 48 |
"""
|
| 49 |
|
| 50 |
# Load models
|
| 51 |
MODEL_ID_M = "prithivMLmods/Camel-Doc-OCR-062825"
|
| 52 |
processor_m = AutoProcessor.from_pretrained(MODEL_ID_M, trust_remote_code=True)
|
| 53 |
model_m = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 54 |
-
MODEL_ID_M,
|
| 55 |
-
trust_remote_code=True,
|
| 56 |
-
torch_dtype=torch.float16
|
| 57 |
).to(device).eval()
|
| 58 |
|
| 59 |
MODEL_ID_T = "prithivMLmods/Megalodon-OCR-Sync-0713"
|
| 60 |
processor_t = AutoProcessor.from_pretrained(MODEL_ID_T, trust_remote_code=True)
|
| 61 |
model_t = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 62 |
-
MODEL_ID_T,
|
| 63 |
-
trust_remote_code=True,
|
| 64 |
-
torch_dtype=torch.float16
|
| 65 |
).to(device).eval()
|
| 66 |
|
| 67 |
MODEL_ID_C = "nanonets/Nanonets-OCR-s"
|
| 68 |
processor_c = AutoProcessor.from_pretrained(MODEL_ID_C, trust_remote_code=True)
|
| 69 |
model_c = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 70 |
-
MODEL_ID_C,
|
| 71 |
-
trust_remote_code=True,
|
| 72 |
-
torch_dtype=torch.float16
|
| 73 |
).to(device).eval()
|
| 74 |
|
| 75 |
MODEL_ID_G = "echo840/MonkeyOCR"
|
| 76 |
SUBFOLDER = "Recognition"
|
| 77 |
processor_g = AutoProcessor.from_pretrained(
|
| 78 |
-
MODEL_ID_G,
|
| 79 |
-
trust_remote_code=True,
|
| 80 |
-
subfolder=SUBFOLDER
|
| 81 |
)
|
| 82 |
model_g = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 83 |
-
MODEL_ID_G,
|
| 84 |
-
trust_remote_code=True,
|
| 85 |
-
subfolder=SUBFOLDER,
|
| 86 |
-
torch_dtype=torch.float16
|
| 87 |
).to(device).eval()
|
| 88 |
|
| 89 |
-
# Utility
|
| 90 |
-
def round_by_factor(number: int, factor: int) -> int:
|
| 91 |
-
return round(number / factor) * factor
|
| 92 |
-
|
| 93 |
-
def smart_resize(
|
| 94 |
-
height: int,
|
| 95 |
-
width: int,
|
| 96 |
-
factor: int = 28,
|
| 97 |
-
min_pixels: int = 3136,
|
| 98 |
-
max_pixels: int = 11289600,
|
| 99 |
-
):
|
| 100 |
-
if max(height, width) / min(height, width) > 200:
|
| 101 |
-
raise ValueError(f"Aspect ratio too extreme: {max(height, width) / min(height, width)}")
|
| 102 |
-
h_bar = max(factor, round_by_factor(height, factor))
|
| 103 |
-
w_bar = max(factor, round_by_factor(width, factor))
|
| 104 |
-
if h_bar * w_bar > max_pixels:
|
| 105 |
-
beta = math.sqrt((height * width) / max_pixels)
|
| 106 |
-
h_bar = round_by_factor(height / beta, factor)
|
| 107 |
-
w_bar = round_by_factor(width / beta, factor)
|
| 108 |
-
elif h_bar * w_bar < min_pixels:
|
| 109 |
-
beta = math.sqrt(min_pixels / (height * width))
|
| 110 |
-
h_bar = round_by_factor(height * beta, factor)
|
| 111 |
-
w_bar = round_by_factor(width * beta, factor)
|
| 112 |
-
return h_bar, w_bar
|
| 113 |
-
|
| 114 |
-
def fetch_image(image_input, min_pixels: int = None, max_pixels: int = None):
|
| 115 |
-
if isinstance(image_input, str):
|
| 116 |
-
if image_input.startswith(("http://", "https://")):
|
| 117 |
-
response = requests.get(image_input)
|
| 118 |
-
image = Image.open(BytesIO(response.content)).convert('RGB')
|
| 119 |
-
else:
|
| 120 |
-
image = Image.open(image_input).convert('RGB')
|
| 121 |
-
elif isinstance(image_input, Image.Image):
|
| 122 |
-
image = image_input.convert('RGB')
|
| 123 |
-
else:
|
| 124 |
-
raise ValueError(f"Invalid image input type: {type(image_input)}")
|
| 125 |
-
if min_pixels or max_pixels:
|
| 126 |
-
min_pixels = min_pixels or MIN_PIXELS
|
| 127 |
-
max_pixels = max_pixels or MAX_PIXELS
|
| 128 |
-
height, width = smart_resize(image.height, image.width, factor=IMAGE_FACTOR, min_pixels=min_pixels, max_pixels=max_pixels)
|
| 129 |
-
image = image.resize((width, height), Image.LANCZOS)
|
| 130 |
-
return image
|
| 131 |
-
|
| 132 |
-
def is_arabic_text(text: str) -> bool:
|
| 133 |
-
if not text:
|
| 134 |
-
return False
|
| 135 |
-
header_pattern = r'^#{1,6}\s+(.+)$'
|
| 136 |
-
paragraph_pattern = r'^(?!#{1,6}\s|!\[|```|\||\s*[-*+]\s|\s*\d+\.\s)(.+)$'
|
| 137 |
-
content_text = []
|
| 138 |
-
for line in text.split('\n'):
|
| 139 |
-
line = line.strip()
|
| 140 |
-
if not line:
|
| 141 |
-
continue
|
| 142 |
-
header_match = re.match(header_pattern, line, re.MULTILINE)
|
| 143 |
-
if header_match:
|
| 144 |
-
content_text.append(header_match.group(1))
|
| 145 |
-
continue
|
| 146 |
-
if re.match(paragraph_pattern, line, re.MULTILINE):
|
| 147 |
-
content_text.append(line)
|
| 148 |
-
if not content_text:
|
| 149 |
-
return False
|
| 150 |
-
combined_text = ' '.join(content_text)
|
| 151 |
-
arabic_chars = 0
|
| 152 |
-
total_chars = 0
|
| 153 |
-
for char in combined_text:
|
| 154 |
-
if char.isalpha():
|
| 155 |
-
total_chars += 1
|
| 156 |
-
if ('\u0600' <= char <= '\u06FF') or ('\u0750' <= char <= '\u077F') or ('\u08A0' <= char <= '\u08FF'):
|
| 157 |
-
arabic_chars += 1
|
| 158 |
-
return total_chars > 0 and (arabic_chars / total_chars) > 0.5
|
| 159 |
|
| 160 |
-
def layoutjson2md(
|
| 161 |
-
|
| 162 |
-
from io import BytesIO
|
| 163 |
markdown_lines = []
|
| 164 |
try:
|
| 165 |
sorted_items = sorted(layout_data, key=lambda x: (x.get('bbox', [0, 0, 0, 0])[1], x.get('bbox', [0, 0, 0, 0])[0]))
|
| 166 |
for item in sorted_items:
|
| 167 |
category = item.get('category', '')
|
| 168 |
-
text = item.get(
|
| 169 |
-
|
| 170 |
-
if
|
| 171 |
-
if bbox and len(bbox) == 4:
|
| 172 |
-
try:
|
| 173 |
-
x1, y1, x2, y2 = bbox
|
| 174 |
-
x1, y1 = max(0, int(x1)), max(0, int(y1))
|
| 175 |
-
x2, y2 = min(image.width, int(x2)), min(image.height, int(y2))
|
| 176 |
-
if x2 > x1 and y2 > y1:
|
| 177 |
-
cropped_img = image.crop((x1, y1, x2, y2))
|
| 178 |
-
buffer = BytesIO()
|
| 179 |
-
cropped_img.save(buffer, format='PNG')
|
| 180 |
-
img_data = base64.b64encode(buffer.getvalue()).decode()
|
| 181 |
-
markdown_lines.append(f"\n")
|
| 182 |
-
else:
|
| 183 |
-
markdown_lines.append("\n")
|
| 184 |
-
except Exception as e:
|
| 185 |
-
print(f"Error processing image region: {e}")
|
| 186 |
-
markdown_lines.append("\n")
|
| 187 |
-
else:
|
| 188 |
-
markdown_lines.append("\n")
|
| 189 |
-
elif not text:
|
| 190 |
continue
|
| 191 |
-
|
|
|
|
| 192 |
markdown_lines.append(f"# {text}\n")
|
| 193 |
elif category == 'Section-header':
|
| 194 |
markdown_lines.append(f"## {text}\n")
|
| 195 |
-
elif category == 'Text':
|
| 196 |
-
markdown_lines.append(f"{text}\n")
|
| 197 |
-
elif category == 'List-item':
|
| 198 |
-
markdown_lines.append(f"- {text}\n")
|
| 199 |
elif category == 'Table':
|
| 200 |
-
if text
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
else:
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
if text.strip().startswith('$') or '\\' in text:
|
| 206 |
-
markdown_lines.append(f"$$\n{text}\n$$\n")
|
| 207 |
-
else:
|
| 208 |
-
markdown_lines.append(f"**Formula:** {text}\n")
|
| 209 |
-
elif category == 'Caption':
|
| 210 |
-
markdown_lines.append(f"*{text}*\n")
|
| 211 |
-
elif category == 'Footnote':
|
| 212 |
-
markdown_lines.append(f"^{text}^\n")
|
| 213 |
-
elif category in ['Page-header', 'Page-footer']:
|
| 214 |
-
continue
|
| 215 |
else:
|
| 216 |
markdown_lines.append(f"{text}\n")
|
| 217 |
-
|
| 218 |
except Exception as e:
|
| 219 |
print(f"Error converting to markdown: {e}")
|
| 220 |
-
return
|
| 221 |
return "\n".join(markdown_lines)
|
| 222 |
|
| 223 |
-
|
| 224 |
-
def inference(model_name: str, image: Image.Image, text: str, max_new_tokens: int = 1024) -> str:
|
| 225 |
-
try:
|
| 226 |
-
if model_name == "Camel-Doc-OCR-062825":
|
| 227 |
-
processor = processor_m
|
| 228 |
-
model = model_m
|
| 229 |
-
elif model_name == "Megalodon-OCR-Sync-0713":
|
| 230 |
-
processor = processor_t
|
| 231 |
-
model = model_t
|
| 232 |
-
elif model_name == "Nanonets-OCR-s":
|
| 233 |
-
processor = processor_c
|
| 234 |
-
model = model_c
|
| 235 |
-
elif model_name == "MonkeyOCR-Recognition":
|
| 236 |
-
processor = processor_g
|
| 237 |
-
model = model_g
|
| 238 |
-
else:
|
| 239 |
-
raise ValueError(f"Invalid model selected: {model_name}")
|
| 240 |
-
|
| 241 |
-
if image is None:
|
| 242 |
-
yield "Please upload an image.", "Please upload an image."
|
| 243 |
-
return
|
| 244 |
-
|
| 245 |
-
messages = [{
|
| 246 |
-
"role": "user",
|
| 247 |
-
"content": [
|
| 248 |
-
{"type": "image", "image": image},
|
| 249 |
-
{"type": "text", "text": text},
|
| 250 |
-
]
|
| 251 |
-
}]
|
| 252 |
-
prompt_full = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 253 |
-
inputs = processor(
|
| 254 |
-
text=[prompt_full],
|
| 255 |
-
images=[image],
|
| 256 |
-
return_tensors="pt",
|
| 257 |
-
padding=True,
|
| 258 |
-
truncation=False,
|
| 259 |
-
max_length=MAX_INPUT_TOKEN_LENGTH
|
| 260 |
-
).to(device)
|
| 261 |
-
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
| 262 |
-
generation_kwargs = {**inputs, "streamer": streamer, "max_new_tokens": max_new_tokens}
|
| 263 |
-
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 264 |
-
thread.start()
|
| 265 |
-
buffer = ""
|
| 266 |
-
for new_text in streamer:
|
| 267 |
-
buffer += new_text
|
| 268 |
-
buffer = buffer.replace("<|im_end|>", "")
|
| 269 |
-
time.sleep(0.01)
|
| 270 |
-
yield buffer, buffer
|
| 271 |
-
except Exception as e:
|
| 272 |
-
print(f"Error during inference: {e}")
|
| 273 |
-
traceback.print_exc()
|
| 274 |
-
yield f"Error during inference: {str(e)}", f"Error during inference: {str(e)}"
|
| 275 |
|
| 276 |
@spaces.GPU
|
| 277 |
-
def
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
try:
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
except Exception as e:
|
| 300 |
-
print(f"
|
| 301 |
-
|
| 302 |
-
yield f"Error processing image: {str(e)}", None
|
| 303 |
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
return None, "No file selected"
|
| 307 |
-
file_ext = os.path.splitext(file_path)[1].lower()
|
| 308 |
-
try:
|
| 309 |
-
if file_ext in ['.jpg', '.jpeg', '.png', '.bmp', '.tiff']:
|
| 310 |
-
image = Image.open(file_path).convert('RGB')
|
| 311 |
-
return image, "Image loaded"
|
| 312 |
-
else:
|
| 313 |
-
return None, f"Unsupported file format: {file_ext}"
|
| 314 |
-
except Exception as e:
|
| 315 |
-
print(f"Error loading file: {e}")
|
| 316 |
-
return None, f"Error loading file: {str(e)}"
|
| 317 |
|
| 318 |
def create_gradio_interface():
|
|
|
|
| 319 |
css = """
|
| 320 |
.main-container { max-width: 1400px; margin: 0 auto; }
|
| 321 |
-
.
|
| 322 |
-
.process-button {
|
| 323 |
-
border: none !important;
|
| 324 |
-
color: white !important;
|
| 325 |
-
font-weight: bold !important;
|
| 326 |
-
background-color: blue !important;}
|
| 327 |
-
.process-button:hover {
|
| 328 |
-
background-color: darkblue !important;
|
| 329 |
-
transform: translateY(-2px) !important;
|
| 330 |
-
box-shadow: 0 4px 8px rgba(0,0,0,0.2) !important; }
|
| 331 |
-
.info-box { border: 1px solid #dee2e6; border-radius: 8px; padding: 15px; margin: 10px 0; }
|
| 332 |
-
.page-info { text-align: center; padding: 8px 16px; border-radius: 20px; font-weight: bold; margin: 10px 0; }
|
| 333 |
-
.model-status { padding: 10px; border-radius: 8px; margin: 10px 0; text-align: center; font-weight: bold; }
|
| 334 |
-
.status-ready { background: #d1edff; color: #0c5460; border: 1px solid #b8daff; }
|
| 335 |
"""
|
| 336 |
with gr.Blocks(theme="bethecloud/storj_theme", css=css) as demo:
|
| 337 |
gr.HTML("""
|
| 338 |
<div class="title" style="text-align: center">
|
| 339 |
<h1>Dot<span style="color: red;">β</span><strong></strong>OCR Comparator</h1>
|
| 340 |
<p style="font-size: 1.1em; color: #6b7280; margin-bottom: 0.6em;">
|
| 341 |
-
Advanced
|
| 342 |
</p>
|
| 343 |
</div>
|
| 344 |
""")
|
|
|
|
| 345 |
with gr.Row():
|
|
|
|
| 346 |
with gr.Column(scale=1):
|
| 347 |
model_choice = gr.Radio(
|
| 348 |
choices=["Camel-Doc-OCR-062825", "MonkeyOCR-Recognition", "Nanonets-OCR-s", "Megalodon-OCR-Sync-0713"],
|
| 349 |
label="Select Model",
|
| 350 |
value="Camel-Doc-OCR-062825"
|
| 351 |
)
|
| 352 |
-
|
| 353 |
-
label="Upload Image",
|
| 354 |
-
file_types=[".jpg", ".jpeg", ".png", ".bmp", ".tiff"],
|
| 355 |
-
type="filepath"
|
| 356 |
-
)
|
| 357 |
-
image_preview = gr.Image(label="Preview", type="pil", interactive=False, height=300)
|
| 358 |
with gr.Accordion("Advanced Settings", open=False):
|
| 359 |
-
max_new_tokens = gr.Slider(minimum=1000, maximum=
|
| 360 |
-
|
| 361 |
-
max_pixels = gr.Number(value=MAX_PIXELS, label="Max Pixels")
|
| 362 |
process_btn = gr.Button("π Process Document", variant="primary", elem_classes=["process-button"], size="lg")
|
| 363 |
clear_btn = gr.Button("ποΈ Clear All", variant="secondary")
|
|
|
|
|
|
|
| 364 |
with gr.Column(scale=2):
|
| 365 |
with gr.Tabs():
|
| 366 |
with gr.Tab("π Extracted Content"):
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
| 368 |
with gr.Tab("π Layout Analysis Results"):
|
| 369 |
-
json_output = gr.JSON(label="Layout
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
for raw_buffer, layout_result in process_image(model_name, image, min_pixels=int(min_pix) if min_pix else None, max_pixels=int(max_pix) if max_pix else None, max_new_tokens=max_tokens):
|
| 378 |
-
yield raw_buffer, layout_result
|
| 379 |
-
except Exception as e:
|
| 380 |
-
error_msg = f"Error processing document: {str(e)}"
|
| 381 |
-
print(error_msg)
|
| 382 |
-
traceback.print_exc()
|
| 383 |
-
yield error_msg, None
|
| 384 |
-
def handle_file_upload(file_path):
|
| 385 |
-
if not file_path:
|
| 386 |
-
return None
|
| 387 |
-
image, status = load_file_for_preview(file_path)
|
| 388 |
-
return image
|
| 389 |
-
def clear_all():
|
| 390 |
-
return None, None, "", None
|
| 391 |
-
file_input.change(handle_file_upload, inputs=[file_input], outputs=[image_preview])
|
| 392 |
process_btn.click(
|
| 393 |
-
|
| 394 |
-
inputs=[model_choice,
|
| 395 |
-
outputs=[
|
| 396 |
)
|
|
|
|
|
|
|
| 397 |
clear_btn.click(
|
| 398 |
-
|
| 399 |
-
outputs=[
|
| 400 |
)
|
|
|
|
| 401 |
return demo
|
| 402 |
|
| 403 |
if __name__ == "__main__":
|
| 404 |
demo = create_gradio_interface()
|
| 405 |
-
demo.queue(
|
|
|
|
| 18 |
AutoProcessor,
|
| 19 |
TextIteratorStreamer,
|
| 20 |
)
|
|
|
|
| 21 |
|
| 22 |
+
# --- Constants and Model Setup ---
|
| 23 |
+
MAX_INPUT_TOKEN_LENGTH = 4096
|
|
|
|
|
|
|
|
|
|
| 24 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
|
| 26 |
+
# The detailed prompt to instruct the model to generate structured JSON
|
| 27 |
prompt = """Please output the layout information from the image, including each layout element's bbox, its category, and the corresponding text content within the bbox.
|
| 28 |
|
| 29 |
1. Bbox format: [x1, y1, x2, y2]
|
|
|
|
| 30 |
2. Layout Categories: The possible categories are ['Caption', 'Footnote', 'Formula', 'List-item', 'Page-footer', 'Page-header', 'Picture', 'Section-header', 'Table', 'Text', 'Title'].
|
|
|
|
| 31 |
3. Text Extraction & Formatting Rules:
|
| 32 |
- Picture: For the 'Picture' category, the text field should be omitted.
|
| 33 |
- Formula: Format its text as LaTeX.
|
| 34 |
+
- Table: For tables, provide the content in a structured format within the JSON.
|
| 35 |
- All Others (Text, Title, etc.): Format their text as Markdown.
|
|
|
|
| 36 |
4. Constraints:
|
| 37 |
- The output text must be the original text from the image, with no translation.
|
| 38 |
- All layout elements must be sorted according to human reading order.
|
| 39 |
+
5. Final Output: The entire output must be a single JSON object wrapped in ```json ... ```.
|
|
|
|
| 40 |
"""
|
| 41 |
|
| 42 |
# Load models
|
| 43 |
MODEL_ID_M = "prithivMLmods/Camel-Doc-OCR-062825"
|
| 44 |
processor_m = AutoProcessor.from_pretrained(MODEL_ID_M, trust_remote_code=True)
|
| 45 |
model_m = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 46 |
+
MODEL_ID_M, trust_remote_code=True, torch_dtype=torch.float16
|
|
|
|
|
|
|
| 47 |
).to(device).eval()
|
| 48 |
|
| 49 |
MODEL_ID_T = "prithivMLmods/Megalodon-OCR-Sync-0713"
|
| 50 |
processor_t = AutoProcessor.from_pretrained(MODEL_ID_T, trust_remote_code=True)
|
| 51 |
model_t = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 52 |
+
MODEL_ID_T, trust_remote_code=True, torch_dtype=torch.float16
|
|
|
|
|
|
|
| 53 |
).to(device).eval()
|
| 54 |
|
| 55 |
MODEL_ID_C = "nanonets/Nanonets-OCR-s"
|
| 56 |
processor_c = AutoProcessor.from_pretrained(MODEL_ID_C, trust_remote_code=True)
|
| 57 |
model_c = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 58 |
+
MODEL_ID_C, trust_remote_code=True, torch_dtype=torch.float16
|
|
|
|
|
|
|
| 59 |
).to(device).eval()
|
| 60 |
|
| 61 |
MODEL_ID_G = "echo840/MonkeyOCR"
|
| 62 |
SUBFOLDER = "Recognition"
|
| 63 |
processor_g = AutoProcessor.from_pretrained(
|
| 64 |
+
MODEL_ID_G, trust_remote_code=True, subfolder=SUBFOLDER
|
|
|
|
|
|
|
| 65 |
)
|
| 66 |
model_g = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 67 |
+
MODEL_ID_G, trust_remote_code=True, subfolder=SUBFOLDER, torch_dtype=torch.float16
|
|
|
|
|
|
|
|
|
|
| 68 |
).to(device).eval()
|
| 69 |
|
| 70 |
+
# --- Utility Functions ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
def layoutjson2md(layout_data: List[Dict]) -> str:
|
| 73 |
+
"""Converts the structured JSON layout data into formatted Markdown."""
|
|
|
|
| 74 |
markdown_lines = []
|
| 75 |
try:
|
| 76 |
sorted_items = sorted(layout_data, key=lambda x: (x.get('bbox', [0, 0, 0, 0])[1], x.get('bbox', [0, 0, 0, 0])[0]))
|
| 77 |
for item in sorted_items:
|
| 78 |
category = item.get('category', '')
|
| 79 |
+
text = item.get('text', '')
|
| 80 |
+
|
| 81 |
+
if not text:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
continue
|
| 83 |
+
|
| 84 |
+
if category == 'Title':
|
| 85 |
markdown_lines.append(f"# {text}\n")
|
| 86 |
elif category == 'Section-header':
|
| 87 |
markdown_lines.append(f"## {text}\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
elif category == 'Table':
|
| 89 |
+
# Check if the text is a dictionary representing a structured table
|
| 90 |
+
if isinstance(text, dict) and 'header' in text and 'rows' in text:
|
| 91 |
+
header = '| ' + ' | '.join(map(str, text['header'])) + ' |'
|
| 92 |
+
separator = '| ' + ' | '.join(['---'] * len(text['header'])) + ' |'
|
| 93 |
+
rows = ['| ' + ' | '.join(map(str, row)) + ' |' for row in text['rows']]
|
| 94 |
+
markdown_lines.append(header)
|
| 95 |
+
markdown_lines.append(separator)
|
| 96 |
+
markdown_lines.extend(rows)
|
| 97 |
+
markdown_lines.append("\n")
|
| 98 |
else:
|
| 99 |
+
# Fallback for unstructured table text
|
| 100 |
+
markdown_lines.append(f"{text}\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
else:
|
| 102 |
markdown_lines.append(f"{text}\n")
|
| 103 |
+
|
| 104 |
except Exception as e:
|
| 105 |
print(f"Error converting to markdown: {e}")
|
| 106 |
+
return "### Error converting JSON to Markdown."
|
| 107 |
return "\n".join(markdown_lines)
|
| 108 |
|
| 109 |
+
# --- Core Application Logic ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
@spaces.GPU
|
| 112 |
+
def process_document_stream(model_name: str, image: Image.Image, text_prompt: str, max_new_tokens: int):
|
| 113 |
+
"""
|
| 114 |
+
Main generator function that streams raw model output and then processes it into
|
| 115 |
+
formatted Markdown and structured JSON for the UI.
|
| 116 |
+
"""
|
| 117 |
+
if image is None:
|
| 118 |
+
yield "Please upload an image.", "Please upload an image.", None
|
| 119 |
+
return
|
| 120 |
+
|
| 121 |
+
# Select the model and processor
|
| 122 |
+
if model_name == "Camel-Doc-OCR-062825": processor, model = processor_m, model_m
|
| 123 |
+
elif model_name == "Megalodon-OCR-Sync-0713": processor, model = processor_t, model_t
|
| 124 |
+
elif model_name == "Nanonets-OCR-s": processor, model = processor_c, model_c
|
| 125 |
+
elif model_name == "MonkeyOCR-Recognition": processor, model = processor_g, model_g
|
| 126 |
+
else:
|
| 127 |
+
yield "Invalid model selected.", "Invalid model selected.", None
|
| 128 |
+
return
|
| 129 |
+
|
| 130 |
+
# Prepare model inputs
|
| 131 |
+
messages = [{"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": text_prompt}]}]
|
| 132 |
+
prompt_full = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 133 |
+
inputs = processor(text=[prompt_full], images=[image], return_tensors="pt", padding=True, truncation=True, max_length=MAX_INPUT_TOKEN_LENGTH).to(device)
|
| 134 |
+
|
| 135 |
+
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
| 136 |
+
generation_kwargs = {**inputs, "streamer": streamer, "max_new_tokens": max_new_tokens}
|
| 137 |
+
|
| 138 |
+
# Start generation in a separate thread
|
| 139 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 140 |
+
thread.start()
|
| 141 |
+
|
| 142 |
+
# Stream raw output to the UI
|
| 143 |
+
buffer = ""
|
| 144 |
+
for new_text in streamer:
|
| 145 |
+
buffer += new_text
|
| 146 |
+
buffer = buffer.replace("<|im_end|>", "")
|
| 147 |
+
time.sleep(0.01)
|
| 148 |
+
# Yield the raw stream and placeholders for the final results
|
| 149 |
+
yield buffer, "β³ Formatting Markdown...", {"status": "processing"}
|
| 150 |
+
|
| 151 |
+
# After streaming is complete, process the final buffer
|
| 152 |
try:
|
| 153 |
+
# Extract the JSON object from the buffer
|
| 154 |
+
json_match = re.search(r'```json\s*([\s\S]+?)\s*```', buffer)
|
| 155 |
+
if not json_match:
|
| 156 |
+
raise json.JSONDecodeError("JSON object not found in the model's output.", buffer, 0)
|
| 157 |
+
|
| 158 |
+
json_str = json_match.group(1)
|
| 159 |
+
layout_data = json.loads(json_str)
|
| 160 |
+
|
| 161 |
+
# Convert the parsed JSON to formatted markdown
|
| 162 |
+
markdown_content = layoutjson2md(layout_data)
|
| 163 |
+
|
| 164 |
+
# Yield the final, complete results
|
| 165 |
+
yield buffer, markdown_content, layout_data
|
| 166 |
+
|
| 167 |
+
except json.JSONDecodeError as e:
|
| 168 |
+
print(f"JSON parsing failed: {e}")
|
| 169 |
+
error_md = f"β **Error:** Failed to parse JSON from the model's output.\n\nSee the raw output stream for details."
|
| 170 |
+
error_json = {"error": "JSONDecodeError", "details": str(e), "raw_output": buffer}
|
| 171 |
+
yield buffer, error_md, error_json
|
| 172 |
except Exception as e:
|
| 173 |
+
print(f"An unexpected error occurred: {e}")
|
| 174 |
+
yield buffer, f"β An unexpected error occurred: {e}", None
|
|
|
|
| 175 |
|
| 176 |
+
|
| 177 |
+
# --- Gradio UI Definition ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
def create_gradio_interface():
|
| 180 |
+
"""Builds and returns the Gradio web interface."""
|
| 181 |
css = """
|
| 182 |
.main-container { max-width: 1400px; margin: 0 auto; }
|
| 183 |
+
.process-button { border: none !important; color: white !important; font-weight: bold !important; background-color: blue !important;}
|
| 184 |
+
.process-button:hover { background-color: darkblue !important; transform: translateY(-2px) !important; box-shadow: 0 4px 8px rgba(0,0,0,0.2) !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
"""
|
| 186 |
with gr.Blocks(theme="bethecloud/storj_theme", css=css) as demo:
|
| 187 |
gr.HTML("""
|
| 188 |
<div class="title" style="text-align: center">
|
| 189 |
<h1>Dot<span style="color: red;">β</span><strong></strong>OCR Comparator</h1>
|
| 190 |
<p style="font-size: 1.1em; color: #6b7280; margin-bottom: 0.6em;">
|
| 191 |
+
Advanced Vision-Language Model for Image Layout Analysis
|
| 192 |
</p>
|
| 193 |
</div>
|
| 194 |
""")
|
| 195 |
+
|
| 196 |
with gr.Row():
|
| 197 |
+
# --- Left Column (Inputs) ---
|
| 198 |
with gr.Column(scale=1):
|
| 199 |
model_choice = gr.Radio(
|
| 200 |
choices=["Camel-Doc-OCR-062825", "MonkeyOCR-Recognition", "Nanonets-OCR-s", "Megalodon-OCR-Sync-0713"],
|
| 201 |
label="Select Model",
|
| 202 |
value="Camel-Doc-OCR-062825"
|
| 203 |
)
|
| 204 |
+
image_input = gr.Image(label="Upload Image", type="pil", sources=['upload'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
with gr.Accordion("Advanced Settings", open=False):
|
| 206 |
+
max_new_tokens = gr.Slider(minimum=1000, maximum=8192, value=4096, step=256, label="Max New Tokens")
|
| 207 |
+
|
|
|
|
| 208 |
process_btn = gr.Button("π Process Document", variant="primary", elem_classes=["process-button"], size="lg")
|
| 209 |
clear_btn = gr.Button("ποΈ Clear All", variant="secondary")
|
| 210 |
+
|
| 211 |
+
# --- Right Column (Outputs) ---
|
| 212 |
with gr.Column(scale=2):
|
| 213 |
with gr.Tabs():
|
| 214 |
with gr.Tab("π Extracted Content"):
|
| 215 |
+
raw_output_stream = gr.Textbox(label="Raw Model Output Stream", interactive=False, lines=15, show_copy_button=True)
|
| 216 |
+
with gr.Accordion("(Formatted Result)", open=True):
|
| 217 |
+
markdown_output = gr.Markdown(label="Formatted Markdown (from JSON)")
|
| 218 |
+
|
| 219 |
with gr.Tab("π Layout Analysis Results"):
|
| 220 |
+
json_output = gr.JSON(label="Structured Layout Data (JSON)", value=None)
|
| 221 |
+
|
| 222 |
+
# --- Event Handlers ---
|
| 223 |
+
def clear_all_outputs():
|
| 224 |
+
"""Resets all input and output fields to their default state."""
|
| 225 |
+
return None, "Raw output will appear here.", "Formatted results will appear here.", None
|
| 226 |
+
|
| 227 |
+
# Connect the process button to the main generator function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
process_btn.click(
|
| 229 |
+
fn=process_document_stream,
|
| 230 |
+
inputs=[model_choice, image_input, gr.Textbox(value=prompt, visible=False), max_new_tokens],
|
| 231 |
+
outputs=[raw_output_stream, markdown_output, json_output]
|
| 232 |
)
|
| 233 |
+
|
| 234 |
+
# Connect the clear button
|
| 235 |
clear_btn.click(
|
| 236 |
+
clear_all_outputs,
|
| 237 |
+
outputs=[image_input, raw_output_stream, markdown_output, json_output]
|
| 238 |
)
|
| 239 |
+
|
| 240 |
return demo
|
| 241 |
|
| 242 |
if __name__ == "__main__":
|
| 243 |
demo = create_gradio_interface()
|
| 244 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, show_error=True)
|