OpenOCR-MLT / app.py
ericleyu's picture
Upload 2 files
f3c0474 verified
Raw History Blame Contribute Delete
11.2 kB
"""OpenOCR-MLT interactive demo using the published Python package."""
import logging
import os
from functools import cache
from pathlib import Path
from threading import RLock
from time import perf_counter
# Match the spaces SDK's accepted values and keep it optional for local CPU use.
ZERO_GPU = os.environ.get("SPACES_ZERO_GPU", "").lower() in {"1", "t", "true"}
if ZERO_GPU:
import spaces
import gradio as gr
from PIL import ImageDraw, ImageFont
from openocr_mlt import OpenOCRMLT
ROOT = Path(__file__).resolve().parent
DEVICE = os.environ.get("OPENOCR_MLT_DEVICE", "cuda:0" if ZERO_GPU else None)
CACHE_DIR = os.environ.get("OPENOCR_MLT_CACHE_DIR")
MODEL_REPOSITORY = "ericleyu/openocr-mlt-models"
MODEL_REVISION = "7ef39160a893fc7e605e146c1544ba64c1a57f43"
MODEL_ARCHIVE = "scriptmoe-medium-fp32-20261101-v1.tar.gz"
# Display English labels while keeping the existing /predict task values.
TASK_CHOICES = [
("Whole-image OCR", "Whole-image OCR / 整图识别"),
("Text detection", "Text detection / 文字检测"),
("Cropped recognition", "Cropped text / 裁剪识别"),
]
TASKS = {value: mode for (_, value), mode in zip(TASK_CHOICES, ("ocr", "det", "rec"))}
LOCK = RLock()
LOGGER = logging.getLogger("openocr-mlt-space")
HEADER = """
<header class="app-header">
<h1>OpenOCR-MLT</h1>
<p class="tagline">A visual demo for the <a href="https://pypi.org/project/openocr-mlt/">openocr-mlt</a>
repository, developed by the <strong>FVL OpenOCR team</strong>.</p>
<p class="script-line">
<strong>Multilingual text recognition</strong>
<span>Latin</span><span>Chinese</span><span>Japanese</span><span>Korean</span><span>Arabic</span>
<span>Bangla</span><span>Devanagari</span><span>Cyrillic</span><span>Thai</span><span>Tibetan</span>
</p>
</header>
"""
# Gradio 6.31 loads built-in controls in the browser locale before running head scripts.
# Switch that locale through the same frontend function used by Gradio's settings.
ENGLISH_LOCALE = r"""
<script type="module">
const core = performance.getEntriesByType("resource").find(({ name }) => {
const url = new URL(name);
return url.origin === location.origin && /\/assets\/core-[\w-]+\.js$/.test(url.pathname);
});
if (core) {
const { changeLocale } = await import(core.name);
changeLocale("en");
}
</script>
"""
CSS = """
.app-header {
text-align: center;
max-width: 1200px;
margin: 20px auto;
padding: 24px 20px;
}
.app-header h1 {
font-size: clamp(2rem, 5vw, 2.4rem);
font-weight: 700;
letter-spacing: -.01em;
margin: 0 0 10px;
}
.app-header .tagline {
font-size: 1.1em;
line-height: 1.6;
margin: 0 0 14px;
}
.app-header .script-line {
font-size: .95em;
line-height: 1.7;
margin: 0;
}
.script-line strong { margin-right: 5px; }
.script-line span {
display: inline-block;
background: color-mix(in srgb, currentColor 8%, transparent);
border-radius: 999px;
margin: 2px 3px;
padding: 2px 10px;
font-size: .88em;
}
.image-container img { object-fit: contain; }
"""
EXAMPLE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".bmp", ".tif", ".tiff", ".webp"}
def example_images(group):
folder = ROOT / "examples" / group
return [
str(path) for path in sorted(folder.glob("*"))
if path.is_file() and path.suffix.lower() in EXAMPLE_EXTENSIONS
]
@cache
def get_engine(task):
options = {}
if task == "ocr":
options = {"page_max_side": 2000, "rec_threshold": 0}
elif task == "rec":
options = {"max_side": 2000, "rec_threshold": 0}
return OpenOCRMLT(task=task, device=DEVICE, cache_dir=CACHE_DIR, offline=ZERO_GPU, **options)
def draw_boxes(image, boxes):
canvas = image.copy()
draw = ImageDraw.Draw(canvas)
font = ImageFont.load_default(size=16)
width = max(2, round(max(canvas.size) / 600))
for index, points in enumerate(boxes, 1):
polygon = [tuple(point) for point in points]
draw.line(polygon + [polygon[0]], fill="#059669", width=width)
x = min(max(int(p[0]) for p in polygon), canvas.width - 1)
y = min(max(0, min(int(p[1]) for p in polygon) - 22), canvas.height - 1)
label = str(index)
left, top, right, bottom = draw.textbbox((x, y), label, font=font)
draw.rectangle((left - 2, top - 2, right + 3, bottom + 2), fill="#059669")
draw.text((x, y), label, font=font, fill="white")
return canvas
def run_ocr(image, task_label, rec_threshold=0.5):
if image is None:
raise gr.Error("Upload an image or select an example.")
task = TASKS.get(task_label)
if task is None:
raise gr.Error("Select an OCR task.")
if image.width * image.height > 16_000_000:
raise gr.Error("Please use an image with at most 16 megapixels.")
if isinstance(rec_threshold, bool) or not isinstance(rec_threshold, (int, float)) or not 0 <= rec_threshold <= 1:
raise gr.Error("Recognition confidence must be between 0 and 1.")
threshold = float(rec_threshold)
started = perf_counter()
try:
with LOCK:
engine = get_engine(task)
prediction = engine(image=image)
device = engine.info["device"]
except (OSError, RuntimeError, ValueError) as error:
LOGGER.exception("OCR request failed for task=%s", task)
raise gr.Error("Processing failed. Please retry or try another image.") from error
elapsed = perf_counter() - started
records = [item.to_dict() for item in (prediction.items if task == "ocr" else prediction)]
score_key = "rec_score" if task == "ocr" else "score"
table = []
for index, row in enumerate(records, 1):
score = row[score_key]
# Apply the slider to the display without recreating the model session.
if row.get("text") and score < threshold:
row.update(text="", status="filtered")
table.append([index, row.get("text", ""), score, row.get("status", "detected")])
text = "\n".join(row["text"] for row in records if row.get("text"))
boxes = [row["points"] for row in records if "points" in row]
payload = {
"task": task, "image_size": list(image.size), "device": device, "results": records,
"recognition_threshold": None if task == "det" else threshold,
}
count = len(records)
unit = "prediction" if task == "rec" else "region"
status = f"{elapsed:.2f} s · {device.upper()} · {count} {unit}{'s' if count != 1 else ''}"
if not records:
status += " · No text found"
elif task != "det" and not text:
status += f" · No text passed confidence {threshold:g}"
return draw_boxes(image, boxes), text, table, status, payload
# ONNX Runtime opens its CUDA sessions on the first inference in the GPU worker.
predict = spaces.GPU(duration=60)(run_ocr) if ZERO_GPU else run_ocr
def create_demo():
cropped_examples = example_images("recognition")
with gr.Blocks(title="OpenOCR-MLT") as demo:
gr.HTML(HEADER)
with gr.Row():
with gr.Column():
image = gr.Image(
label="Input image", type="pil", sources=["upload"],
elem_classes=["image-container"], height=380,
)
task = gr.Radio(
choices=TASK_CHOICES, value=TASK_CHOICES[0][1], label="Task",
)
with gr.Column() as page_gallery:
gr.Examples(
examples=example_images("page"), inputs=image,
label="Examples for whole-image OCR and text detection", cache_examples=False,
)
with gr.Column(visible=False) as crop_gallery:
if cropped_examples:
gr.Examples(
examples=cropped_examples, inputs=image,
label="Cropped recognition examples", cache_examples=False,
)
else:
gr.Markdown("Upload an image cropped to a single word or line of text.")
run = gr.Button("Run OCR", variant="primary")
rec_threshold = gr.Slider(
maximum=1, value=0.5, step=0.01,
label="Recognition confidence",
info="Minimum confidence for recognized text.",
)
with gr.Column():
visualization = gr.Image(
label="Visualization",
elem_classes=["image-container"], height=380,
)
status = gr.Textbox(label="Latency")
structured = gr.JSON(label="Result (JSON)", height=310)
with gr.Accordion("Recognized text and confidence scores", open=False):
text = gr.Textbox(label="Recognized text", lines=6, buttons=["copy"])
table = gr.Dataframe(
headers=["ID", "Text", "Confidence", "Status"],
datatype=["number", "str", "number", "str"],
wrap=True, label="Results",
)
def update_task(label):
mode = TASKS.get(label)
return (
gr.update(visible=mode != "rec"),
gr.update(visible=mode == "rec"),
gr.update(visible=mode != "det"),
gr.update(value={"det": "Run Detection", "rec": "Run Recognition"}.get(mode, "Run OCR")),
)
task.change(
update_task, inputs=task, outputs=[page_gallery, crop_gallery, rec_threshold, run],
queue=False, api_visibility="private",
)
run.click(
predict, inputs=[image, task, rec_threshold],
outputs=[visualization, text, table, status, structured],
api_name="predict", concurrency_limit=1,
)
gr.Markdown(
"[PyPI](https://pypi.org/project/openocr-mlt/) · "
"[Models](https://huggingface.co/ericleyu/openocr-mlt-models) · "
"[ScriptMoE](https://github.com/YesianRohn/ScriptMoE) · "
"[Reference demo and example images](https://huggingface.co/spaces/Yesianrohn/MultilingualOCR-Demo)"
)
return demo.queue(max_size=12)
def prepare_space_assets():
"""Verify and unpack the build-preloaded model archive without opening CUDA sessions."""
from huggingface_hub import hf_hub_download
from openocr_mlt import fetch_models
archive = hf_hub_download(
repo_id=MODEL_REPOSITORY,
filename=MODEL_ARCHIVE,
revision=MODEL_REVISION,
local_files_only=True,
)
fetch_models(source=archive, cache_dir=CACHE_DIR, offline=True)
if __name__ == "__main__":
if ZERO_GPU:
prepare_space_assets()
create_demo().launch(
server_name="0.0.0.0", max_file_size="20mb",
theme=gr.themes.Soft(), css=CSS, head=ENGLISH_LOCALE,
)