Spaces:
Running
Running
Uddipan Basu Bir
commited on
Commit
·
ab9088f
1
Parent(s):
5b9baff
Load tokenizer & processor from preprocessor subfolder
Browse files
app.py
CHANGED
@@ -6,26 +6,32 @@ from inference import OcrReorderPipeline
|
|
6 |
from transformers import (
|
7 |
AutoProcessor,
|
8 |
LayoutLMv3Model,
|
9 |
-
T5ForConditionalGeneration,
|
10 |
AutoTokenizer
|
11 |
)
|
12 |
import torch
|
13 |
|
14 |
-
# Load from your model repo
|
15 |
repo = "Uddipan107/ocr-layoutlmv3-base-t5-small"
|
16 |
model = LayoutLMv3Model.from_pretrained(repo)
|
17 |
-
tokenizer = AutoTokenizer.from_pretrained(repo)
|
18 |
-
processor = AutoProcessor.from_pretrained(repo, apply_ocr=False)
|
19 |
-
|
|
|
|
|
20 |
|
21 |
def infer(image, words_json, boxes_json):
|
22 |
words = json.loads(words_json)
|
23 |
boxes = json.loads(boxes_json)
|
24 |
-
|
|
|
|
|
|
|
25 |
b64 = base64.b64encode(buf.getvalue()).decode()
|
26 |
-
|
|
|
27 |
return pipe(b64, words, boxes)[0]
|
28 |
|
|
|
29 |
demo = gr.Interface(
|
30 |
fn=infer,
|
31 |
inputs=[
|
|
|
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=[
|