Spaces:
Build error
Build error
| import gradio as gr | |
| import PyPDF2 | |
| import os | |
| def merge_pdfs(pdf_list, output_filename): | |
| """ | |
| Merge multiple PDF files into a single PDF file with a specified filename. | |
| Parameters: | |
| pdf_list (list): List of uploaded PDF files (file-like objects). | |
| output_filename (str): Desired name of the output merged PDF file. | |
| Returns: | |
| str: Path to the merged PDF file. | |
| """ | |
| pdf_merger = PyPDF2.PdfMerger() | |
| # If no extension is provided, add .pdf | |
| if not output_filename.lower().endswith('.pdf'): | |
| output_filename += '.pdf' | |
| for pdf_file in pdf_list: | |
| pdf_file_path = pdf_file.name # Get the path from the uploaded file object | |
| pdf_merger.append(pdf_file_path) | |
| with open(output_filename, 'wb') as output_pdf: | |
| pdf_merger.write(output_pdf) | |
| return output_filename | |
| # Gradio interface | |
| def gradio_pdf_merge(pdf_files, output_filename): | |
| output_pdf_path = merge_pdfs(pdf_files, output_filename) | |
| return output_pdf_path | |
| # Create the Gradio interface | |
| gr_interface = gr.Interface( | |
| fn=gradio_pdf_merge, # The function to run when files are uploaded | |
| inputs=[ | |
| gr.Files(file_types=['.pdf'], label="Upload PDF files"), # Allow multiple PDFs to be uploaded | |
| gr.Textbox(label="Output PDF filename", placeholder="Enter the output filename (e.g., merged.pdf)") # Input for output filename | |
| ], | |
| outputs=gr.File(label="Merged PDF") # Return the merged PDF file | |
| ) | |
| # Launch the interface | |
| gr_interface.launch() | |