Spaces:
Sleeping
Sleeping
Commit
·
66ebefb
1
Parent(s):
4e0a104
feat: added more result and modified layout
Browse files- README.md +14 -2
- apis/layoutlm.py +16 -8
- app.py +21 -10
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: 📚
|
| 4 |
colorFrom: pink
|
| 5 |
colorTo: blue
|
|
@@ -9,4 +9,16 @@ app_file: app.py
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: LayoutLM Invoice
|
| 3 |
emoji: 📚
|
| 4 |
colorFrom: pink
|
| 5 |
colorTo: blue
|
|
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
## LayoutLM for Invoice
|
| 13 |
+
|
| 14 |
+
### Pre-requirements
|
| 15 |
+
|
| 16 |
+
Tesseract requires you to install `tesseract-ocr` via `apt` command, just adding it in `packages.txt`.
|
| 17 |
+
|
| 18 |
+
```text
|
| 19 |
+
tesseract-ocr
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
### Secure
|
| 23 |
+
|
| 24 |
+
This service is private application, requires user name and password (ref. secret variable `USERNAME` & `PASSWORD`).
|
apis/layoutlm.py
CHANGED
|
@@ -41,21 +41,25 @@ class LayoutLM:
|
|
| 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 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 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}')
|
|
@@ -65,8 +69,10 @@ class LayoutLM:
|
|
| 65 |
'Merchant ID',
|
| 66 |
'Merchant name',
|
| 67 |
'Merchant address',
|
|
|
|
| 68 |
'Invoice no.',
|
| 69 |
'Products',
|
|
|
|
| 70 |
'POS no.',
|
| 71 |
'Net price',
|
| 72 |
'Date/Time'
|
|
@@ -75,8 +81,10 @@ class LayoutLM:
|
|
| 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)
|
|
|
|
| 41 |
return answer
|
| 42 |
|
| 43 |
def inference(self, img, is_debug=False):
|
| 44 |
+
merchant_id = self.answer_the_question(img, 'What is merchant ID?', is_debug=is_debug)
|
| 45 |
+
merchant_name = self.answer_the_question(img, 'What is merchant name?', is_debug=is_debug)
|
| 46 |
+
merchant_address = self.answer_the_question(img, 'What is merchant address?', is_debug=is_debug)
|
| 47 |
+
merchant_branch = self.answer_the_question(img, 'What is branch of merchant?', is_debug=is_debug)
|
| 48 |
+
invoice_no = self.answer_the_question(img, 'What is invoice number or INV?', is_debug=is_debug)
|
| 49 |
+
products = self.answer_the_question(img, 'What are buy products?', is_debug=is_debug)
|
| 50 |
+
product_codes = self.answer_the_question(img, 'What are code of buy products?', is_debug=is_debug)
|
| 51 |
+
pos_no = self.answer_the_question(img, 'What is POS number?', is_debug=is_debug)
|
| 52 |
+
net_price = self.answer_the_question(img, 'What is the net-price?', is_debug=is_debug)
|
| 53 |
+
date_time = self.answer_the_question(img, 'What date, year and time of the invoice?', is_debug=is_debug)
|
| 54 |
|
| 55 |
if is_debug:
|
| 56 |
print(f'Merchant ID: {merchant_id}')
|
| 57 |
print(f'Merchant name: {merchant_name}')
|
| 58 |
print(f'Merchant address: {merchant_address}')
|
| 59 |
+
print(f'Merchant branch: {merchant_branch}')
|
| 60 |
print(f'Invoice no.: {invoice_no}')
|
| 61 |
print(f'Products: {products}')
|
| 62 |
+
print(f'Product codes: {product_codes}')
|
| 63 |
print(f'POS no.: {pos_no}')
|
| 64 |
print(f'Net price: {net_price}')
|
| 65 |
print(f'Date/Time: {date_time}')
|
|
|
|
| 69 |
'Merchant ID',
|
| 70 |
'Merchant name',
|
| 71 |
'Merchant address',
|
| 72 |
+
'Merchant branch',
|
| 73 |
'Invoice no.',
|
| 74 |
'Products',
|
| 75 |
+
'Product codes',
|
| 76 |
'POS no.',
|
| 77 |
'Net price',
|
| 78 |
'Date/Time'
|
|
|
|
| 81 |
str(merchant_id),
|
| 82 |
str(merchant_name),
|
| 83 |
str(merchant_address),
|
| 84 |
+
str(merchant_branch),
|
| 85 |
str(invoice_no),
|
| 86 |
str(products),
|
| 87 |
+
str(product_codes),
|
| 88 |
str(pos_no),
|
| 89 |
str(net_price),
|
| 90 |
str(date_time)
|
app.py
CHANGED
|
@@ -13,25 +13,36 @@ def auth(username, password):
|
|
| 13 |
def inference(img) -> pd.DataFrame:
|
| 14 |
return layoutlm.inference(img)
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
layoutlm = LayoutLM()
|
| 20 |
-
layoutlm.set_model(layoutlm.default_model)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
headers=['Data', 'Value'],
|
| 27 |
datatype=['str', 'str'],
|
| 28 |
row_count=8,
|
| 29 |
col_count=(2, 'fixed'),
|
| 30 |
interactive=False
|
| 31 |
)
|
| 32 |
-
)
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
print(str(e))
|
|
|
|
| 13 |
def inference(img) -> pd.DataFrame:
|
| 14 |
return layoutlm.inference(img)
|
| 15 |
|
| 16 |
+
def ask(img, question) -> str:
|
| 17 |
+
return layoutlm.answer_the_question(img, question, is_debug=True)
|
| 18 |
|
| 19 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
with gr.Column():
|
| 22 |
+
inp_img = gr.Image(type='pil')
|
| 23 |
+
|
| 24 |
+
with gr.Row():
|
| 25 |
+
inp_img = gr.Image(type='pil')
|
| 26 |
+
out = gr.Dataframe(
|
| 27 |
headers=['Data', 'Value'],
|
| 28 |
datatype=['str', 'str'],
|
| 29 |
row_count=8,
|
| 30 |
col_count=(2, 'fixed'),
|
| 31 |
interactive=False
|
| 32 |
)
|
|
|
|
| 33 |
|
| 34 |
+
txt_custom_question = gr.Textbox(label='Your question')
|
| 35 |
+
btn_ask = gr.Button('Ask me')
|
| 36 |
+
txt_out_answer = gr.Textbox(label='Answer', interactive=False)
|
| 37 |
+
|
| 38 |
+
# event
|
| 39 |
+
inp_img.change(inference, inp_img, out)
|
| 40 |
+
btn_ask.click(ask, [inp_img, txt_custom_question], txt_out_answer)
|
| 41 |
|
| 42 |
+
if __name__ == '__main__':
|
| 43 |
+
try:
|
| 44 |
+
layoutlm = LayoutLM()
|
| 45 |
+
layoutlm.set_model(layoutlm.default_model)
|
| 46 |
+
demo.launch(auth=auth)
|
| 47 |
except Exception as e:
|
| 48 |
print(str(e))
|