xavierbarbier commited on
Commit
70b610d
·
verified ·
1 Parent(s): 714f084

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -1,27 +1,29 @@
1
  import gradio as gr
2
  from gradio_pdf import PDF
3
- from pypdf import PdfReader
 
 
4
 
5
- def upload_file(files):
6
- #file_paths = [file.name for file in files]
7
- file_paths = files
8
- return file_paths
9
-
10
- def summarise_file(file):
11
- # text = PdfReader(file)
12
-
13
- return file
14
 
15
- with gr.Blocks() as demo:
16
- file_output = gr.File()
17
- upload_button = gr.UploadButton("Click to Upload a File", file_types=["pdf", "doc"], file_count="single")
18
- upload_button.upload(upload_file, upload_button, file_output)
19
-
20
- file_summary = gr.Textbox()
21
- sum_button = gr.Button("Click to summarise file")
22
-
23
- sum_button.click(summarise_file, file_output, file_summary)
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  if __name__ == "__main__":
27
- demo.queue(max_size=3).launch()
 
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()