File size: 931 Bytes
d660e96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from docling.parsers.pdf_parser import PDFParser
import tempfile
import os

def pdf_to_markdown(pdf_file):
    if pdf_file is None:
        return "No file uploaded."

    with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
        tmp.write(pdf_file.read())
        tmp_path = tmp.name

    try:
        parser = PDFParser(tmp_path)
        doc = parser.parse()
        markdown = doc.to_markdown()
        return markdown
    except Exception as e:
        return f"Error parsing PDF: {str(e)}"
    finally:
        os.remove(tmp_path)

iface = gr.Interface(
    fn=pdf_to_markdown,
    inputs=gr.File(label="Upload PDF", file_types=[".pdf"]),
    outputs=gr.Markdown(label="Markdown Output"),
    title="PDF to Markdown with docling",
    description="Upload a PDF file. This app parses it using `docling` and displays the Markdown version."
)

if __name__ == "__main__":
    iface.launch()