Spaces:
Sleeping
Sleeping
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
|
4 |
+
def read_pdf(*inps):
|
5 |
+
pdf_file, start_index, end_index, text_rule = inps
|
6 |
+
reader = PdfReader(pdf_file)
|
7 |
+
pages = reader.pages
|
8 |
+
text = ""
|
9 |
+
for page in pages[int(start_index):int(end_index)+1]:
|
10 |
+
sub = page.extract_text()
|
11 |
+
if text_rule:
|
12 |
+
for rule in text_rule.split(";"):
|
13 |
+
sub = eval(rule)
|
14 |
+
text += sub
|
15 |
+
return text
|
16 |
+
|
17 |
+
with gr.Blocks() as demo:
|
18 |
+
with gr.Row():
|
19 |
+
with gr.Column():
|
20 |
+
pdf_file = gr.File(label="PDF", interactive=True)
|
21 |
+
with gr.Row():
|
22 |
+
start_index = gr.Number(label="start_page",value=0)
|
23 |
+
end_index = gr.Number(label="end_page",value=0)
|
24 |
+
text_rule = gr.Textbox(label="rule", value='sub.replace(" \n", "\n");sub.replace(" ", " ")')
|
25 |
+
submit = gr.Button(value="submit")
|
26 |
+
text_output = gr.Textbox(interactive=True)
|
27 |
+
|
28 |
+
inputs = [pdf_file, start_index, end_index, text_rule]
|
29 |
+
submit.click(fn=read_pdf, inputs=inputs, outputs=text_output)
|
30 |
+
|
31 |
+
demo.launch()
|