JoeQian commited on
Commit
4c8bf13
·
verified ·
1 Parent(s): 582c567

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio_pdf import PDF
3
+ from pdf2image import convert_from_path
4
+ from transformers import pipeline
5
+ from pathlib import Path
6
+
7
+ dir_ = Path(__file__).parent
8
+
9
+ p = pipeline(
10
+ "document-question-answering",
11
+ model="impira/layoutlm-document-qa",
12
+ )
13
+
14
+ def qa(question: str, doc: str) -> str:
15
+ img = convert_from_path(doc)[0]
16
+ output = p(img, question)
17
+ return sorted(output, key=lambda x: x["score"], reverse=True)[0]['answer']
18
+
19
+
20
+ demo = gr.Interface(
21
+ qa,
22
+ [gr.Textbox(label="Question"), PDF(label="Document")],
23
+ gr.Textbox(),
24
+ examples=[["What is the total gross worth?", str(dir_ / "invoice_2.pdf")],
25
+ ["Whos is being invoiced?", str(dir_ / "sample_invoice.pdf")]]
26
+ )
27
+
28
+ if __name__ == "__main__":
29
+ demo.launch()