changes
Browse files
app.py
CHANGED
|
@@ -43,28 +43,33 @@ def respond(
|
|
| 43 |
"""
|
| 44 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
"""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|
|
|
|
| 43 |
"""
|
| 44 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
"""
|
| 46 |
+
|
| 47 |
+
import gradio as gr
|
| 48 |
+
from gradio_pdf import PDF
|
| 49 |
+
from pdf2image import convert_from_path
|
| 50 |
+
from transformers import pipeline
|
| 51 |
+
from pathlib import Path
|
| 52 |
+
|
| 53 |
+
dir_ = Path(__file__).parent
|
| 54 |
+
|
| 55 |
+
p = pipeline(
|
| 56 |
+
"document-question-answering",
|
| 57 |
+
model="impira/layoutlm-document-qa",
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
+
def qa(question: str, doc: str) -> str:
|
| 61 |
+
img = convert_from_path(doc)[0]
|
| 62 |
+
output = p(img, question)
|
| 63 |
+
return sorted(output, key=lambda x: x["score"], reverse=True)[0]['answer']
|
| 64 |
+
|
| 65 |
|
| 66 |
+
demo = gr.Interface(
|
| 67 |
+
qa,
|
| 68 |
+
[gr.Textbox(label="Question"), PDF(label="Document")],
|
| 69 |
+
gr.Textbox(),
|
| 70 |
+
examples=[["What is the total gross worth?", str(dir_ / "invoice_2.pdf")],
|
| 71 |
+
["Whos is being invoiced?", str(dir_ / "sample_invoice.pdf")]]
|
| 72 |
+
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
demo.launch()
|