Fix HF runtime crash: lazy OCR init + disable SSR
Browse files
app.py
CHANGED
|
@@ -1,54 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def run_ocr(img):
|
| 2 |
if img is None:
|
| 3 |
-
return "", 0.0
|
| 4 |
|
| 5 |
img_np = np.array(img)
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
lines = []
|
| 10 |
confs = []
|
| 11 |
|
| 12 |
-
|
| 13 |
-
# - [ [ [box, (text, conf)], ... ] ] (nested)
|
| 14 |
-
# - [ [box, (text, conf)], ... ] (less nested)
|
| 15 |
-
# - other variants depending on version
|
| 16 |
-
blocks = result
|
| 17 |
-
if isinstance(result, list) and len(result) == 1 and isinstance(result[0], list):
|
| 18 |
-
# sometimes it's wrapped one extra level
|
| 19 |
-
blocks = result[0]
|
| 20 |
|
| 21 |
for item in blocks:
|
| 22 |
-
if not item:
|
| 23 |
-
continue
|
| 24 |
-
|
| 25 |
-
# item might be: [box, (text, conf)] OR [[box], [text, conf]] etc.
|
| 26 |
-
# Try common case first
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
if text:
|
| 32 |
-
lines.append(str(text))
|
| 33 |
-
confs.append(conf)
|
| 34 |
-
continue
|
| 35 |
except Exception:
|
| 36 |
-
pass
|
| 37 |
-
|
| 38 |
-
# Fallback: search inside item for a (text, conf) pair
|
| 39 |
-
found = False
|
| 40 |
-
for part in item:
|
| 41 |
-
if isinstance(part, (list, tuple)) and len(part) >= 2:
|
| 42 |
-
# part could be (text, conf) or [text, conf]
|
| 43 |
-
if isinstance(part[0], str) and isinstance(part[1], (float, int)):
|
| 44 |
-
lines.append(part[0])
|
| 45 |
-
confs.append(float(part[1]))
|
| 46 |
-
found = True
|
| 47 |
-
break
|
| 48 |
-
if found:
|
| 49 |
continue
|
| 50 |
|
| 51 |
extracted = "\n".join(lines).strip()
|
| 52 |
-
avg_conf =
|
| 53 |
|
| 54 |
return extracted if extracted else "(No text detected)", avg_conf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from paddleocr import PaddleOCR
|
| 4 |
+
|
| 5 |
+
ocr = None # lazy init
|
| 6 |
+
|
| 7 |
+
def get_ocr():
|
| 8 |
+
global ocr
|
| 9 |
+
if ocr is None:
|
| 10 |
+
ocr = PaddleOCR(use_angle_cls=True, lang="en")
|
| 11 |
+
return ocr
|
| 12 |
+
|
| 13 |
def run_ocr(img):
|
| 14 |
if img is None:
|
| 15 |
+
return "(No image)", 0.0
|
| 16 |
|
| 17 |
img_np = np.array(img)
|
| 18 |
|
| 19 |
+
ocr_engine = get_ocr()
|
| 20 |
+
result = ocr_engine.ocr(img_np)
|
| 21 |
|
| 22 |
lines = []
|
| 23 |
confs = []
|
| 24 |
|
| 25 |
+
blocks = result[0] if isinstance(result, list) and result and isinstance(result[0], list) else result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
for item in blocks:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
+
text, conf = item[1]
|
| 30 |
+
lines.append(text)
|
| 31 |
+
confs.append(float(conf))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
continue
|
| 34 |
|
| 35 |
extracted = "\n".join(lines).strip()
|
| 36 |
+
avg_conf = sum(confs) / len(confs) if confs else 0.0
|
| 37 |
|
| 38 |
return extracted if extracted else "(No text detected)", avg_conf
|
| 39 |
+
|
| 40 |
+
demo = gr.Interface(
|
| 41 |
+
fn=run_ocr,
|
| 42 |
+
inputs=gr.Image(type="pil", label="Upload a page photo"),
|
| 43 |
+
outputs=[
|
| 44 |
+
gr.Textbox(label="Extracted text", lines=12),
|
| 45 |
+
gr.Number(label="Average confidence (0–1)")
|
| 46 |
+
],
|
| 47 |
+
title="BookReader × Reachy Mini",
|
| 48 |
+
description="CPU-based PaddleOCR for physical book pages.",
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
demo.launch(ssr_mode=False)
|