File size: 3,181 Bytes
ced780f
 
 
 
6e26061
 
 
 
 
 
 
 
 
 
 
 
 
 
ced780f
 
 
 
 
 
 
 
 
 
 
 
 
6e26061
 
ced780f
6e26061
ced780f
6e26061
ced780f
6e26061
ced780f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e26061
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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)