Uddipan Basu Bir commited on
Commit
fabf362
·
1 Parent(s): 93dce4d

Download checkpoint from HF hub in OcrReorderPipeline

Browse files
Files changed (1) hide show
  1. app.py +31 -23
app.py CHANGED
@@ -1,46 +1,54 @@
1
- import 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 (
7
- AutoProcessor,
8
- LayoutLMv3Model,
9
- AutoTokenizer
10
- )
11
- import torch
12
 
13
- # 1) Load from your model repo, pointing at the `preprocessor/` folder
14
- repo = "Uddipan107/ocr-layoutlmv3-base-t5-small"
15
  model = LayoutLMv3Model.from_pretrained(repo)
16
  tokenizer = AutoTokenizer.from_pretrained(repo, subfolder="preprocessor")
17
  processor = AutoProcessor.from_pretrained(repo, subfolder="preprocessor", apply_ocr=False)
 
 
 
 
 
 
 
 
 
18
 
19
- # 2) Instantiate your pipeline
20
- pipe = OcrReorderPipeline(model, tokenizer, processor, device=0)
 
 
21
 
22
- def infer(image, words_json, boxes_json):
23
- words = json.loads(words_json)
24
- boxes = json.loads(boxes_json)
25
 
26
- # Encode PIL image PNG base64
27
- buf = BytesIO()
28
- image.save(buf, format="PNG")
29
  b64 = base64.b64encode(buf.getvalue()).decode()
30
 
31
- # Run your custom pipeline and return the first (only) output string
32
  return pipe(b64, words, boxes)[0]
33
 
34
- # 3) Gradio UI
35
  demo = gr.Interface(
36
  fn=infer,
37
  inputs=[
38
- gr.Image(type="pil", label="Image"),
39
- gr.Textbox(label="Words (JSON list)"),
40
- gr.Textbox(label="Boxes (JSON list)")
 
41
  ],
42
  outputs="text",
43
- title="OCR Reorder Pipeline"
44
  )
45
 
46
  if __name__ == "__main__":
 
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")
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
+ # 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}'"
30
 
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__":