import gradio as gr import pdf2image import numpy as np # Convert a PDF to images def pdf_to_imgs(pdf): """ 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) first_page=True # we want here only the first page as image 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]) #return np.array(imgs[0]), num_pages title = "First page of a PDF >> image" description = "Drop a PDF (WARNING: only the first page will be converted into an image)." examples = [["example.pdf"]] css = ".output-image, .input-image, .image-preview {height: 600px !important}" allow_flagging = "never" live = False iface = gr.Interface(fn=pdf_to_imgs, #inputs=[gr.File(label="PDF"), gr.Checkbox(label="Only first page?", value=True)], inputs=gr.File(label="PDF"), # sdk_version: 3.0.2 #inputs=gr.inputs.File(type="file", label="PDF"), # sdk_version: 2.9.4 needed to use gr.Interface.load("spaces/pierreguillou/pdf2imgs") in other Spaces #outputs=[gr.Image(type="numpy", label="page image"), gr.Textbox(label="number of pages")], # sdk_version: 3.0.2 outputs=gr.Image(type="numpy", label="image of the first page"), # sdk_version: 3.0.2 title=title, description=description, examples=examples, #article=article, css=css, allow_flagging=allow_flagging, live=live ) iface.launch(debug=True, enable_queue=True)