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]), num_pages return np.array(imgs[0]) title = "PDF to images" description = "Drop a PDF (WARNING: only the first page will be converted into an image)." examples =[["exemple.pdf"]] css = ".output-image, .input-image, .image-preview {height: 600px !important}" allow_flagging = "never" theme = "peach" # default, huggingface, grass, peach 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 Speces #outputs=[gr.Image(type="numpy", label="page image"), gr.Textbox(label="number of pages")], #outputs=gr.outputs.Image(type="numpy", label="image of the first page"), # sdk_version: 2.9.4 needed to use gr.Interface.load("spaces/pierreguillou/pdf2imgs") in other Speces outputs=gr.outputs.Image(label="image of the first page"), # sdk_version: 2.9.4 needed to use gr.Interface.load("spaces/pierreguillou/pdf2imgs") in other Speces title=title, description=description, examples=examples, #article=article, css=css, allow_flagging=allow_flagging, theme=theme ) iface.launch(debug=False, enable_queue=True)