Spaces:
Runtime error
Runtime error
Commit
·
8b2ec29
1
Parent(s):
8c393df
first version
Browse files- app.py +38 -0
- packages.txt +1 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pdf2image
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# Convert a PDF to images
|
6 |
+
def pdf_to_imgs(pdf, first_page):
|
7 |
+
"""
|
8 |
+
pdf: pdf file
|
9 |
+
first_page: convert to image only the first page
|
10 |
+
return numpy array of the first page and number of images
|
11 |
+
"""
|
12 |
+
|
13 |
+
# get path to pdf
|
14 |
+
path_to_pdf = pdf.name
|
15 |
+
|
16 |
+
# convert PDF to PIL images (one image by page)
|
17 |
+
if first_page: last_page = 1
|
18 |
+
else: last_page = None
|
19 |
+
|
20 |
+
imgs = pdf2image.convert_from_path(path_to_pdf, last_page=last_page)
|
21 |
+
num_pages = len(imgs)
|
22 |
+
|
23 |
+
return np.array(imgs[0]), num_pages
|
24 |
+
|
25 |
+
title = "Interactive demo: PDF to images"
|
26 |
+
description = "Enter um PDF and check the box for getting the image of only the first page."
|
27 |
+
examples =[["Petição (pag 1 - 5)-1.pdf"]]
|
28 |
+
css = ".output-image, .input-image, .image-preview {height: 600px !important}"
|
29 |
+
|
30 |
+
iface = gr.Interface(fn=pdf_to_imgs,
|
31 |
+
inputs=[gr.File(label="PDF"), gr.Checkbox(label="Only first page?", value=True)],
|
32 |
+
outputs=[gr.Image(type="numpy", label="page image"), gr.Textbox(label="number of pages")],
|
33 |
+
title=title,
|
34 |
+
description=description,
|
35 |
+
# examples=examples,
|
36 |
+
#article=article,
|
37 |
+
css=css)
|
38 |
+
iface.launch(debug=True, enable_queue=True)
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
poppler-utils
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
pdf2image
|
2 |
+
numpy
|
3 |
+
gradio
|