Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
import fitz # PyMuPDF
|
| 3 |
import os
|
| 4 |
import tempfile
|
| 5 |
-
import shutil
|
| 6 |
import zipfile
|
| 7 |
|
| 8 |
def extract_text(pdf_file):
|
|
@@ -10,7 +9,7 @@ def extract_text(pdf_file):
|
|
| 10 |
text = ""
|
| 11 |
for page in doc:
|
| 12 |
text += page.get_text()
|
| 13 |
-
return text
|
| 14 |
|
| 15 |
def extract_images(pdf_file):
|
| 16 |
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
|
@@ -35,7 +34,6 @@ def extract_images(pdf_file):
|
|
| 35 |
if img_count == 0:
|
| 36 |
return "No images found in the PDF.", None
|
| 37 |
|
| 38 |
-
# Zip all extracted images
|
| 39 |
zip_path = os.path.join(output_dir, "images.zip")
|
| 40 |
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 41 |
for fname in os.listdir(output_dir):
|
|
@@ -51,7 +49,7 @@ def merge_pdfs(pdf_files):
|
|
| 51 |
merged_pdf.insert_pdf(doc)
|
| 52 |
temp_path = tempfile.mktemp(suffix=".pdf")
|
| 53 |
merged_pdf.save(temp_path)
|
| 54 |
-
return temp_path
|
| 55 |
|
| 56 |
def split_pdf(pdf_file):
|
| 57 |
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
|
@@ -67,53 +65,56 @@ def split_pdf(pdf_file):
|
|
| 67 |
for fname in os.listdir(output_dir):
|
| 68 |
if fname.endswith(".pdf"):
|
| 69 |
zipf.write(os.path.join(output_dir, fname), fname)
|
| 70 |
-
return zip_path
|
| 71 |
|
| 72 |
-
def pdf_tool(task, pdf_input1
|
| 73 |
if task == "Extract Text":
|
| 74 |
if not pdf_input1:
|
| 75 |
return "Please upload a PDF file.", None
|
| 76 |
-
return extract_text(pdf_input1)
|
| 77 |
|
| 78 |
elif task == "Extract Images":
|
| 79 |
if not pdf_input1:
|
| 80 |
return "Please upload a PDF file.", None
|
| 81 |
-
|
| 82 |
-
return msg, zip_path
|
| 83 |
|
| 84 |
elif task == "Merge PDFs":
|
| 85 |
if not pdf_input1 or not pdf_input2:
|
| 86 |
return "Please upload two PDF files to merge.", None
|
| 87 |
-
|
| 88 |
-
return "PDFs merged successfully.", merged_path
|
| 89 |
|
| 90 |
elif task == "Split PDF":
|
| 91 |
if not pdf_input1:
|
| 92 |
return "Please upload a PDF file.", None
|
| 93 |
-
|
| 94 |
-
return "PDF split into separate pages.", zip_path
|
| 95 |
|
| 96 |
else:
|
| 97 |
return "Invalid task selected.", None
|
| 98 |
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
gr.Markdown("## 🛠️ PDF Utility Tool")
|
| 101 |
-
task = gr.Radio(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
pdf_input2 = gr.File(label="PDF File 2 (for Merge only)", file_types=[".pdf"], visible=True)
|
| 106 |
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
| 112 |
|
| 113 |
def process(task, pdf_input1, pdf_input2):
|
| 114 |
result_text, result_file = pdf_tool(task, pdf_input1, pdf_input2)
|
| 115 |
-
return result_text, gr.update(value=result_file, visible=
|
| 116 |
|
|
|
|
| 117 |
run_button.click(process, inputs=[task, pdf_input1, pdf_input2], outputs=[output_text, output_file])
|
| 118 |
|
| 119 |
demo.launch()
|
|
|
|
| 2 |
import fitz # PyMuPDF
|
| 3 |
import os
|
| 4 |
import tempfile
|
|
|
|
| 5 |
import zipfile
|
| 6 |
|
| 7 |
def extract_text(pdf_file):
|
|
|
|
| 9 |
text = ""
|
| 10 |
for page in doc:
|
| 11 |
text += page.get_text()
|
| 12 |
+
return text, None
|
| 13 |
|
| 14 |
def extract_images(pdf_file):
|
| 15 |
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
|
|
|
| 34 |
if img_count == 0:
|
| 35 |
return "No images found in the PDF.", None
|
| 36 |
|
|
|
|
| 37 |
zip_path = os.path.join(output_dir, "images.zip")
|
| 38 |
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 39 |
for fname in os.listdir(output_dir):
|
|
|
|
| 49 |
merged_pdf.insert_pdf(doc)
|
| 50 |
temp_path = tempfile.mktemp(suffix=".pdf")
|
| 51 |
merged_pdf.save(temp_path)
|
| 52 |
+
return "PDFs merged successfully.", temp_path
|
| 53 |
|
| 54 |
def split_pdf(pdf_file):
|
| 55 |
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
|
|
|
| 65 |
for fname in os.listdir(output_dir):
|
| 66 |
if fname.endswith(".pdf"):
|
| 67 |
zipf.write(os.path.join(output_dir, fname), fname)
|
| 68 |
+
return "PDF split into separate pages.", zip_path
|
| 69 |
|
| 70 |
+
def pdf_tool(task, pdf_input1, pdf_input2):
|
| 71 |
if task == "Extract Text":
|
| 72 |
if not pdf_input1:
|
| 73 |
return "Please upload a PDF file.", None
|
| 74 |
+
return extract_text(pdf_input1)
|
| 75 |
|
| 76 |
elif task == "Extract Images":
|
| 77 |
if not pdf_input1:
|
| 78 |
return "Please upload a PDF file.", None
|
| 79 |
+
return extract_images(pdf_input1)
|
|
|
|
| 80 |
|
| 81 |
elif task == "Merge PDFs":
|
| 82 |
if not pdf_input1 or not pdf_input2:
|
| 83 |
return "Please upload two PDF files to merge.", None
|
| 84 |
+
return merge_pdfs([pdf_input1, pdf_input2])
|
|
|
|
| 85 |
|
| 86 |
elif task == "Split PDF":
|
| 87 |
if not pdf_input1:
|
| 88 |
return "Please upload a PDF file.", None
|
| 89 |
+
return split_pdf(pdf_input1)
|
|
|
|
| 90 |
|
| 91 |
else:
|
| 92 |
return "Invalid task selected.", None
|
| 93 |
|
| 94 |
with gr.Blocks() as demo:
|
| 95 |
gr.Markdown("## 🛠️ PDF Utility Tool")
|
| 96 |
+
task = gr.Radio(
|
| 97 |
+
choices=["Extract Text", "Extract Images", "Merge PDFs", "Split PDF"],
|
| 98 |
+
label="Select a Task",
|
| 99 |
+
value="Extract Text"
|
| 100 |
+
)
|
| 101 |
|
| 102 |
+
pdf_input1 = gr.File(label="PDF File 1", file_types=[".pdf"])
|
| 103 |
+
pdf_input2 = gr.File(label="PDF File 2 (only for Merge)", file_types=[".pdf"], visible=False)
|
|
|
|
| 104 |
|
| 105 |
+
output_text = gr.Textbox(label="Result / Output", lines=5)
|
| 106 |
+
output_file = gr.File(label="Download File", visible=False)
|
| 107 |
|
| 108 |
+
def update_file2_visibility(t):
|
| 109 |
+
return gr.update(visible=(t == "Merge PDFs"))
|
| 110 |
+
|
| 111 |
+
task.change(update_file2_visibility, inputs=task, outputs=pdf_input2)
|
| 112 |
|
| 113 |
def process(task, pdf_input1, pdf_input2):
|
| 114 |
result_text, result_file = pdf_tool(task, pdf_input1, pdf_input2)
|
| 115 |
+
return result_text, gr.update(value=result_file, visible=result_file is not None)
|
| 116 |
|
| 117 |
+
run_button = gr.Button("Run")
|
| 118 |
run_button.click(process, inputs=[task, pdf_input1, pdf_input2], outputs=[output_text, output_file])
|
| 119 |
|
| 120 |
demo.launch()
|