freddyaboulton/gradio-pdf

Source code of the gradio_pdf custom component.

44

stars

48

commits

JavaScript

primary language

Nov 26, 2025

updated

README

Gradio PDF 📕

Static Badge Static Badge

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

nametypedefaultdescription
value
Any
NoneNone
height
int | None
NoneNone
label
str | None
NoneNone
info
str | None
NoneNone
show_label
bool | None
NoneNone
container
bool
TrueNone
scale
int | None
NoneNone
min_width
int | None
NoneNone
interactive
bool | None
NoneNone
visible
bool
TrueNone
elem_id
str | None
NoneNone
elem_classes
list[str] | str | None
NoneNone
render
bool
TrueNone
load_fn
Callable[Ellipsis, Any] | None
NoneNone
every
float | None
NoneNone
starting_page
int | None
1None

Events

namedescription
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

Contributors

freddyaboulton

45 commits

LeoKingzc1

1 commits

maltenlz

1 commits

neosanma

1 commits

freddyaboulton/gradio-pdf

Source code of the gradio_pdf custom component.

44

stars

48

commits

JavaScript

primary language

Nov 26, 2025

updated

README

Gradio PDF 📕

Static Badge Static Badge

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

nametypedefaultdescription
value
Any
NoneNone
height
int | None
NoneNone
label
str | None
NoneNone
info
str | None
NoneNone
show_label
bool | None
NoneNone
container
bool
TrueNone
scale
int | None
NoneNone
min_width
int | None
NoneNone
interactive
bool | None
NoneNone
visible
bool
TrueNone
elem_id
str | None
NoneNone
elem_classes
list[str] | str | None
NoneNone
render
bool
TrueNone
load_fn
Callable[Ellipsis, Any] | None
NoneNone
every
float | None
NoneNone
starting_page
int | None
1None

Events

namedescription
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

Contributors

freddyaboulton

45 commits

LeoKingzc1

1 commits

maltenlz

1 commits

neosanma

1 commits

Languages

JavaScript

99.3%