-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
55 lines (41 loc) · 2.01 KB
/
gui.py
File metadata and controls
55 lines (41 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import gradio as gr
import shutil
import os
from pdf2norm import SmartPDFNormalizer
def process_pdf(pdf_file, insert_position):
if not pdf_file:
return None, None, None, "", "❌ Please upload a PDF file."
out_dir = os.path.abspath("gradio_outputs")
os.makedirs(out_dir, exist_ok=True)
# Получаем базовое имя файла
base_name = os.path.splitext(os.path.basename(pdf_file))[0]
input_path = os.path.join(out_dir, f"{base_name}_input.pdf")
output_path = os.path.join(out_dir, f"{base_name}_normalised.pdf")
report_txt = os.path.join(out_dir, f"{base_name}_report.txt")
report_json = os.path.join(out_dir, f"{base_name}_report.json")
shutil.copy(pdf_file, input_path)
normaliser = SmartPDFNormalizer(
input_path=input_path,
output_path=output_path,
report_txt=report_txt,
report_json=report_json,
insert_blank_at=(insert_position - 1 if insert_position else None)
)
normaliser.normalize()
with open(report_txt, "r", encoding="utf-8") as f:
report_preview = f.read()
return output_path, report_txt, report_json, report_preview, "✅ Normalisation completed successfully."
with gr.Blocks(title="SmartPDFNormalizer") as demo:
gr.Markdown("# 📄 SmartPDFNormalizer\nNormalise PDF page sizes and optionally insert a blank page.")
pdf_file = gr.File(label="Upload PDF", file_types=[".pdf"])
insert_page = gr.Number(label="Insert blank page at (optional)", value=0, precision=0)
run_button = gr.Button("Normalise PDF")
output_pdf = gr.File(label="📄 Normalised PDF")
output_txt = gr.File(label="📝 Text Report (.txt)")
output_json = gr.File(label="🧾 JSON Report (.json)")
report_preview = gr.Textbox(label="📋 Report Preview", lines=20)
status_message = gr.Markdown()
run_button.click(fn=process_pdf,
inputs=[pdf_file, insert_page],
outputs=[output_pdf, output_txt, output_json, report_preview, status_message])
demo.launch()