Spaces:
Running
on
Zero
Running
on
Zero
File size: 19,436 Bytes
f08abae e4442f3 f08abae e4442f3 864e5c4 58b56ea a44a287 e1b1045 003891a f55c2c2 003891a f55c2c2 003891a f55c2c2 003891a e1b1045 f08abae 5639776 2c499db 5f3165f 2c499db 5f3165f f08abae 5639776 2c499db 5639776 f08abae 5639776 2c499db 5639776 2c499db 5639776 2c499db 5639776 2c499db 5639776 2c499db 5639776 2c499db 5639776 2c499db f08abae 5639776 5f3165f f08abae 5639776 f08abae 5f3165f f08abae 2c499db f08abae 5f3165f 5639776 f08abae 5639776 82483a0 f08abae 5639776 5f3165f f08abae 5f3165f f08abae 5f3165f 898d181 5639776 2c499db 5639776 2c499db 5639776 2c499db 83e370e 5639776 2c499db 5639776 e1b1045 003891a f55c2c2 5639776 58b56ea 003891a 82483a0 e1b1045 003891a 5639776 58b56ea 003891a e4442f3 003891a e4442f3 5639776 e4442f3 f55c2c2 5639776 e4442f3 5639776 e4442f3 58b56ea 5639776 58b56ea 5639776 58b56ea e4442f3 5639776 58b56ea 5639776 e4442f3 58b56ea 5639776 e4442f3 58b56ea e4442f3 5639776 58b56ea e4442f3 58b56ea e4442f3 f08abae 5639776 f08abae 5639776 003891a 5f3165f 003891a 2c499db 5f3165f e4442f3 2c499db e4442f3 83e370e e4442f3 003891a f55c2c2 83e370e f55c2c2 83e370e e4442f3 5639776 003891a e4442f3 2c499db f55c2c2 83e370e f55c2c2 83e370e e4442f3 2c499db 5639776 e4442f3 5639776 e4442f3 5f3165f f55c2c2 5f3165f f08abae 5f3165f e07254e f55c2c2 f08abae 82483a0 f55c2c2 8225805 f08abae 5f3165f 82483a0 429439c 82483a0 f08abae 82483a0 5639776 5f3165f 82483a0 f08abae 82483a0 f08abae 82483a0 5639776 f08abae e4442f3 003891a f55c2c2 f08abae 5639776 f9f088f 82483a0 f9f088f 82483a0 f9f088f f08abae 82483a0 f08abae 82483a0 f08abae 82483a0 5f3165f f08abae e4442f3 82483a0 f08abae e4442f3 5639776 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
import gradio as gr
from PIL import Image
import xml.etree.ElementTree as ET
import os
import torch
from transformers import AutoProcessor, AutoModelForImageTextToText, pipeline
import spaces
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1" # turn on HF_TRANSFER
# --- Global Model and Processor ---
MODELS = {}
PROCESSORS = {}
PIPELINES = {}
MODEL_LOAD_ERROR_MSG = {}
# Available models
AVAILABLE_MODELS = ["RolmOCR", "Nanonets-OCR-s"]
# Load RolmOCR
try:
PROCESSORS["RolmOCR"] = AutoProcessor.from_pretrained("reducto/RolmOCR")
MODELS["RolmOCR"] = AutoModelForImageTextToText.from_pretrained(
"reducto/RolmOCR", torch_dtype=torch.bfloat16, device_map="auto"
)
PIPELINES["RolmOCR"] = pipeline(
"image-text-to-text", model=MODELS["RolmOCR"], processor=PROCESSORS["RolmOCR"]
)
except Exception as e:
MODEL_LOAD_ERROR_MSG["RolmOCR"] = f"Failed to load RolmOCR: {str(e)}"
print(f"Error loading RolmOCR: {e}")
# Load Nanonets-OCR-s
try:
PROCESSORS["Nanonets-OCR-s"] = AutoProcessor.from_pretrained(
"nanonets/Nanonets-OCR-s"
)
MODELS["Nanonets-OCR-s"] = AutoModelForImageTextToText.from_pretrained(
"nanonets/Nanonets-OCR-s", torch_dtype=torch.bfloat16, device_map="auto"
)
PIPELINES["Nanonets-OCR-s"] = pipeline(
"image-text-to-text",
model=MODELS["Nanonets-OCR-s"],
processor=PROCESSORS["Nanonets-OCR-s"],
)
except Exception as e:
MODEL_LOAD_ERROR_MSG["Nanonets-OCR-s"] = f"Failed to load Nanonets-OCR-s: {str(e)}"
print(f"Error loading Nanonets-OCR-s: {e}")
# --- Helper Functions ---
def get_xml_namespace(xml_file_path):
"""
Dynamically gets the namespace from the XML file.
Returns both the namespace and the format type (ALTO or PAGE).
"""
try:
tree = ET.parse(xml_file_path)
root = tree.getroot()
if "}" in root.tag:
ns = root.tag.split("}")[0] + "}"
# Determine format based on root element
if "PcGts" in root.tag:
return ns, "PAGE"
elif "alto" in root.tag.lower():
return ns, "ALTO"
except ET.ParseError:
print(f"Error parsing XML to find namespace: {xml_file_path}")
return "", "UNKNOWN"
def parse_page_xml_for_text(xml_file_path):
"""
Parses a PAGE XML file to extract text content.
Returns:
- full_text (str): All extracted text concatenated.
"""
full_text_lines = []
if not xml_file_path or not os.path.exists(xml_file_path):
return "Error: XML file not provided or does not exist."
try:
ns_prefix, _ = get_xml_namespace(xml_file_path)
tree = ET.parse(xml_file_path)
root = tree.getroot()
# Find all TextLine elements
for text_line in root.findall(f".//{ns_prefix}TextLine"):
# First try to get text from TextEquiv/Unicode
text_equiv = text_line.find(f"{ns_prefix}TextEquiv/{ns_prefix}Unicode")
if text_equiv is not None and text_equiv.text:
full_text_lines.append(text_equiv.text)
continue
# If no TextEquiv, try to get text from Word elements
line_text_parts = []
for word in text_line.findall(f"{ns_prefix}Word"):
word_text = word.find(f"{ns_prefix}TextEquiv/{ns_prefix}Unicode")
if word_text is not None and word_text.text:
line_text_parts.append(word_text.text)
if line_text_parts:
full_text_lines.append(" ".join(line_text_parts))
return "\n".join(full_text_lines)
except ET.ParseError as e:
return f"Error parsing XML: {e}"
except Exception as e:
return f"An unexpected error occurred during XML parsing: {e}"
def parse_alto_xml_for_text(xml_file_path):
"""
Parses an ALTO XML file to extract text content.
Returns:
- full_text (str): All extracted text concatenated.
"""
full_text_lines = []
if not xml_file_path or not os.path.exists(xml_file_path):
return "Error: XML file not provided or does not exist."
try:
ns_prefix, _ = get_xml_namespace(xml_file_path)
tree = ET.parse(xml_file_path)
root = tree.getroot()
for text_line in root.findall(f".//{ns_prefix}TextLine"):
line_text_parts = []
for string_element in text_line.findall(f"{ns_prefix}String"):
if text := string_element.get("CONTENT"):
line_text_parts.append(text)
if line_text_parts:
full_text_lines.append(" ".join(line_text_parts))
return "\n".join(full_text_lines)
except ET.ParseError as e:
return f"Error parsing XML: {e}"
except Exception as e:
return f"An unexpected error occurred during XML parsing: {e}"
def parse_xml_for_text(xml_file_path):
"""
Main function to parse XML files, automatically detecting the format.
"""
if not xml_file_path or not os.path.exists(xml_file_path):
return "Error: XML file not provided or does not exist."
try:
_, xml_format = get_xml_namespace(xml_file_path)
if xml_format == "PAGE":
return parse_page_xml_for_text(xml_file_path)
elif xml_format == "ALTO":
return parse_alto_xml_for_text(xml_file_path)
else:
return "Error: Unsupported XML format. Expected ALTO or PAGE XML."
except Exception as e:
return f"Error determining XML format: {str(e)}"
@spaces.GPU
def predict(pil_image, model_name="RolmOCR"):
"""Performs OCR prediction using the selected Hugging Face model."""
global PIPELINES, MODEL_LOAD_ERROR_MSG
if model_name not in PIPELINES:
error_to_report = MODEL_LOAD_ERROR_MSG.get(
model_name,
f"Model {model_name} could not be initialized or is not available.",
)
raise RuntimeError(error_to_report)
selected_pipe = PIPELINES[model_name]
# Format the message based on the model
if model_name == "RolmOCR":
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": pil_image},
{
"type": "text",
"text": "Return the plain text representation of this document as if you were reading it naturally.\n",
},
],
}
]
else: # Nanonets-OCR-s
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": pil_image},
{
"type": "text",
"text": "Extract and return all the text from this image. Include all text elements and maintain the reading order. If there are tables, convert them to markdown format. If there are mathematical equations, convert them to LaTeX format.",
},
],
}
]
max_tokens = 8096
# Use the pipeline with the properly formatted messages
return selected_pipe(messages, max_new_tokens=max_tokens)
def run_hf_ocr(image_path, model_name="RolmOCR"):
"""
Runs OCR on the provided image using the selected Hugging Face model (via predict function).
"""
if image_path is None:
return "No image provided for OCR."
try:
pil_image = Image.open(image_path).convert("RGB")
ocr_results = predict(
pil_image, model_name
) # predict handles model loading and inference
# Parse the output based on the user's example structure
if (
isinstance(ocr_results, list)
and ocr_results
and "generated_text" in ocr_results[0]
):
generated_content = ocr_results[0]["generated_text"]
if isinstance(generated_content, str):
return generated_content
if isinstance(generated_content, list) and generated_content:
if assistant_message := next(
(
msg["content"]
for msg in reversed(generated_content)
if isinstance(msg, dict)
and msg.get("role") == "assistant"
and "content" in msg
),
None,
):
return assistant_message
# Fallback if the specific assistant message structure isn't found but there's content
if (
isinstance(generated_content[0], dict)
and "content" in generated_content[0]
):
if (
len(generated_content) > 1
and isinstance(generated_content[1], dict)
and "content" in generated_content[1]
):
return generated_content[1][
"content"
] # Assuming second part is assistant
else:
return generated_content[0]["content"]
print(f"Unexpected OCR output structure from HF model: {ocr_results}")
return "Error: Could not parse OCR model output. Check console."
else:
print(f"Unexpected OCR output structure from HF model: {ocr_results}")
return "Error: OCR model did not return expected output. Check console."
except RuntimeError as e: # Catch model loading/initialization errors from predict
return str(e)
except Exception as e:
print(f"Error during Hugging Face OCR processing: {e}")
return f"Error during Hugging Face OCR: {str(e)}"
# --- Gradio Interface Function ---
def process_files(image_path, xml_path, model_name):
"""
Main function for the Gradio interface.
Processes the image for display, runs OCR with selected model,
and parses XML if provided.
"""
img_to_display = None
xml_text_output = "XML not provided or not processed."
hf_ocr_text_output = "Image not provided or OCR not run."
ocr_download = gr.DownloadButton(visible=False)
xml_download = gr.DownloadButton(visible=False)
if image_path:
try:
img_to_display = Image.open(image_path).convert("RGB")
hf_ocr_text_output = run_hf_ocr(image_path, model_name)
# Create download file for OCR output
if hf_ocr_text_output and not hf_ocr_text_output.startswith("Error"):
ocr_filename = f"vlm_ocr_output_{model_name}.txt"
with open(ocr_filename, "w", encoding="utf-8") as f:
f.write(hf_ocr_text_output)
ocr_download = gr.DownloadButton(
label="Download VLM OCR", value=ocr_filename, visible=True
)
except Exception as e:
img_to_display = None # Clear image if it failed to load
hf_ocr_text_output = f"Error loading image or running {model_name} OCR: {e}"
else:
hf_ocr_text_output = "Please upload an image to perform OCR."
if xml_path:
xml_text_output = parse_xml_for_text(xml_path)
# Create download file for XML text
if xml_text_output and not xml_text_output.startswith("Error"):
xml_filename = "traditional_ocr_output.txt"
with open(xml_filename, "w", encoding="utf-8") as f:
f.write(xml_text_output)
xml_download = gr.DownloadButton(
label="Download XML Text", value=xml_filename, visible=True
)
else:
xml_text_output = "No XML file uploaded."
# If only XML is provided without an image
if not image_path and xml_path:
img_to_display = None # No image to display
hf_ocr_text_output = "Upload an image to perform OCR."
return (
img_to_display,
xml_text_output,
hf_ocr_text_output,
ocr_download,
xml_download,
)
# --- Create Gradio App ---
with gr.Blocks() as demo:
gr.Markdown("# π°οΈ OCR Time Machine")
gr.Markdown(
"Travel through time to see how OCR technology has evolved! \n\n "
"For decades, galleries, libraries, archives, and museums (GLAMs) have used Optical Character Recognition "
"to transform digitized books, newspapers, and manuscripts into machine-readable text. Traditional OCR "
"produces complex XML formats like ALTO, packed with layout details but difficult to use. "
"Now, cutting-edge Vision-Language Models (VLMs) are revolutionizing OCR with simpler, cleaner Markdown output. "
"This Space makes it easy to compare these two approaches and see which works best for your historical documents. "
"Upload a historical document image and its XML file to compare these approaches side-by-side. "
"We'll extract the reading order from your XML for an apples-to-apples comparison of the actual text content.\n\n"
"**Available models:** [RolmOCR](https://huggingface.co/reducto/RolmOCR) | "
"[Nanonets-OCR-s](https://huggingface.co/nanonets/Nanonets-OCR-s)"
)
gr.Markdown("---")
# How it works section
gr.Markdown("## π How it works")
gr.Markdown(
"1. π€ **Upload Image**: Select a historical document image (JPG, PNG, JP2)\n"
"2. π **Upload XML** (Optional): Add the corresponding ALTO or PAGE XML file for comparison\n"
"3. π€ **Choose Model**: Select between RolmOCR (new) or Nanonets-OCR-s (even newer!)\n"
"4. π **Compare**: Click 'Compare OCR Methods' to process\n"
"5. πΎ **Download**: Save the results for further analysis"
)
gr.Markdown("---")
# Input section
gr.Markdown("## π₯ Upload Files")
with gr.Row():
with gr.Column(scale=1):
with gr.Group():
gr.Markdown("### π€ Step 1: Upload your document")
image_input = gr.File(
label="Historical Document Image",
type="filepath",
file_types=["image"],
)
xml_input = gr.File(
label="XML File (Optional - ALTO or PAGE format)",
type="filepath",
file_types=[".xml"],
)
with gr.Group():
gr.Markdown("### π€ Step 2: Select OCR Model")
model_selector = gr.Radio(
choices=AVAILABLE_MODELS,
value="RolmOCR",
label="Choose Model",
info="RolmOCR: Fast & general-purpose | Nanonets: Advanced with table/math support",
)
submit_button = gr.Button(
"π Compare OCR Methods", variant="primary", size="lg"
)
# Results section
gr.Markdown("## π Results")
with gr.Row():
with gr.Column(scale=1):
with gr.Group():
gr.Markdown("### πΌοΈ Document Image")
output_image_display = gr.Image(
label="Uploaded Document", type="pil", interactive=False
)
with gr.Column(scale=1):
with gr.Group():
gr.Markdown("### π€ Modern VLM OCR Output")
hf_ocr_output_textbox = gr.Markdown(
label="Markdown Format",
show_copy_button=True,
)
ocr_download_btn = gr.DownloadButton(
label="πΎ Download VLM OCR", visible=False, size="sm"
)
with gr.Group():
gr.Markdown("### π Traditional OCR Output")
xml_output_textbox = gr.Textbox(
label="XML Reading Order",
lines=10,
interactive=False,
show_copy_button=True,
)
xml_download_btn = gr.DownloadButton(
label="πΎ Download XML Text", visible=False, size="sm"
)
submit_button.click(
fn=process_files,
inputs=[image_input, xml_input, model_selector],
outputs=[
output_image_display,
xml_output_textbox,
hf_ocr_output_textbox,
ocr_download_btn,
xml_download_btn,
],
)
gr.Markdown("---")
# Examples section
with gr.Group():
gr.Markdown("## π― Try an Example")
gr.Examples(
examples=[
[
"examples/one/74442232.3.jpg",
"examples/one/74442232.34.xml",
"RolmOCR",
],
[
"examples/one/74442232.3.jpg",
"examples/one/74442232.34.xml",
"Nanonets-OCR-s",
],
],
inputs=[image_input, xml_input, model_selector],
outputs=[
output_image_display,
xml_output_textbox,
hf_ocr_output_textbox,
ocr_download_btn,
xml_download_btn,
],
fn=process_files,
cache_examples=False,
)
gr.Markdown(
"*Example from ['A Medical History of British India'](https://data.nls.uk/data/digitised-collections/a-medical-history-of-british-india/) "
"collection, National Library of Scotland*"
)
gr.Markdown("---")
# Tips section
with gr.Accordion("π‘ Tips & Information", open=False):
gr.Markdown(
"### π About ALTO/PAGE XML\n"
"- **ALTO** (Analyzed Layout and Text Object) and **PAGE** are XML formats that store OCR results with detailed layout information\n"
"- These files are typically generated by traditional OCR software and include position data for each text element\n"
"- This tool extracts just the reading order text for easier comparison\n\n"
"### π― Best Practices\n"
"- Use high-resolution scans (300+ DPI) for best results\n"
"- Historical documents with clear text work best\n"
"- The VLM models can handle complex layouts, tables, and mathematical notation\n\n"
)
gr.Code(
value=(
"""<alto xmlns="http://www.loc.gov/standards/alto/v3/alto.xsd">
<Layout>
<Page>
<PrintSpace>
<TextLine>
<String CONTENT="Hello World"/>
</TextLine>
</PrintSpace>
</Page>
</Layout>
</alto>"""
),
interactive=False,
)
# Footer
gr.Markdown("---")
gr.Markdown(
"<center>\n\n"
"Built with β€οΈ for the GLAM community | "
"[Learn more about OCR formats](https://www.loc.gov/standards/alto/) | "
"Questions? [Open an issue](https://github.com/davanstrien/ocr-playground/issues)\n\n"
"</center>"
)
if __name__ == "__main__":
print("Attempting to launch Gradio demo...")
print(
"If the Hugging Face model is large, initial startup might take some time due to model download/loading (on first OCR attempt)."
)
demo.launch()
|