import gradio as gr import pdf2image import numpy as np # Convert a PDF to images def pdf_to_imgs(pdf, first_page): """ pdf: pdf file first_page: convert to image only the first page return numpy array of the first page and number of images """ # get path to pdf path_to_pdf = pdf.name # convert PDF to PIL images (one image by page) if first_page: last_page = 1 else: last_page = None imgs = pdf2image.convert_from_path(path_to_pdf, last_page=last_page) num_pages = len(imgs) return np.array(imgs[0]), num_pages title = "Interactive demo: PDF to images" description = "Enter um PDF and check the box for getting the image of only the first page." examples =[["Petição (pag 1 - 5)-1.pdf"]] css = ".output-image, .input-image, .image-preview {height: 600px !important}" iface = gr.Interface(fn=pdf_to_imgs, inputs=[gr.File(label="PDF"), gr.Checkbox(label="Only first page?", value=True)], outputs=[gr.Image(type="numpy", label="page image"), gr.Textbox(label="number of pages")], title=title, description=description, # examples=examples, #article=article, css=css) iface.launch(debug=True, enable_queue=True, share=True)