Spaces:
Sleeping
Sleeping
Commit ·
fc60fc6
1
Parent(s): d8f309f
initial space
Browse files- __init__.py +0 -0
- apis/__init__.py +0 -0
- apis/layoutlm.py +84 -0
- demo.py +36 -0
__init__.py
ADDED
|
File without changes
|
apis/__init__.py
ADDED
|
File without changes
|
apis/layoutlm.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
class LayoutLM:
|
| 5 |
+
|
| 6 |
+
def __init__(self, save_pretrained_fpath:str=None) -> None:
|
| 7 |
+
self.pipeline_category = 'document-question-answering'
|
| 8 |
+
self.tf_pipeline = pipeline
|
| 9 |
+
self.pipeline = None
|
| 10 |
+
|
| 11 |
+
if save_pretrained_fpath is not None:
|
| 12 |
+
pipe = self.tf_pipeline(self.pipeline_category)
|
| 13 |
+
pipe.save_pretrained(save_pretrained_fpath)
|
| 14 |
+
|
| 15 |
+
self.default_model = 'impira/layoutlm-invoices'
|
| 16 |
+
self.default_ex_answer = {'score':0, 'answer':'-'}
|
| 17 |
+
|
| 18 |
+
def set_model(self, model:str):
|
| 19 |
+
if model is None:
|
| 20 |
+
model = self.default_model
|
| 21 |
+
|
| 22 |
+
self.pipeline = self.tf_pipeline(self.pipeline_category, model=model)
|
| 23 |
+
|
| 24 |
+
def answer_the_question(self, img, question: str, is_debug=False):
|
| 25 |
+
score = 0
|
| 26 |
+
answer = '-'
|
| 27 |
+
answers = None
|
| 28 |
+
if self.pipeline is not None:
|
| 29 |
+
answers = self.pipeline(img, question)
|
| 30 |
+
|
| 31 |
+
for a in answers:
|
| 32 |
+
if a['score'] > score:
|
| 33 |
+
score = a['score']
|
| 34 |
+
answer = a['answer']
|
| 35 |
+
|
| 36 |
+
if is_debug:
|
| 37 |
+
print('--------------------')
|
| 38 |
+
print(f'Q: {question}\nA: {answer} (acc:{score:.2f})\n')
|
| 39 |
+
print(answers)
|
| 40 |
+
|
| 41 |
+
return answer
|
| 42 |
+
|
| 43 |
+
def inference(self, img, is_debug=False):
|
| 44 |
+
merchant_id = self.answer_the_question(img, 'What is merchant ID?')
|
| 45 |
+
merchant_name = self.answer_the_question(img, 'What is merchant name?')
|
| 46 |
+
merchant_address = self.answer_the_question(img, 'What is merchant address?')
|
| 47 |
+
invoice_no = self.answer_the_question(img, 'What is tax invoice number?')
|
| 48 |
+
products = self.answer_the_question(img, 'What are buy products?')
|
| 49 |
+
pos_no = self.answer_the_question(img, 'What is POS number?')
|
| 50 |
+
net_price = self.answer_the_question(img, 'What is the net-price?')
|
| 51 |
+
date_time = self.answer_the_question(img, 'What date, year and time of the invoice?')
|
| 52 |
+
|
| 53 |
+
if is_debug:
|
| 54 |
+
print(f'Merchant ID: {merchant_id}')
|
| 55 |
+
print(f'Merchant name: {merchant_name}')
|
| 56 |
+
print(f'Merchant address: {merchant_address}')
|
| 57 |
+
print(f'Invoice no.: {invoice_no}')
|
| 58 |
+
print(f'Products: {products}')
|
| 59 |
+
print(f'POS no.: {pos_no}')
|
| 60 |
+
print(f'Net price: {net_price}')
|
| 61 |
+
print(f'Date/Time: {date_time}')
|
| 62 |
+
|
| 63 |
+
return pd.DataFrame({
|
| 64 |
+
'Data' : [
|
| 65 |
+
'Merchant ID',
|
| 66 |
+
'Merchant name',
|
| 67 |
+
'Merchant address',
|
| 68 |
+
'Invoice no.',
|
| 69 |
+
'Products',
|
| 70 |
+
'POS no.',
|
| 71 |
+
'Net price',
|
| 72 |
+
'Date/Time'
|
| 73 |
+
],
|
| 74 |
+
'Value' : [
|
| 75 |
+
str(merchant_id),
|
| 76 |
+
str(merchant_name),
|
| 77 |
+
str(merchant_address),
|
| 78 |
+
str(invoice_no),
|
| 79 |
+
str(products),
|
| 80 |
+
str(pos_no),
|
| 81 |
+
str(net_price),
|
| 82 |
+
str(date_time)
|
| 83 |
+
]
|
| 84 |
+
})
|
demo.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from apis.layoutlm import LayoutLM
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
layoutlm = None
|
| 7 |
+
|
| 8 |
+
def auth(username, password):
|
| 9 |
+
u = os.environ.get('USERNAME')
|
| 10 |
+
p = os.environ.get('PASSWORD')
|
| 11 |
+
return (username == u and password == p)
|
| 12 |
+
|
| 13 |
+
def inference(img) -> pd.DataFrame:
|
| 14 |
+
return layoutlm.inference(img)
|
| 15 |
+
|
| 16 |
+
if __name__ == "__main__":
|
| 17 |
+
try:
|
| 18 |
+
layoutlm = LayoutLM()
|
| 19 |
+
layoutlm.set_model(layoutlm.default_model)
|
| 20 |
+
|
| 21 |
+
demo = gr.Interface(
|
| 22 |
+
inference,
|
| 23 |
+
gr.Image(),
|
| 24 |
+
gr.Dataframe(
|
| 25 |
+
headers=['Data', 'Value'],
|
| 26 |
+
datatype=['str', 'str'],
|
| 27 |
+
row_count=8,
|
| 28 |
+
col_count=(2, 'fixed'),
|
| 29 |
+
interactive=False
|
| 30 |
+
)
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
demo.launch(auth=auth)
|
| 34 |
+
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(str(e))
|