ma7583 commited on
Commit
d660e96
·
verified ·
1 Parent(s): 4e631b1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from docling.parsers.pdf_parser import PDFParser
3
+ import tempfile
4
+ import os
5
+
6
+ def pdf_to_markdown(pdf_file):
7
+ if pdf_file is None:
8
+ return "No file uploaded."
9
+
10
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
11
+ tmp.write(pdf_file.read())
12
+ tmp_path = tmp.name
13
+
14
+ try:
15
+ parser = PDFParser(tmp_path)
16
+ doc = parser.parse()
17
+ markdown = doc.to_markdown()
18
+ return markdown
19
+ except Exception as e:
20
+ return f"Error parsing PDF: {str(e)}"
21
+ finally:
22
+ os.remove(tmp_path)
23
+
24
+ iface = gr.Interface(
25
+ fn=pdf_to_markdown,
26
+ inputs=gr.File(label="Upload PDF", file_types=[".pdf"]),
27
+ outputs=gr.Markdown(label="Markdown Output"),
28
+ title="PDF to Markdown with docling",
29
+ description="Upload a PDF file. This app parses it using `docling` and displays the Markdown version."
30
+ )
31
+
32
+ if __name__ == "__main__":
33
+ iface.launch()