import os import gradio as gr import tempfile from pathlib import Path from utils import get_file_name, run_pdf2text, run_paper2slides # Create a temporary directory for outputs OUTPUT_FOLDER = tempfile.mkdtemp(prefix="pdf_slides_") def paper2slides(pdf_path: str, logo_path: str='logo.npg'): # Ensure the output directory exists os.makedirs(OUTPUT_FOLDER, exist_ok=True) output_path = os.path.join(OUTPUT_FOLDER, "converted_slides.pptx") file_name = get_file_name(full_path=pdf_path) run_pdf2text(paper_pdf_path=pdf_path, save_json_name=file_name + '.json') ## pdf to json file run_paper2slides(paper_json_name=file_name + '.json', model='llama3_70b', logo_path=logo_path, save_file_name=output_path) ## json file to slides return output_path def convert_pdf_to_slides(pdf_path): """ Convert a PDF file to a slide deck. Args: pdf_path (str): Path to the input PDF file Returns: str: Path to the created slide deck file """ print(f"Processing PDF: {pdf_path}") # Ensure the output directory exists os.makedirs(OUTPUT_FOLDER, exist_ok=True) # Create output filename in the temporary directory output_filename = f"{Path(pdf_path).stem}_slides.pptx" output_path = os.path.join(OUTPUT_FOLDER, output_filename) output_path = paper2slides(pdf_path, 'logo.png') print(f"Conversion complete. Slides saved to: {output_path}") return output_path def process_upload(pdf_file): """ Process the uploaded PDF file and return the path to the generated slide deck. Args: pdf_file (tempfile): The uploaded PDF file Returns: str: Path to the slide deck for download """ if pdf_file is None: return None try: # Convert the PDF to slides slide_path = convert_pdf_to_slides(pdf_file.name) return slide_path except Exception as e: print(f"Error during conversion: {str(e)}") return None # Create the Gradio interface with gr.Blocks(title="PDF to Slides Converter") as app: gr.Markdown("# PDF to Slides Converter") gr.Markdown("Upload a PDF file to convert it into a slide deck presentation.") with gr.Row(): with gr.Column(): pdf_input = gr.File(label="Upload PDF File", file_types=[".pdf"]) convert_btn = gr.Button("Convert to Slides", variant="primary") with gr.Column(): output_file = gr.File(label="Download Slides") status = gr.Markdown("Upload a PDF and click 'Convert to Slides'") convert_btn.click( fn=process_upload, inputs=[pdf_input], outputs=[output_file], api_name="convert" ) pdf_input.change( fn=lambda x: "PDF uploaded. Click 'Convert to Slides' to process." if x else "Upload a PDF and click 'Convert to Slides'", inputs=[pdf_input], outputs=[status] ) # Launch the app if __name__ == "__main__": app.launch() # Optional: Clean up the temporary directory when the app exits import shutil shutil.rmtree(OUTPUT_FOLDER, ignore_errors=True)