Spaces:
Sleeping
Sleeping
xavierbarbier
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
from gradio_pdf import PDF
|
3 |
-
from
|
|
|
|
|
4 |
|
5 |
-
|
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 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
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.
|
|
|
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()
|