Uddipan Basu Bir commited on
Commit
2ebc710
·
1 Parent(s): fabf362

Download checkpoint from HF hub in OcrReorderPipeline

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -1,14 +1,11 @@
1
- import os
2
- import json
3
- import base64
4
  from io import BytesIO
5
  from PIL import Image
6
  import gradio as gr
7
-
8
  from inference import OcrReorderPipeline
9
  from transformers import AutoProcessor, LayoutLMv3Model, AutoTokenizer
10
 
11
- # 1) Load your model + tokenizer + processor as before
12
  repo = "Uddipan107/ocr-layoutlmv3-base-t5-small"
13
  model = LayoutLMv3Model.from_pretrained(repo)
14
  tokenizer = AutoTokenizer.from_pretrained(repo, subfolder="preprocessor")
@@ -16,14 +13,17 @@ processor = AutoProcessor.from_pretrained(repo, subfolder="preprocessor", apply_
16
  pipe = OcrReorderPipeline(model, tokenizer, processor, device=0)
17
 
18
  def infer(image_path, json_file):
19
- # 2) Extract the filename user uploaded
20
  img_name = os.path.basename(image_path)
21
 
22
- # 3) Load the entire JSON; assume it’s a list of entries
 
23
  with open(json_file.name, "r", encoding="utf-8") as f:
24
- data = json.load(f)
 
 
 
 
25
 
26
- # 4) Find the entry matching this image
27
  entry = next((e for e in data if e["img_name"] == img_name), None)
28
  if entry is None:
29
  return f"❌ No JSON entry found for image '{img_name}'"
@@ -31,24 +31,22 @@ def infer(image_path, json_file):
31
  words = entry["src_word_list"]
32
  boxes = entry["src_wordbox_list"]
33
 
34
- # 5) Read the image, encode to base64 for your pipeline
35
  img = Image.open(image_path).convert("RGB")
36
  buf = BytesIO(); img.save(buf, format="PNG")
37
  b64 = base64.b64encode(buf.getvalue()).decode()
38
 
39
- # 6) Call your pipeline and return the reordered text
40
  return pipe(b64, words, boxes)[0]
41
 
42
  demo = gr.Interface(
43
  fn=infer,
44
  inputs=[
45
- # get the file path so we can match the filename
46
  gr.Image(type="filepath", label="Upload Image"),
47
- # this is the JSON file containing a list of entries
48
- gr.File(label="Upload JSON file")
49
  ],
50
  outputs="text",
51
- title="OCR Reorder (match image JSON entry)"
52
  )
53
 
54
  if __name__ == "__main__":
 
1
+ import os, json, base64
 
 
2
  from io import BytesIO
3
  from PIL import Image
4
  import gradio as gr
 
5
  from inference import OcrReorderPipeline
6
  from transformers import AutoProcessor, LayoutLMv3Model, AutoTokenizer
7
 
8
+ # Load model/tokenizer/processor...
9
  repo = "Uddipan107/ocr-layoutlmv3-base-t5-small"
10
  model = LayoutLMv3Model.from_pretrained(repo)
11
  tokenizer = AutoTokenizer.from_pretrained(repo, subfolder="preprocessor")
 
13
  pipe = OcrReorderPipeline(model, tokenizer, processor, device=0)
14
 
15
  def infer(image_path, json_file):
 
16
  img_name = os.path.basename(image_path)
17
 
18
+ # Parse NDJSON from the uploaded file
19
+ data = []
20
  with open(json_file.name, "r", encoding="utf-8") as f:
21
+ for line in f:
22
+ line = line.strip()
23
+ if not line:
24
+ continue
25
+ data.append(json.loads(line))
26
 
 
27
  entry = next((e for e in data if e["img_name"] == img_name), None)
28
  if entry is None:
29
  return f"❌ No JSON entry found for image '{img_name}'"
 
31
  words = entry["src_word_list"]
32
  boxes = entry["src_wordbox_list"]
33
 
34
+ # Read and encode image
35
  img = Image.open(image_path).convert("RGB")
36
  buf = BytesIO(); img.save(buf, format="PNG")
37
  b64 = base64.b64encode(buf.getvalue()).decode()
38
 
39
+ # Run pipeline
40
  return pipe(b64, words, boxes)[0]
41
 
42
  demo = gr.Interface(
43
  fn=infer,
44
  inputs=[
 
45
  gr.Image(type="filepath", label="Upload Image"),
46
+ gr.File(label="Upload JSON (NDJSON format)")
 
47
  ],
48
  outputs="text",
49
+ title="OCR Reorder (Image + NDJSON upload)"
50
  )
51
 
52
  if __name__ == "__main__":