File size: 13,128 Bytes
18115ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""LightOnOCR-3 demo: drop a page, pick a model and a mode, watch the transcription stream in.

The interface is one custom component (ui/): a sidebar for the document, the model, the mode and
the examples, and a stage with the page on the left, every layout block drawn on it, and the text on
the right, block by block, hover and click synced between the two. Gradio is the backend: the
component sends custom events (open a file, change page, run, stop) and receives a JSON value,
streamed while the model writes, so the boxes appear while the page is being read.

Files: ocr.py (the variants and the two backends), grounding.py (the output format), render.py
(PDF or image to page image), ui/ (the interface: template, CSS, JS).
"""

from __future__ import annotations

import hashlib
import io
import time
from pathlib import Path

import gradio as gr
from PIL import Image

import ocr
from grounding import parse_blocks, strip_thinking, to_markdown
from render import DEFAULT_LONGEST_EDGE, page_count, render_page

ROOT = Path(__file__).parent
UI = ROOT / "ui"
UPLOADS = Path(gr.utils.get_upload_folder()).resolve()  # where the component's upload() puts files
CACHE = UPLOADS / "lightonocr3"  # page images for the viewer, served by Gradio like the uploads

COLLECTION = "https://huggingface.co/collections/lightonai/lightonocr-3-6ac64536c01ee676d9bf46a7"
PAPER = "https://arxiv.org/abs/2601.14251"
BLOG = "https://huggingface.co/blog/lightonai/lightonocr-3"
WEBSITE = "https://lighton.ai/"

# Click a thumbnail to run that page with the current settings: (file, caption).
EXAMPLES = [
    ("examples/paper-page-3.png", "LightOnOCR paper, page 3"),
    ("examples/manual-figure.png", "Technical manual with a figure"),
    ("examples/annual-report-table.png", "Annual report, dense table"),
    ("examples/docs-table.png", "Documentation page"),
    ("examples/arxiv-two-columns.png", "Two-column paper"),
    ("examples/thesis-math.png", "Thesis page, equations"),
    ("examples/handwritten-math.png", "Handwritten notes, math"),
    ("examples/handwritten-diagrams.png", "Handwritten notes, diagrams"),
    ("examples/lecture-notes.png", "Lecture notes, highlighted"),
    ("examples/social-post.png", "Social media post"),
    ("examples/telegram.jpg", "1906 telegram, photo"),
    ("examples/library-card.jpg", "Library card, photo"),
]
EXAMPLES = [e for e in EXAMPLES if (ROOT / e[0]).is_file()]
gr.set_static_paths(paths=[ROOT / "examples"])  # thumbnails are plain <img src="gradio_api/file=examples/..."> tags


def thumb(path: str) -> str:
    small = ROOT / "examples" / "thumbs" / (Path(path).stem + ".jpg")
    return f"gradio_api/file={small.relative_to(ROOT)}" if small.is_file() else f"gradio_api/file={path}"


# ---- The component's value -----------------------------------------------------------------------------


def cache_image(image: Image.Image) -> str:
    """Save the page as a JPEG in Gradio's cache and return its URL. Sent once per page, not with every stream update."""
    CACHE.mkdir(parents=True, exist_ok=True)
    buf = io.BytesIO()
    image.save(buf, "JPEG", quality=88)
    path = CACHE / (hashlib.sha1(buf.getvalue()).hexdigest()[:20] + ".jpg")
    if not path.exists():
        path.write_bytes(buf.getvalue())
    return f"gradio_api/file={path}"


def page_name(doc: dict) -> str:
    stem = Path(doc.get("name") or doc.get("path") or "page").stem
    return f"{stem}-page-{doc['page']}" if doc.get("pages", 1) > 1 else stem


def state_value(doc: dict | None = None, image: Image.Image | None = None, image_url: str | None = None, mode: str = "grounding",
                raw: str = "", status: str = "idle", message: str = "", model: str | None = None, example: int | None = None) -> dict:
    """What ui/app.js renders: the document, its page, the parsed blocks (grounding) and the Markdown, plus a status line."""
    doc = doc or {}
    value = {"path": doc.get("path"), "name": doc.get("name"), "pages": doc.get("pages", 1), "page": doc.get("page", 1),
             "image": image_url, "width": image.width if image else None, "height": image.height if image else None,
             "mode": mode, "model": model, "raw": raw, "markdown": raw, "blocks": [], "status": status, "message": message,
             "filename": page_name(doc) if doc else "page", "example": example}
    if mode == "grounding":
        blocks = parse_blocks(raw)
        value["blocks"] = [b.to_dict() for b in blocks]
        value["markdown"] = to_markdown(blocks)
    return value


EMPTY = state_value()


def resolve(path: str | None) -> Path:
    """A path the component may ask for: something it uploaded, or an example. Anything else is refused."""
    p = Path(str(path or "")).resolve()
    if not p.is_file() or not (p.is_relative_to(UPLOADS) or p.is_relative_to((ROOT / "examples").resolve())):
        raise gr.Error("Please upload the file again.")
    return p


def open_doc(path: str | None, name: str | None, page: int, longest_edge: int) -> tuple[dict, Image.Image]:
    p = resolve(path)
    pages = page_count(p)
    page = min(max(int(page or 1), 1), pages)
    image = render_page(p, page, int(longest_edge or DEFAULT_LONGEST_EDGE))
    return {"path": str(p), "name": name or p.name, "pages": pages, "page": page}, image


def ready(doc: dict, image: Image.Image, mode: str, example: int | None = None) -> dict:
    where = f"page {doc['page']} of {doc['pages']} · " if doc["pages"] > 1 else ""
    return state_value(doc, image, cache_image(image), mode, status="ready", example=example,
                       message=f"{doc['name']} · {where}{image.width}×{image.height} px · ready")


# ---- Events from the component ---------------------------------------------------------------------------


def data(evt: gr.EventData) -> dict:
    return evt._data if isinstance(evt._data, dict) else {}


def on_open(evt: gr.EventData):
    d = data(evt)
    doc, image = open_doc(d.get("path"), d.get("name"), 1, d.get("longest_edge"))
    return ready(doc, image, d.get("mode") or "grounding")


def on_page(evt: gr.EventData):
    d = data(evt)
    doc, image = open_doc(d.get("path"), None, d.get("page", 1), d.get("longest_edge"))
    return ready(doc, image, d.get("mode") or "grounding")


def on_run(evt: gr.EventData):
    """Render the page, stream the model over it, and yield the value as the text comes in."""
    d = data(evt)
    mode = d.get("mode") if d.get("mode") in ocr.MODES else "grounding"
    model_key = d.get("model") if d.get("model") in ocr.ENABLED else ocr.DEFAULT_VARIANT
    temperature = min(max(float(d.get("temperature", 0.2)), 0.0), 1.0)
    max_tokens = int(d.get("max_tokens", ocr.DEFAULT_MAX_TOKENS))
    longest_edge = int(d.get("longest_edge", DEFAULT_LONGEST_EDGE))
    example = d.get("example")
    if example is not None:
        example = int(example)
        if not 0 <= example < len(EXAMPLES):
            raise gr.Error("Unknown example.")
        path, name = str(ROOT / EXAMPLES[example][0]), EXAMPLES[example][1]
        doc, image = open_doc(path, name, 1, longest_edge)
    else:
        doc, image = open_doc(d.get("path"), d.get("name"), d.get("page", 1), longest_edge)
    variant = ocr.VARIANTS[model_key]
    shown = dict(doc=doc, image=image, image_url=cache_image(image), mode=mode, model=variant.name, example=example)
    size = f"{image.width}×{image.height} px"
    yield state_value(**shown, status="running", message=f"{variant.name} · {size} · waiting for the first token…")

    started = time.monotonic()
    first_token: float | None = None
    last_shown = 0.0
    text = ""
    try:
        for update in ocr.stream(model_key, image, mode, temperature, max_tokens):
            now = time.monotonic()
            text = strip_thinking(update.text)
            if update.error:
                yield state_value(**shown, raw=text, status="error", message=update.error)
                return
            if text and first_token is None:
                first_token = now - started
            if update.done:
                elapsed = now - started
                value = state_value(**shown, raw=text, status="done")
                parts = [variant.name, ocr.backend_name(model_key), size]
                if mode == "grounding":
                    parts.append(f"{len(value['blocks'])} block{'s' if len(value['blocks']) != 1 else ''}")
                if update.tokens:
                    parts.append(f"{update.tokens:,} tokens")
                    if first_token is not None and elapsed - first_token > 0.5:
                        parts.append(f"{update.tokens / (elapsed - first_token):.0f} tok/s")
                parts.append(f"{elapsed:.1f} s")
                value["message"] = " · ".join(parts)
                yield value
            elif now - last_shown >= 0.3:  # the component re-renders on every update: a few per second is plenty
                last_shown = now
                yield state_value(**shown, raw=text, status="running", message=f"{variant.name} · {size} · generating… {now - started:.0f} s")
    except gr.Error:
        raise  # ZeroGPU quota messages and the like: Gradio shows them
    except Exception as e:
        yield state_value(**shown, raw=text, status="error", message=f"{type(e).__name__}: {e}")


# ---- The app: one component ------------------------------------------------------------------------------

TEMPLATE = (UI / "app.html").read_text(encoding="utf-8")
for key, text in {"__LOGO_LIGHT__": (ROOT / "static" / "lighton.svg").read_text(encoding="utf-8"),
                  "__LOGO_DARK__": (ROOT / "static" / "lighton-dark.svg").read_text(encoding="utf-8"),
                  "__COLLECTION__": COLLECTION, "__PAPER__": PAPER, "__BLOG__": BLOG, "__WEBSITE__": WEBSITE}.items():
    TEMPLATE = TEMPLATE.replace(key, text)

HEAD = """
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css">
<script src="https://cdn.jsdelivr.net/npm/marked@14.1.3/marked.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dompurify@3.1.7/dist/purify.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.min.js"></script>
"""

# The page frame and the design tokens. `.dark` is the class Gradio puts on <body>; ui/app.js toggles it too.
CSS = """
body {
  --lo-font: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --lo-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --lo-bg: #f3f5f8; --lo-surface: #ffffff; --lo-surface-2: #f6f8fa; --lo-ink: #0f172a; --lo-muted: #64748b;
  --lo-line: #e5e9f0; --lo-line-strong: #cbd3df; --lo-accent: #0a8db0; --lo-accent-soft: rgba(10, 141, 176, .1);
  --lo-danger: #dc2626; --lo-ok: #16a34a;
  background: var(--lo-bg) !important;
}
body.dark {
  --lo-bg: #0b0f15; --lo-surface: #121820; --lo-surface-2: #182029; --lo-ink: #e6ebf2; --lo-muted: #8e9aab;
  --lo-line: #232d3a; --lo-line-strong: #34404f; --lo-accent: #2eb5d1; --lo-accent-soft: rgba(46, 181, 209, .14);
  --lo-danger: #f87171; --lo-ok: #4ade80;
}
.dark .logo-light { display: none !important } .dark .logo-dark { display: inline !important }
.gradio-container { max-width: none !important; padding: 0 !important; background: transparent !important }
.gradio-container .main.app { padding: 0 !important }
#lo-app { border: 0 !important; border-radius: 0 !important }
footer { display: none !important }
@keyframes lo-spin { to { transform: rotate(360deg) } }
@keyframes lo-live { 50% { box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 45%, transparent) } }
@keyframes lo-blink { 50% { opacity: 0 } }
@keyframes lo-slide { 0% { transform: translateX(-100%) } 100% { transform: translateX(340%) } }
"""

with gr.Blocks(title="LightOnOCR-3", fill_width=True) as demo:
    app = gr.HTML(
        EMPTY,
        html_template=TEMPLATE,
        css_template=(UI / "app.css").read_text(encoding="utf-8"),
        js_on_load=(UI / "app.js").read_text(encoding="utf-8"),
        head=HEAD,
        apply_default_css=False,
        padding=False,
        elem_id="lo-app",
        # props read by ui/app.js
        models=[{"key": k, "name": ocr.VARIANTS[k].name, "summary": ocr.VARIANTS[k].summary} for k in ocr.ENABLED],
        examples=[{"thumb": thumb(p), "caption": c} for p, c in EXAMPLES],
        defaults={"model": ocr.DEFAULT_VARIANT, "mode": "grounding", "temperature": 0.2, "max_tokens": ocr.DEFAULT_MAX_TOKENS, "longest_edge": DEFAULT_LONGEST_EDGE},
    )
    app.lo_open(on_open, None, [app], show_progress="hidden")
    app.lo_page(on_page, None, [app], show_progress="hidden")
    running = app.lo_run(on_run, None, [app], show_progress="hidden")
    app.lo_stop(lambda: None, None, None, cancels=[running], show_progress="hidden")


if __name__ == "__main__":
    demo.queue().launch(css=CSS, ssr_mode=False, footer_links=[])