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

Download checkpoint from HF hub in OcrReorderPipeline

Browse files
Files changed (1) hide show
  1. app.py +8 -14
app.py CHANGED
@@ -8,21 +8,17 @@ 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
 
17
- # instantiate your custom pipeline
18
- pipe = OcrReorderPipeline(model, tokenizer, processor, device=0)
19
-
20
- # ── 2) Inference function ─────────────────────────────────────────────────
21
  def infer(image_path, json_file):
22
- # Extract filename
23
  img_name = os.path.basename(image_path)
24
 
25
- # Parse NDJSON (one JSON object per line)
26
  data = []
27
  with open(json_file.name, "r", encoding="utf-8") as f:
28
  for line in f:
@@ -31,7 +27,6 @@ def infer(image_path, json_file):
31
  continue
32
  data.append(json.loads(line))
33
 
34
- # Find the matching entry
35
  entry = next((e for e in data if e["img_name"] == img_name), None)
36
  if entry is None:
37
  return f"❌ No JSON entry found for image '{img_name}'"
@@ -39,17 +34,15 @@ def infer(image_path, json_file):
39
  words = entry["src_word_list"]
40
  boxes = entry["src_wordbox_list"]
41
 
42
- # Load & encode the image as base64
43
  img = Image.open(image_path).convert("RGB")
44
- buf = BytesIO()
45
- img.save(buf, format="PNG")
46
  b64 = base64.b64encode(buf.getvalue()).decode()
47
 
48
- # ▢️ Correct keyword call so preprocess gets all args
49
- reordered = pipe(image=b64, words=words, boxes=boxes)[0]
50
  return reordered
51
 
52
- # ── 3) Build the Gradio interface ───────────────────────────────────────────
53
  demo = gr.Interface(
54
  fn=infer,
55
  inputs=[
@@ -61,4 +54,5 @@ demo = gr.Interface(
61
  )
62
 
63
  if __name__ == "__main__":
 
64
  demo.launch()
 
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
  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
  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=[
 
54
  )
55
 
56
  if __name__ == "__main__":
57
+ # set share=True if you want a public link
58
  demo.launch()