herryg's picture
Update app.py
3f4042d verified
import gradio as gr
import os
# Import the replace function from your local pdf_parser module
from pdf_parser.api import replace_pdf_text
def replace_text_in_pdf(input_pdf, find_text, replace_text, page_num, instance_index, allow_auto_insert):
output_path = "output/output.pdf" # Updated to match where the PDF is saved
try:
success = replace_pdf_text(
input_pdf=input_pdf,
output_pdf=output_path,
target_text=find_text,
replacement_text=replace_text,
page_num=page_num if page_num >= 0 else None,
instance_index=instance_index,
allow_auto_insert=allow_auto_insert,
verbose=1
)
except Exception as e:
return f"Error during processing: {str(e)}", None
if not success or not os.path.exists(output_path):
return "Replacement failed ❌. Please check font compatibility, input text, or page settings.", None
return "Replacement successful βœ…. Download the modified PDF below.", output_path
demo = gr.Interface(
fn=replace_text_in_pdf,
inputs=[
gr.File(file_types=[".pdf"], label="Upload PDF File"),
gr.Textbox(label="Text to Find"),
gr.Textbox(label="Replacement Text"),
gr.Number(label="Page Number (0-based, -1 for all pages)", value=-1),
gr.Number(label="Instance Index (-1 for all matches)", value=-1),
gr.Checkbox(label="Allow Auto-Insertion of Missing Characters", value=False)
],
outputs=[
gr.Textbox(label="Status"),
gr.File(label="Modified PDF")
],
title="PDF Text Replacement Tool",
description="Upload a PDF and replace specific text accurately using the local pdf_parser module."
)
demo.launch()