Easily display PDFs in Gradio
Project description
gradio_pdf
Easily display PDFs in Gradio
Installation
pip install gradio_pdf
Usage
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
Initialization
name | type | default | description |
---|---|---|---|
value |
typing.Any
|
None |
None |
height |
int | None
|
None |
None |
label |
str | None
|
None |
None |
info |
str | None
|
None |
None |
show_label |
bool | None
|
None |
None |
container |
bool
|
True |
None |
scale |
int | None
|
None |
None |
min_width |
int | None
|
None |
None |
interactive |
bool | None
|
None |
None |
visible |
bool
|
True |
None |
elem_id |
str | None
|
None |
None |
elem_classes |
list[str] | str | None
|
None |
None |
render |
bool
|
True |
None |
load_fn |
Callable[Ellipsis, typing.Any] | None
|
None |
None |
every |
float | None
|
None |
None |
Events
name | description |
---|---|
change |
|
upload |
User function
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).
- When used as an Input, the component only impacts the input signature of the user function.
- When used as an output, the component only impacts the return signature of the user function.
The code snippet below is accurate in cases where the component is used as both an input and an output.
- As output: Is passed, the preprocessed input data sent to the user's function in the backend.
- As input: Should return, the output data received by the component from the user's function in the backend.
def predict(
value: str
) -> str | None:
return value
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
gradio_pdf-0.0.7.tar.gz
(669.5 kB
view hashes)
Built Distribution
gradio_pdf-0.0.7-py3-none-any.whl
(306.2 kB
view hashes)
Close
Hashes for gradio_pdf-0.0.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6cb3e0937593637cc1b6236a8cfbeb69d250c2dca3b75247a94a321fe610f88a |
|
MD5 | ba580debe911e1f6b1f9322908aa4408 |
|
BLAKE2b-256 | b497f4b40cab69c75340a835d11c760d3d2bfe01de578cce4823c295290c0c37 |