Easily display PDFs in Gradio
pip install gradio_pdf
import gradio as gr
from gradio_pdf import PDF
from pdf2image import convert_from_path
from transformers import pipeline
from pathlib import Path
dir_ = Path(__file__).parent
p = pipeline(
"document-question-answering",
model="impira/layoutlm-document-qa",
)
def qa(question: str, doc: str) -> str:
img = convert_from_path(doc)[0]
output = p(img, question)
return sorted(output, key=lambda x: x["score"], reverse=True)[0]['answer']
demo = gr.Interface(
qa,
[gr.Textbox(label="Question"), PDF(label="Document")],
gr.Textbox(),
examples=[["What is the total gross worth?", str(dir_ / "invoice_2.pdf")],
["Whos is being invoiced?", str(dir_ / "sample_invoice.pdf")]]
)
if __name__ == "__main__":
demo.launch()
PDF| name | type | default | description |
|---|---|---|---|
value |
| None | None |
height |
| None | None |
label |
| None | None |
info |
| None | None |
show_label |
| None | None |
container |
| True | None |
scale |
| None | None |
min_width |
| None | None |
interactive |
| None | None |
visible |
| True | None |
elem_id |
| None | None |
elem_classes |
| None | None |
render |
| True | None |
load_fn |
| None | None |
every |
| None | None |
starting_page |
| 1 | None |
| name | description |
|---|---|
change | |
upload |
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
The code snippet below is accurate in cases where the component is used as both an input and an output.
def predict(
value: str
) -> str | None:
return value
JavaScript
99.3%
Easily display PDFs in Gradio
pip install gradio_pdf
import gradio as gr
from gradio_pdf import PDF
from pdf2image import convert_from_path
from transformers import pipeline
from pathlib import Path
dir_ = Path(__file__).parent
p = pipeline(
"document-question-answering",
model="impira/layoutlm-document-qa",
)
def qa(question: str, doc: str) -> str:
img = convert_from_path(doc)[0]
output = p(img, question)
return sorted(output, key=lambda x: x["score"], reverse=True)[0]['answer']
demo = gr.Interface(
qa,
[gr.Textbox(label="Question"), PDF(label="Document")],
gr.Textbox(),
examples=[["What is the total gross worth?", str(dir_ / "invoice_2.pdf")],
["Whos is being invoiced?", str(dir_ / "sample_invoice.pdf")]]
)
if __name__ == "__main__":
demo.launch()
PDF| name | type | default | description |
|---|---|---|---|
value |
| None | None |
height |
| None | None |
label |
| None | None |
info |
| None | None |
show_label |
| None | None |
container |
| True | None |
scale |
| None | None |
min_width |
| None | None |
interactive |
| None | None |
visible |
| True | None |
elem_id |
| None | None |
elem_classes |
| None | None |
render |
| True | None |
load_fn |
| None | None |
every |
| None | None |
starting_page |
| 1 | None |
| name | description |
|---|---|
change | |
upload |
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
The code snippet below is accurate in cases where the component is used as both an input and an output.
def predict(
value: str
) -> str | None:
return value
JavaScript
99.3%