Uddipan Basu Bir commited on
Commit
4617aca
Β·
1 Parent(s): a0040a5

Download checkpoint from HF hub in OcrReorderPipeline

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -8,17 +8,18 @@ import gradio as gr
8
  from inference import OcrReorderPipeline
9
  from transformers import AutoProcessor, LayoutLMv3Model, AutoTokenizer
10
 
11
- # 1) Load model/tokenizer/processor
12
  repo = "Uddipan107/ocr-layoutlmv3-base-t5-small"
13
  model = LayoutLMv3Model.from_pretrained(repo)
14
  tokenizer = AutoTokenizer.from_pretrained(repo, subfolder="preprocessor")
15
  processor = AutoProcessor.from_pretrained(repo, subfolder="preprocessor", apply_ocr=False)
16
  pipe = OcrReorderPipeline(model, tokenizer, processor, device=0)
17
 
 
18
  def infer(image_path, json_file):
19
  img_name = os.path.basename(image_path)
20
 
21
- # Parse NDJSON
22
  data = []
23
  with open(json_file.name, "r", encoding="utf-8") as f:
24
  for line in f:
@@ -27,6 +28,7 @@ def infer(image_path, json_file):
27
  continue
28
  data.append(json.loads(line))
29
 
 
30
  entry = next((e for e in data if e["img_name"] == img_name), None)
31
  if entry is None:
32
  return f"❌ No JSON entry found for image '{img_name}'"
@@ -34,15 +36,17 @@ def infer(image_path, json_file):
34
  words = entry["src_word_list"]
35
  boxes = entry["src_wordbox_list"]
36
 
37
- # Read & encode image
38
  img = Image.open(image_path).convert("RGB")
39
- buf = BytesIO(); img.save(buf, format="PNG")
 
40
  b64 = base64.b64encode(buf.getvalue()).decode()
41
 
42
- # ⚠️ Pass as `inputs`
43
  reordered = pipe(inputs=b64, words=words, boxes=boxes)[0]
44
  return reordered
45
 
 
46
  demo = gr.Interface(
47
  fn=infer,
48
  inputs=[
 
8
  from inference import OcrReorderPipeline
9
  from transformers import AutoProcessor, LayoutLMv3Model, AutoTokenizer
10
 
11
+ # ── 1) Load model + tokenizer + processor ─────────────────────────
12
  repo = "Uddipan107/ocr-layoutlmv3-base-t5-small"
13
  model = LayoutLMv3Model.from_pretrained(repo)
14
  tokenizer = AutoTokenizer.from_pretrained(repo, subfolder="preprocessor")
15
  processor = AutoProcessor.from_pretrained(repo, subfolder="preprocessor", apply_ocr=False)
16
  pipe = OcrReorderPipeline(model, tokenizer, processor, device=0)
17
 
18
+ # ── 2) Inference function ──────────────────────────────────────────
19
  def infer(image_path, json_file):
20
  img_name = os.path.basename(image_path)
21
 
22
+ # Parse NDJSON entries from uploaded file
23
  data = []
24
  with open(json_file.name, "r", encoding="utf-8") as f:
25
  for line in f:
 
28
  continue
29
  data.append(json.loads(line))
30
 
31
+ # Find matching entry for this image
32
  entry = next((e for e in data if e["img_name"] == img_name), None)
33
  if entry is None:
34
  return f"❌ No JSON entry found for image '{img_name}'"
 
36
  words = entry["src_word_list"]
37
  boxes = entry["src_wordbox_list"]
38
 
39
+ # Read and encode image to base64
40
  img = Image.open(image_path).convert("RGB")
41
+ buf = BytesIO()
42
+ img.save(buf, format="PNG")
43
  b64 = base64.b64encode(buf.getvalue()).decode()
44
 
45
+ # Call pipeline with `inputs` keyword plus extra args
46
  reordered = pipe(inputs=b64, words=words, boxes=boxes)[0]
47
  return reordered
48
 
49
+ # ── 3) Gradio interface ─────────────────────────────────────────────
50
  demo = gr.Interface(
51
  fn=infer,
52
  inputs=[